"B. Rothstein" wrote:
> If I have a scalar variable that itslef is a list of names and numbers, for
> example
> > $names = 'john 35, jack 18, albert 24, timmy 42'; is it possible, and if
> so how can it be done to separate the individual names and ages from the
> list in their scalar form in order to create new lists sorted by names and
> ages. thanks for any suggestions.
>
> my @names = sort split /, ?/, $names;
>
> this worked very well for the names only but did not sort properly for ages
> as well
>
> G'Night and good luck.
>
> James
Strings sort one way. Numbers sort in another. Consider
1
10
100
101
102
11
110
111
12
13
2
20
21
22
23
3
4
That is a perfect sort of those numbers, as strings. The alphabetical sort uses
the default {$a cmp $b} comparison. Numerical sort needs to be specified with
{$a <=> $b} to indicate numerical comparision. The sort function is highly
flexible. You can offer almost any function that will render values of -1, 0 or
1 based on the comparison between any two elements$a and $b.
BTW, this is one very good reason to avoid using literals as variable names.
The $a and $b variables are special Perl variables intended for use in sort, and
should not be used in any other context.
To get your numerical sort, you want:
@sorted_array = sort {$a <=> $b} @numerical_array;
perldoc -f sort
Joseph
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]