LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   arithmetic on alphanumeric in PERL (https://www.linuxquestions.org/questions/programming-9/arithmetic-on-alphanumeric-in-perl-668355/)

gaynut 09-08-2008 06:58 AM

arithmetic on alphanumeric in PERL
 
Hi,

Is it possible to do arithmetic on alphanumeric data?

Eg: say i get the input as 01F01,01F02 etc.

If i have to generate the numbers till "n", what should my datatype read to perform the operation.
And also let me know how to do it.





Thanks,
~ gaynut

matthewg42 09-08-2008 07:18 AM

interpretted in what radix, hexidecimal? Yes, just prefix with "0x" when interpreting, and use printf to format as hex:
Code:

printf "%x\n", (0x2 * 0xf);

sundialsvcs 09-08-2008 09:09 AM

Usually, what I prefer to do is to set up a regular-expression, which extracts the "useful" parts of the information while also being sure that the input actually does conform to the expected syntax.

Perl provides a full complement of "printf/scanf"-type formatting routines, and it's also pretty darned smart when it's "type-casting" information to-and-from a string. This is to be expected, since Perl is really a power-tool for manipulating strings.

As you explore your project, look first at CPAN. Open your mind to any possibility that you can find. No matter what you are setting out to do, CPAN has probably already done it. You will either find complete applications or modules that will give you a huge head-start...


All times are GMT -5. The time now is 10:13 AM.