in reply to Re: array splitting and sorting
in thread array splitting and sorting

Thanks for your wisdom.

I tried this:

while (<FILE>){ #FILE=filehandle chomp; (@last, @first, @age, @sex, @height, @weight, @Comment) = split (/\,/) +; }

however it did not work. I am assuming that the reason it did not work was cause I was using the string split, and tring to convert it to spliting an array.

Thanks again for your help. I will try this out.

A greatfull, Traineeeee.....

Replies are listed 'Best First'.
Re: Re: Re: array splitting and sorting
by tachyon (Chancellor) on Jun 01, 2001 at 09:55 UTC

    Your problem is that the while iterates over each record in turn (setting it to the magical $_), thus you need to set each element of each array in turn, this is done by modifying your code thusly:

    $i = -1; while (<FILE>){ chomp; $i++; ($last[$i], $first[$i], $age[$i], $sex[$i], $height[$i], $weight[$i +]) = split/,/; } die "Sorry, no data\n" if $i == -1;

    Note you do not need the \ before the comma in your split or the parenths. Presume you have no problems with the sort, if not you can use:

    @alphabetical = sort @alphabetical # the above is equivalent to @alphabetical = sort {$a cmp $b} @alphabetical @numerical = sort {$a <=> $b} @numerical; # to reverse the sort order reverse the positions of $a and $b, or use + reverse sort @a

    Hope this helps

    Cheers

    tachyon