[29251] in Perl-Users-Digest
Perl-Users Digest, Issue: 495 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jun 8 09:14:22 2007
Date: Fri, 8 Jun 2007 06:14:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 8 Jun 2007 Volume: 11 Number: 495
Today's topics:
Re: Sorting keith@bytebrothers.co.uk
Re: Sorting keith@bytebrothers.co.uk
Re: Sorting <mritty@gmail.com>
Re: Sorting keith@bytebrothers.co.uk
Re: Sorting <mritty@gmail.com>
Re: urgent:reading 2 files into 2 different $ variables <joe@inwap.com>
Re: Yet Another Software Challenge <me@privacy.net>
Re: Yet Another Software Challenge <rjh@see.sig.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 08 Jun 2007 01:28:20 -0700
From: keith@bytebrothers.co.uk
Subject: Re: Sorting
Message-Id: <1181291300.480233.204340@k79g2000hse.googlegroups.com>
On 7 Jun, 16:46, Paul Lalli <mri...@gmail.com> wrote:
> On Jun 7, 11:27 am, k...@bytebrothers.co.uk wrote:
> > Any advice on ways to tighten this up a tad without losing too much readability?
>
> rather than messing with a bunch of indices, I would prefer a
> Schwartzian transform. The syntax has a bit of a learning curve, but
> once you "get it", it becomes intuitive.
>
> print map { $_->[0] }
> sort { $a->[1] cmp $b->[1] }
> map { [ $_, (split /\|/)[3] ] }
> @lines;
Oh, that's sweet! All I need to do now is sit down and work out
exactly how the feck that works!
------------------------------
Date: Fri, 08 Jun 2007 02:59:57 -0700
From: keith@bytebrothers.co.uk
Subject: Re: Sorting
Message-Id: <1181296797.765864.111780@q75g2000hsh.googlegroups.com>
On 8 Jun, 09:28, k...@bytebrothers.co.uk wrote:
> On 7 Jun, 16:46, Paul Lalli <mri...@gmail.com> wrote:
>
> > On Jun 7, 11:27 am, k...@bytebrothers.co.uk wrote:
> > > Any advice on ways to tighten this up a tad without losing too much readability?
>
> > rather than messing with a bunch of indices, I would prefer a
> > Schwartzian transform. The syntax has a bit of a learning curve, but
> > once you "get it", it becomes intuitive.
>
> > print map { $_->[0] }
> > sort { $a->[1] cmp $b->[1] }
> > map { [ $_, (split /\|/)[3] ] }
> > @lines;
>
> Oh, that's sweet! All I need to do now is sit down and work out
> exactly how the feck that works!
I've been working through this, and I think I'm getting there, slowly;
there's something going on here with anonymous list references, for a
start. But how would I use this paradigm if there was a more
complicated key? For example, in my original example, if I needed to
sort by the second column, which contains a date, I would have done
something like:
@fields = split(/\|/);
($dy,$mn,$yr) = split(/\//,$field[1]);
push @key, "$yr$mn$dy";
etc...
How would this transform approach allow me to do something similar?
------------------------------
Date: Fri, 08 Jun 2007 10:32:43 -0000
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Sorting
Message-Id: <1181298763.762176.165760@q75g2000hsh.googlegroups.com>
On Jun 8, 5:59 am, k...@bytebrothers.co.uk wrote:
> > On 7 Jun, 16:46, Paul Lalli <mri...@gmail.com> wrote:
> > > print map { $_->[0] }
> > > sort { $a->[1] cmp $b->[1] }
> > > map { [ $_, (split /\|/)[3] ] }
> > > @lines;
>
> But how would I use this paradigm if there was a more
> complicated key? For example, in my original example, if I
> needed to sort by the second column, which contains a date, I
> would have done something like:
>
> @fields = split(/\|/);
> ($dy,$mn,$yr) = split(/\//,$field[1]);
> push @key, "$yr$mn$dy";
> etc...
>
> How would this transform approach allow me to do something similar?
Well, obviously, it's going to be a little messier, but the concept is
the same;
print map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [
$_,
do {
my ($d,$m,$y) = split '/', (split /\|/)[1];
"$y$m$d";
}
]
}
@lines;
When trying to decipher a Schwartzian transform, read it backwards.
1) We start with the array of @lines.
2) The bottom map transform the array of lines into a list of array
references. The first element of the array reference is the line
itself, and the second is the value we want to sort by eventually. In
this case, that's the "year-month-day" value.
3) The sort now takes this list of array references, and sorts it by
the second element of each referenced array. That is, it sorts the
array references on our sort key.
4) The top map takes this sorted list of array references and
transforms it to a new list containing the first element of each
referenced array - that is, the original line.
5) print is passed this list of lines.
It might be helpful if you break it out into it's individual steps.
In this case, I'll use a generic get_key() to represent obtaining the
sort key from your line. That's the only part of a Schwartzian
transform that ever changes. The syntax is always the same for the
rest of it.
my @lines_keys = map { [ $_, get_key($_) ] } @lines;
my @sorted_lines_keys = sort { $a->[1] cmp $b->[1] } @lines_keys;
my @sorted_lines = map { $_->[0] } @sorted_lines_keys;
print @sorted_lines;
Hope that helps,
Paul Lalli
------------------------------
Date: Fri, 08 Jun 2007 03:46:02 -0700
From: keith@bytebrothers.co.uk
Subject: Re: Sorting
Message-Id: <1181299562.975031.144490@e65g2000hsc.googlegroups.com>
On 8 Jun, 11:32, Paul Lalli <mri...@gmail.com> wrote:
> When trying to decipher a Schwartzian transform, read it backwards.
> 1) We start with the array of @lines.
> 2) The bottom map transform the array of lines into a list of array
> references. The first element of the array reference is the line
> itself, and the second is the value we want to sort by eventually. In
> this case, that's the "year-month-day" value.
> 3) The sort now takes this list of array references, and sorts it by
> the second element of each referenced array. That is, it sorts the
> array references on our sort key.
> 4) The top map takes this sorted list of array references and
> transforms it to a new list containing the first element of each
> referenced array - that is, the original line.
> 5) print is passed this list of lines.
I think I just had a religious experience. That is new and wonderful,
and thank you for explaining it for me!
------------------------------
Date: Fri, 08 Jun 2007 05:33:29 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: Sorting
Message-Id: <1181306009.912091.55150@e65g2000hsc.googlegroups.com>
On Jun 8, 6:46 am, k...@bytebrothers.co.uk wrote:
> On 8 Jun, 11:32, Paul Lalli <mri...@gmail.com> wrote:
> > [description of Schwartzian Transform]
> I think I just had a religious experience. That is new and
> wonderful, and thank you for explaining it for me!
You're welcome. Glad to help.
I would be remiss, however, if I didn't point out that Uri has created
a module which generalizes the creation of a Schwartzian Transform
sort algorithm (amongst other things). It is available on the CPAN,
named Sort::Maker. Using that module, the process becomes:
use Sort::Maker
my $sorter = make_sorter('ST', string => \&get_key);
print $sorter->(@lines);
#get_key simply extracts the key from your data
#so in the second example, it would be:
sub get_key {
my $date = (split /\|/, $_)[1];
my ($d, $m, $y) = split '/', $date;
"$y$m$d";
}
#in the original, it would be as simple as:
sub get_key {
(split /\|/)[3];
}
Paul Lalli
------------------------------
Date: Thu, 07 Jun 2007 23:05:34 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: urgent:reading 2 files into 2 different $ variables
Message-Id: <A8udnc7IlYMubPXbnZ2dnUVZ_gWdnZ2d@comcast.com>
geot wrote:
> On Jun 4, 10:00 pm, geot <george.ti...@gmail.com> wrote:
>
> pls need help
This is not the place to demand urgent help.
Responses can take weeks to show up.
------------------------------
Date: Fri, 8 Jun 2007 09:10:59 +0000 (UTC)
From: Lionel B <me@privacy.net>
Subject: Re: Yet Another Software Challenge
Message-Id: <f4b6f3$seg$1@south.jnrs.ja.net>
On Thu, 07 Jun 2007 03:29:40 -0700, yanosc wrote:
> If you're mad about software riddles, just have a look at
> http://software.challenge.googlepages.com and challenge yourself or
> invite your friends to join.
That site appears to be badly broken. Tried it in Firefox and Opera and
the only links (appart from the "Relevant Link" section) appear to lead
to jpegs.
--
Lionel B
------------------------------
Date: Fri, 08 Jun 2007 10:51:25 +0000
From: Richard Heathfield <rjh@see.sig.invalid>
Subject: Re: Yet Another Software Challenge
Message-Id: <3_KdnZpzT7yyqfTbnZ2dnUVZ8sSrnZ2d@bt.com>
Keith Thompson said:
> yanosc@gmail.com writes:
>> On Jun 7, 5:37 pm, "Jürgen Exner" <jurge...@hotmail.com> wrote:
>>> yan...@gmail.com wrote:
>>> > If you're mad about software riddles, just have a look at
>>> >http://software.challenge.googlepages.comand challenge yourself or
>>> > invite your friends to join.
>>>
>>> I suppose the first riddle is to find out how to use that web page.
>>> In short: poor design because it gives no indication to the user
>>> what to do next or in this case where to start.
>>
>> You're absolutely wrong! Where to start is clearly shown. Thousands
>> of people have succeed so far..
> [...]
>
> Ok, I think I figured out how to proceed from the first page. (I
> won't post details, just in case anyone actually cares about solving
> it.)
>
> The resulting page shows an image, which I suppose might mean
> something, but I have no idea where I'm supposed to go from there.
>
> Maybe I'm missing something really clever, but the whole thing looks
> boring and frustrating.
>
> Incidentally, your Terms of Use page says, among other things:
>
> By accessing or using this Site in any way, including, without
> limitation, using or downloading any Content, or merely browsing
> the Site, you agree to and are bound by the Terms of Use.
>
> For the record, I do not agree to any such terms.
Likewise. Furthermore, that's an illegal contract, and has no legal
validity. It's okay to say "here are the terms of use - if you don't
like them, go away", but that's not what he's doing. He's opening a
shop on the High Street, waiting for people to come through the door
out of curiosity, and then saying to them "by coming through that door,
you have agreed to the Terms of Use for this shop", which might be
anything - e.g. Para 17) You agree to hand over ALL your money to the
shopkeeper, in return for a small plastic toy of no significant value
whatsoever.
I have no doubt that the OP will be eager to learn from this, and will
change his Terms of Use page forthwith.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 495
**************************************