1f45 Slicing a long number - Perl Programming
Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Slicing a long number

Posted by urno (urno), 19 May 2006
Hi,

I have a long number:

4034404144414442444344444445

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?

Thanks.

Posted by Custard (Custard), 19 May 2006
I 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.

Code:
my $number=1234567890;
$number=reverse( $number );
$number=~s/(\d{4})/$1\t/g;
$number=reverse( $number );


In your case, you might not even need the reverses.

hth

Bruce

Posted by urno (urno), 19 May 2006
No. I'm not getting it yet!

What i need is to transform the long number:

4034404144414442444344444445

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....

Urno.

Posted by admin (Graham Ellis), 20 May 2006
Urno, 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?

- Graham

Posted by Custard (Custard), 20 May 2006
Hi,

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.

HTH

Bruce

Posted by urno (urno), 22 May 2006
D'oh!

What can i say....

Many thanks for the help.




Posted by Custard (Custard), 22 May 2006
Yes, I l know

I made the same mistake too though..

HTH

B

Posted by urno (urno), 22 May 2006
I *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?

Cheers,

Urno.



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.
fb5c

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho
0