Click here to get back home

help on Schwartzian method

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
help on Schwartzian method jis 06-12-2007
Posted by jis on June 12, 2007, 12:29 pm
Please log in for more thread options


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 even after the explanation given by him.Please help me
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] ] }
<>;
What is $_->[0] all about?


regards,
jis


Posted by Jim Gibson on June 12, 2007, 3:26 pm
Please log in for more thread options



> 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 even after the explanation given by him.Please help me
> 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?

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

>
> 2)
>
> @pairs = map {
> ($name, $score) = split;
> [ $_, $score ];
> } @data;
> What is $_ in the ablove code?

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

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

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.

>
>
> 3)
> print
> map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [$_, (split)[1] ] }
> <>;
> What is $_->[0] all about?

$_ 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
perhaps a little more readable.

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

Similar ThreadsPosted
Help on Schwartzian method June 12, 2007, 12:15 pm
Schwartzian method June 12, 2007, 12:30 pm
Which Net::FTP method for this?" November 7, 2005, 1:36 pm
get method. February 24, 2007, 7:18 am
Is a hash the best method to do this? December 24, 2007, 1:23 pm
Is there any method/way to use API(Windows or Linux) November 30, 2006, 7:26 am
field method for Win32::IE::Mechanize December 15, 2004, 5:39 pm
Credentials and POST method with IO::Socket::SSL? June 30, 2006, 5:14 pm
Can't locate object method "ymd" via package... August 3, 2007, 6:14 am
Pg.pm Question - Can't locate object method driver October 31, 2004, 9:17 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap