Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Splitting every 3 digits?

by sifukurt (Hermit)
on Oct 01, 2001 at 17:21 UTC ( #115854=note: print w/replies, xml ) Need Help??


in reply to Splitting every 3 digits?

The only thing you'll have to be wary of with some of the previous examples is if your string, $a, contains a number of digits that isn't evenly divisible by 3, your array won't contain the last 1 or 2 digits. If that is what you want, then you needn't worry about this. If, however, you want the last element in your array to contain the last digits even if the length of your string isn't evenly divisible by 3, you'll want to do something like this:
$a = '12345678901'; while ( $a =~ /(\d{3})/g ) { push ( @nums, $1 ); $last = $'; } if ( length($a) % 3 ) { push ( @nums, $last ); }
In the above example, @nums will contain:
123
456
789
01

There may be a more elegant way of doing this, but it works. Hope this helps.
___________________
Kurt

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://115854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2023-05-31 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?