The only note would be that Jenda use alpha numeric data(a,b,c) and
referenced <=> which is numeric( where cmp does the alphanumeric ) within
the sort routine.  Just a heads up.

Wags ;)

-----Original Message-----
From: Jenda Krynicky [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 13:53
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: How to get the biggest integers from an array?


From: Rob Richardson <[EMAIL PROTECTED]>
> 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;

This is something most Perl programmers would have a hard time 
understanding. The construct comes from functional languages.
 
> 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.
> 
> 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.
 
> 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.

Here the code uses something we call "array slice". For example if 
you want to pass somewhere a list containing the first, third a 
fourth element of an array you would write this:

        some_function( @array[0,2,3])

You can also assign to an array slice:

        @array[0,2,4] = (1,1,1)

And the stuff inside those square brackets may be any expression 
returning a list of numbers, it doesn't have to be a constant.

> 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.  

Yes that's it. There are no statements inside the brackets, it's a 
single (yet complex) expression. Let's add some braces in there:

@website[
        map  $_->[0], (
                sort { $b->[1] <=> $a->[1] } (
                        map  [ $_, $array[$_] ], (0 .. $#array)
                )
        )
] = 1 .. @array;

Though I would probably write it as

@website[
        map  {$_->[0]} (
                sort { $b->[1] <=> $a->[1] } (
                        map {[ $_, $array[$_] ]} (0 .. $#array)
                )
        )
] = 1 .. @array;


As you found out you have to read such things inside out.

> 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.

[ $_, $array[$_] ] creates an anonymous array whose first element is 
the index and the second the value of @array at that index and 
returns a reference to it.

That is the map{}() gets a list of indices to @array and returns an 
array of arrays containing both the indices and the values.
An example:

        @array = ('a','c','b');
        map {[ $_, $array[$_] ]} (0 .. $#array)

creates a list like this

        ( [0,'a'], [1,'c'], [2,'b'] )


> 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.

It would not make much sense to sort on the references. We want to 
sort on the second elements of the referenced arrays.
So my example would be

        ( [0,'a'], [2,'b'], [1,'c'] )

at this point.

> 6.  The outer map statement applies "$_->[0]" to the list returned by
> the sort.  I do not know what this means either.

It extracts the first element of the arrays. That is the index into 
the @array. So in my example the map would produce

        ( 0, 2, 1)

> 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].

@array = (8, 1, 7, 2, 6, 3, 5, 4);

inner map => ( [ 0, 8 ], [ 1, 1 ], [ 2, 7 ], [ 3, 2 ], [ 4, 6 ], [ 5, 
3 ], [ 6, 5 ], [ 7, 4 ] )

sort => ( [ 0, 8 ], [ 2, 7 ], [ 4, 6 ], [ 6, 5 ], [ 7, 4 ], [ 5, 3 ], 
[ 3, 2 ], [ 1, 1 ] )

outer map => ( 0, 2, 4, 6, 7, 5, 3, 1 )

So the assignment is :

@website[ 0, 2, 4, 6, 7, 5, 3, 1 ] = (1 .. @array);

So you were right :-)

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to