Home > Archive > PERL Beginners > September 2006 > code explanation please...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
code explanation please...
|
|
| onlineviewer 2006-09-05, 6:57 pm |
| Hello All,
This is an example from the O'Reilly Perl References, Modules book. I
see the end result here, but i'm a bit fuzzy as to why this line:
@ranks[@sorted_positions] = (0..@sorted_positions);
does what it does. It's assigning the indicies of the @sorted_postition
elements to the array @ranks indicies ? Is that right ? The end result
@ranks array tells you which index number each element occurs at. Some
explanation please. Thanks,,.
my @input = qw(Gilligan Skipper Professor Ginger Mary_Ann);
my @sorted_positions = sort { $input[$a] cmp $input[$b] } 0..$#input;
my @ranks;
@ranks[@sorted_positions] = (0..@sorted_positions);
print "@ranks\n";
| |
| Paul Lalli 2006-09-05, 6:57 pm |
| onlineviewer wrote:
> This is an example from the O'Reilly Perl References, Modules book. I
> see the end result here, but i'm a bit fuzzy as to why this line:
> @ranks[@sorted_positions] = (0..@sorted_positions);
> does what it does. It's assigning the indicies of the @sorted_postition
> elements to the array @ranks indicies ? Is that right ?
Yes. This is using what is known as an array slice. It may make more
sense to you if you look at the list that the array @sorted_positions
contains, and spell out the range operator. See below.
> The end result
> @ranks array tells you which index number each element occurs at. Some
> explanation please. Thanks,,.
>
> my @input = qw(Gilligan Skipper Professor Ginger Mary_Ann);
> my @sorted_positions = sort { $input[$a] cmp $input[$b] } 0..$#input;
So at this point, @sorted_positions contains the indices of @input, in
the order of @input sorted alphabetically - (0, 3, 4, 2, 1). That is,
0 is first because $input[0] is alphabetically first. 3 is second
because $input[3] is alphabetically second. Etcetera.
> my @ranks;
> @ranks[@sorted_positions] = (0..@sorted_positions);
This statement is now equivalent to this:
@ranks[0, 3, 4, 2, 1] = (0, 1, 2, 3, 4 5);
Which is exactly the same as the following five individual statements:
$ranks[0] = 0;
$ranks[3] = 1;
$ranks[4] = 2;
$ranks[2] = 3;
$ranks[1] = 4;
(the 5 on the right is ignored, due to having less elements on the left
than values on the right).
Does this help to clarify, or is there something specific you don't
understand?
Paul Lalli
| |
| onlineviewer 2006-09-05, 6:57 pm |
| This makes perfect sense. I appreciate the explanation.
Thanks,
Paul Lalli wrote:
> onlineviewer wrote:
>
> Yes. This is using what is known as an array slice. It may make more
> sense to you if you look at the list that the array @sorted_positions
> contains, and spell out the range operator. See below.
>
>
> So at this point, @sorted_positions contains the indices of @input, in
> the order of @input sorted alphabetically - (0, 3, 4, 2, 1). That is,
> 0 is first because $input[0] is alphabetically first. 3 is second
> because $input[3] is alphabetically second. Etcetera.
>
>
> This statement is now equivalent to this:
> @ranks[0, 3, 4, 2, 1] = (0, 1, 2, 3, 4 5);
> Which is exactly the same as the following five individual statements:
> $ranks[0] = 0;
> $ranks[3] = 1;
> $ranks[4] = 2;
> $ranks[2] = 3;
> $ranks[1] = 4;
> (the 5 on the right is ignored, due to having less elements on the left
> than values on the right).
>
> Does this help to clarify, or is there something specific you don't
> understand?
>
> Paul Lalli
|
|
|
|
|