# Help on Schwartzian method

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on

Hi all,

I was reading through UR coloumns by Randal in www.stonehenge.com. I
came across the code which i really tried heating up my brain ..but
with no result. Can anyone help me understand how the concept goes.

(1)

@data = <>; # read data
foreach (@data) {
(\$name,\$score) = split; # get score
\$score = \$score; # record it
}

I guess \$_ contains  what is read from @data at a given time in
the loop.Am I  correct?

(2)
But what is \$_ in

@pairs = map {
(\$name, \$score) = split;
[ \$_, \$score ];
} @data;

Randal says" I build a two-element anonymous list from the \$score and
the original value \$_"

what does that mean?

(3)
print
map { \$_->[0] }
sort { \$a->[1] <=> \$b->[1] }
map { [\$_, (split)[1] ] }
<>;

Now  I dont even have any clue on what is \$_->[0],\$a->[1]  and  \$b-

regards,
jis

## help on Schwartzian method

Hi all,

I was reading through  UR coloumn by Randal in ww.stonehenge.com. I
cam across the Schwartzian method which I find extremely difficult to
understand the concepts.

1)
@data = <>; # read data
foreach (@data) {
(\$name,\$score) = split; # get score
\$score = \$score; # record it
}

I guess \$_  has the instantaneous data from @data.  Am I correct?

2)

@pairs = map {
(\$name, \$score) = split;
[ \$_, \$score ];
} @data;
What is \$_ in the ablove code?
Randal says "I  build a two-element anonymous list from the
\$score and the  original value \$_". Can somebody help me to understand
what he means?

3)
print
map { \$_->[0] }
sort { \$a->[1] <=> \$b->[1] }
map { [\$_, (split)[1] ] }
<>;

regards,
jis

## Re: help on Schwartzian method

Yes. \$_ is the default variable for many Perl operations if an explicit
variable is not present.

\$_ is aliased to each element of @data in turn within the block of the
map statement.

The construct [ \$_, \$score ] creates a list of two elements and returns
a reference to that list. The list is "anonymous" because it is not
assigned to a named variable, e.g. such as the variable @list in "my
@list = (\$_,\$score);". There will be one such list for each element of
@data. The references to all of these anonymous lists will be stored in
the array @pairs.

\$_ is a scalar variable that contains a reference to a list. \$_->[0] is
the first element of that list. It is equivalent to \$[0], but

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

## Schwartzian method

Hi all,

I was reading through  UR coloumn by Randal in ww.stonehenge.com. I
cam across the Schwartzian method which I find extremely difficult to
understand the concepts.

1)
@data = <>; # read data
foreach (@data) {
(\$name,\$score) = split; # get score
\$score = \$score; # record it
}

I guess \$_  has the instantaneous data from @data.  Am I correct?

2)

@pairs = map {
(\$name, \$score) = split;
[ \$_, \$score ];
} @data;
What is \$_ in the ablove code?
Randal says "I  build a two-element anonymous list from the
\$score and the  original value \$_". Can somebody help me to understand
what he means?

3)
print
map { \$_->[0] }
sort { \$a->[1] <=> \$b->[1] }
map { [\$_, (split)[1] ] }
<>;

regards,
jis

## Re: Schwartzian method

There's really no reason to post three copies of the same message.
One is sufficient.

Randal wrote that?!  Shame!!

explicitly given a named variable to use to iterate over the list, the
\$_ variable is used automatically.

See also: `perldoc -f map`.  The above executes the code within the
block once for each element of @data.  The results of each execution
are added to the @pairs array.  For each iteration of this execution,
it puts the current value of @data into \$_, very much the same way
that the foreach loop above operates.

For each element of @data, he is splitting that element on the
whitespace.  The results of that split are put into \$name and \$score.
That's the first line of the map block.  The second line is an
anonymous array reference.  The first element of this anonymous array
is \$_ - which is the current element of @data.  The second element of
the anonymous array is the value of \$score that was found for this
iteration.  That anonymous array reference is then returned from the
block (all Perl blocks return the last value evaluated) and is
therefore added to the @pairs array.

So if @data started out as:
("Paul 100", "John 30", "Mary 15")
then @pairs would be:
( [ "Paul 100", 100], ["John 30", 30"], ["Mary 15", 15] )
That is, @pairs is a transformation of @data, where each element of
@data is now represented as a two element array.

You need to read up on references.  I suggest `perldoc perlreftut` as
a good starting point.  This code takes what is read from <> (which
was represented by @data earlier), maps it to give us our list of
array references (@pairs earlier), then sorts @pairs, and then passes
that sorted list of array references back to map.

Like before, the \$_ in the map block represents each subsequent
element of the list that it's iterating over.  In this case, each of
those elements is a reference to an anonymous array (because that's
what the bottom map created).  \$_->[0] is the first element of the
array that \$_ references.  That is, it is the original line from <>.

Hope this helps,
Paul Lalli