Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i need to split a number into individual numbers ie $number = 123456 $item1 = 6 $item2 = 5 $item3 = 4 the question is how do i get from $number to $item? i have tried using split, but there is no pattern..

Replies are listed 'Best First'.
Re: banna split it aint
by japhy (Canon) on Feb 10, 2000 at 05:44 UTC
    unpack() is NOT sick. Simple string splitting is something that unpack() is great for, and it's a great way to LEARN how to use unpack(). You could also use:
    push @parts, substr($num,0,3,"") while $num ne "";
Re: banna split it aint
by vince (Initiate) on Feb 09, 2000 at 21:13 UTC
    you can use this
    ($item6, $item5, $item4, $item3, $item2, $item1) = split //, $number
    or this
    ($item6, $item5, $item4, $item3, $item2, $item1) = split '', $number
    but i suspect that what you need is an array, so you should do this
    @items = split //, $number
    or if you want it the the reverse order
    @items = reverse split //, $number
Re: banna split it aint
by btrott (Parson) on Feb 09, 2000 at 23:24 UTC
    Or you could use:
    my @items = reverse unpack 'A'x(length $number), $number;
    which, from my benchmarking, is *slightly* faster, and probably more easily extended to splitting a string on variable-length segments. Say, for example, that you wanted to split your string into segments of length 3:
    my @items = reverse unpack 'A3'x(length($number)/3), $number;
    Not that you asked about that, of course. :)
Re: banna split it aint
by jamieamieamily (Initiate) on Feb 10, 2000 at 07:38 UTC
    What about something like the following (it works for me):
    $number = 123456; ($item1,$item2) = ($number =~ /(\d)(\d)/);
    where, if $item1 or $item2 need to be greater than 1 digit you can just add "\d"'s
RE: banna split it aint
by Anonymous Monk on Feb 10, 2000 at 03:10 UTC
    unpack? You guys are sick. How about nice and simple?
    @items = ( $num =~ /\d/g );
    will do what you want, as long as you are certain you always want it split into individual digits. To be more interesting,
    @items = ( $num =~ /\d{1,$n}/g );
    will split the number into as many groups of $n digits as it can and the last element of the array will be any remaining digits (ie, length($num) % n ). Mik
RE: banna split it aint
by vince (Initiate) on Feb 09, 2000 at 21:22 UTC
    you can use this
    ($item6, $item5, $item4, $item3, $item2, $item1) = split //, $number;
    or this
    ($item6, $item5, $item4, $item3, $item2, $item1) = split '', $number;
    but i suspect that what you need is an array, so you should do this
    @items = split //, $number;
    or if you want it the the reverse order
    @items = reverse split //, $number;
RE: banna split it aint
by Anonymous Monk on Feb 10, 2000 at 02:02 UTC
    $foo = '123456'; @items = reverse(split //,$foo); foreach $item (@items){ $i++; $temp = 'item'.$i; $$temp = $item; print"$$temp\n"; } Though, if you don't *need* having the items in their own individual variables, you can skip the foreach loop and simply use @items.