Slicing a long number
Posted by urno (urno), 19 May 2006Hi,
I have a long number:
Which i'd like to divide into tab separated "4's" (eg. 4034atabhere4041anothertab4441 etc).
Am i right in thinking that "unpack" would be the way to do this?
Posted by Custard (Custard), 19 May 2006I don't think unpack can do that for you..
However, this is a classic 'putting commas in numbers' question.
There are as always many ways to do it.
My fave is the reverse, regex, reverse pattern.
In your case, you might not even need the reverses.
Posted by urno (urno), 19 May 2006No. I'm not getting it yet!
What i need is to transform the long number:
into tab separated:
4034 4041 4441 4442 4443 4444 4445
when i put 12 digits through your suggested code it's ok, but if put a further 4 then it comes out rather oddly:
4.0344 0414 442e+15
I tried fiddling with the code (minus the reverses) but can't get it to play ball with any more than 12 digits....
Posted by admin (Graham Ellis), 20 May 2006Urno, Custard's code looks good to me. Can you post up your modified version of it so that we can look at that and see if we can spot anything?
Posted by Custard (Custard), 20 May 2006Hi,
Try putting the number in quotes (a string).
Perl has turned your very large integer into scientific notation hence the 4.0 ... 2e+15 that is appearing.
Posted by urno (urno), 22 May 2006D'oh!
What can i say....
Many thanks for the help.
Posted by Custard (Custard), 22 May 2006Yes, I l know
I made the same mistake too though..
Posted by urno (urno), 22 May 2006I *had* a new problem, which i've since overcome without Perl, but i'd be interested to see if you could advise me on how i could've done it with Perl.
I omitted the full example of a line of the data i'm dealing with earlier, which contains mostly fixed format data:
"CDHY44282Bloggs, Joseph, T. CHRIChrist Church 4034404144414442444344444445"
The white spaces above are spaces (ie. not tabs).
I was chuntering on with this unpack code:
my ($dcode,$ccode) = unpack("A4A5",$line);
..until i realised that i was fast approaching the "name" bit, which cannot be counted through due to it's unfixed length.
So, is there any way to "unpack until" eg. "unpack until you get to a | or \t"?
Or would you recommend a different angle of attack?
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho