On Wed, Dec 11, 2002 at 11:31:25AM -0800, Rob Richardson wrote:
> Paul,
Hello Rob,
> I am a Perl newbie, but I am employed as a Visual C++ and Visual Basic
> programmer. I'm having trouble figuring out what your construct is
> doing. For reference, here it is:
>
> > @website[
> > map $_->[0],
> > sort { $b->[1] <=> $a->[1] }
> > map [ $_, $array[$_] ],
> > 0 .. $#array
> > ] = 1 .. @array;
>
> Let me see if I can figure out what this is doing, and I would
> appreciate it if you could fill in the holes. Hmmm... On second
> thought, I am more baffled than I thought I was. Let's see where I can
> get anyway.
You've obviously put a fair bit of effort into this, and you've got most
of it. Thanks for spending the time on it.
> 1. "@array" used in numeric context returns the number of elements in
> the array. So, you are assigning the numbers 1 through n to elements
> of the @website array.
Yes. Technically we are assigning to a slice of the array, so it would
be as well to ensure that there is nothing else in the array to start
with.
> 2. The order in which the elements of the @website array are filled is
> determined by the code within the brackets. Code within brackets is a
> completely new concept to me.
Yes. Within the brackets we need a list. In this case we are using an
expression to generate that list.
> 3. There are two Perl statements withing @website's brackets, both of
> them being map calls. I can't explain why there aren't any semicolons
> in there. If this is incorrect, my second guess is that there is a
> single map statement in there that has another map statement inside it.
> No, I'm not on the right track. It's a map that uses a sort that uses
> a map.
Correct again. Got there in the end ;-)
> 4. The innermost map statement is "map [ $_, $array[$_] ], 0 ..
> $#array. This is of the "map EXPR, LIST" form, which returns a list
> built by applying the expression to the list. Aside from knowing that
> $_ is the default argument, I do not know what "[ $_, $array[$_] ]"
> does.
You are correct about the map. [] creates an anonymous array and
returns a reference to it. Within this array are two elements: $_, the
index into @array and $array[$_], the element at that index.
So what we are doing is duplicating @array, but with each element we are
also storing its original position in @array.
> 5. The sort statement sorts the list returned by the inner map from
> highest to lowest value. I've seen the "$b <=> $a" syntax before (but
> only in a book). I don't know what the "[1]"s are in there for.
Perl sets $a and $b to be references to the elements of the list to be
compared for the sort. The list returned from the map is a list of
array references, with the original element of @array in the second
position. So we need to dereference the array reference and sort on the
second element in the array. That is what ->[1] does.
<=> is the spaceship operator. It numerically compares its operands and
returns 0, a negative number or a positiive number depending on the
comparison. This is just what sort needs.
> 6. The outer map statement applies "$_->[0]" to the list returned by
> the sort. I do not know what this means either.
At this point we have our list of array references sorted by the values
in @array. ->[0] retrieves the original position of the element in
@array that we stored in the first map. This list of the original
positions defines the slice to which we assign.
> 7. If we assume an input array of (8, 1, 7, 2, 6, 3, 5, 4), the
> complete statement would first put "1" in $websites[0], then put "2" in
> $websites[2], then put "3" in $websites[4], then put "4" in
> $websites[6], then put "5" in $websites[7], then put "6" in
> $websites[5], then put "7" in $websites[3], and then put "8" in
> $$websites[1].
>
> OK, how far off am I?
I think the only thing you were missing was the stuff to do with the
array references.
> Thanks!
I realise that was rather throwing you in at the deep end, but I think
you swam ;-)
--
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]