[18949] in Perl-Users-Digest
Perl-Users Digest, Issue: 1144 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 16 09:05:38 2001
Date: Sat, 16 Jun 2001 06:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <992696707-v10-i1144@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Sat, 16 Jun 2001 Volume: 10 Number: 1144
Today's topics:
Re: Design pattern for a generic functions dispatcher <goldbb2@earthlink.net>
Re: Help please with sourcing an environment variable <goldbb2@earthlink.net>
Help? (urgent!!!) <andrew@rwts.com.au>
Re: How to pass parameter between two cgi programs??? (isterin)
Re: How to pass parameter between two cgi programs??? <pne-news-20010616@newton.digitalspace.net>
Re: HTML input box variables? <flavell@mail.cern.ch>
Re: looping through text to get info <goldbb2@earthlink.net>
Re: Newbie Post : Flushing output for long scripts (frater mus)
Re: Newbie Post : Flushing output for long scripts (frater mus)
Re: Perl question <goldbb2@earthlink.net>
picture pushing <miriam@allfineantz.nl>
Re: Porblem: Predesignated Runtime? <pne-news-20010616@newton.digitalspace.net>
Re: Q: How to make a Perlscript Shareware? <pne-news-20010616@newton.digitalspace.net>
Re: quick array question <abe@ztreet.demon.nl>
Re: replace with logic in Perl <pne-news-20010616@newton.digitalspace.net>
Sorting hash (Ron Anderson)
Re: Sorting hash (Sam Holden)
Re: Sorting hash (Sam Holden)
Re: Sorting hash <m.grimshaw@salford.ac.uk>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 16 Jun 2001 01:35:36 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Design pattern for a generic functions dispatcher
Message-Id: <3B2AF028.E2703ABF@earthlink.net>
Bart Lateur wrote:
>
> Benjamin Goldberg wrote:
>
> >> > ${$subname}(); # symref call
> >
> >This should be: &{$subname}(); # symref call
>
> Or
> $subname->();
>
> Looks neater. Works, apparently, both with sub names and with proper
> sub refs.
Yes, but I like to make a visible distinction between things which I
expect to be symref calls and code ref calls.
To make this distinction, I put something which I expect to be a string
which is the name of a sub in a call like this:
&{$subname}();
And something which I expect to be a code reference in a call like this:
$coderef->();
The only time I call a coderef with & is when I want the magic passing
of @_ from the caller's context... and I comment it when I do so to make
that clear.
&$coderef; # pass current @_ to coderef.
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 16 Jun 2001 01:42:25 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Help please with sourcing an environment variable
Message-Id: <3B2AF1C1.AF41B37D@earthlink.net>
John W. Krahn wrote:
>
> Benjamin Goldberg wrote:
> >
> > Abigail wrote:
> > >
> > > Simon Barnard (sbarnard@dial.pipex.com) wrote:
> > > &&
> > > && $ENV{DB_USER} = `. <path to shell script>; echo $DB_USER`;
> > > && $ENV{DB_PASSWD} = `. <path to shell script>; echo $DB_PASSWD`;
> > >
> > > $DB_USER and $DB_PASSWD are interpolated by Perl before the line is
> > > passed to the shell. You would need to escape the $ signs with a
> > > backslash.
> >
> > Or else use qx and '
>
> And use chomp correctly.
>
> > chomp $ENV{DB_USER} = qx'. <path to shell script>; echo $DB_USER ';
> > chomp $ENV{DB_PASSWD} = qx'. <path to shell script>; echo $DB_PASSWD';
>
> chomp( $ENV{DB_USER} = qx'. <path to shell script>; echo $DB_USER ' );
> chomp( $ENV{DB_PASSWD} = qx'. <path to shell script>; echo $DB_PASSWD' );
>
> > Of course, it would be better to do both assignments at once:
> > chomp @ENV{DB_USER,DB_PASSWD} = qx'
> > . <path to shell script>
> > echo $DB_USER
> > echo $DB_PASSWD';
>
> chomp( @ENV{DB_USER,DB_PASSWD} = qx'
> . <path to shell script>;
> echo $DB_USER;
> echo $DB_PASSWD' );
This seems to be an instance of perl's binding being un-DWIMish. Why
doesn't the assignment have a higher precedence than the chomp?
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 16 Jun 2001 08:05:21 GMT
From: Andrew Yager <andrew@rwts.com.au>
Subject: Help? (urgent!!!)
Message-Id: <B7515060.25D%andrew@rwts.com.au>
HELP!
I installed Perl 5.6.1 on my system (I had Perl 5.005_3 before).
Unfortunately, there was some stuff which didn't work with 5.6.1, so I have
to go back to 5.005_3
... It worked for a while, but then I remade Perl 5.005_3
... Bad move... Now it doesn't work again!
When I get to the point
make test
It errors at anydbm in test 12 of 12 .
I didn't notice this the first time, so I have a broken Perl build by the
looks of things... What can I do???
... I don't know that much so any very clear and precise step by step
instructions, or even vague pointers would be very much appreciated!!!
Thanks in advance,
Andrew
------------------------------
Date: 15 Jun 2001 22:34:46 -0700
From: isterin@hotmail.com (isterin)
Subject: Re: How to pass parameter between two cgi programs???
Message-Id: <db67a7f3.0106152134.61ac676a@posting.google.com>
Have a nice day! <stingchung@kimo.com.tw> wrote in message news:<mhglit8qophpjajpn42codnrf6alo6brpv@4ax.com>...
> If these two cgi programs locate in two different servers and use two
> different databases.
> and one of the parameters is password....
>
Your best bet would be to read the CGI documentation. There are many
ways of doing many things. QUERY_STRING through post or get methods
are the most common ways.
Ilya
> in that case, What solutions can I use???
>
> thanks again!!
> On Fri, 15 Jun 2001 17:55:17 -0000, Chris Stith
> <mischief@velma.motion.net> wrote:
>
> >Have a nice day! <stingchung@kimo.com.tw> wrote:
> >> How can I pass parameter from a cgi program to another cgi program???
> >
> >1) as a cookie
> >2) as a hidden form field
> >3) in a temporary file keyed by some other info
> >4) by using a session management module
> >5) in a database, keyed by some other info
> >6) search for 'session' and 'cgi' on CPAN and Google Groups, and
> > read up
> >
> >Chris
------------------------------
Date: Sat, 16 Jun 2001 08:33:29 +0200
From: Philip Newton <pne-news-20010616@newton.digitalspace.net>
Subject: Re: How to pass parameter between two cgi programs???
Message-Id: <a2qlit0m3pmjp08d4c5gloulric543h5em@4ax.com>
[Please don't top-post.]
On Sat, 16 Jun 2001 10:27:40 +0800, Have a nice day!
<stingchung@kimo.com.tw> wrote:
> If these two cgi programs locate in two different servers and use two
> different databases.
Then you can still use "2) as a hidden form field". And if the two
different servers are in the same domain, "1) as a cookie" will probably
also work.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sat, 16 Jun 2001 14:00:56 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: HTML input box variables?
Message-Id: <Pine.LNX.4.30.0106161342360.12672-100000@lxplus003.cern.ch>
On Sat, 16 Jun 2001, Stefan Weiss wrote:
> > > > #!/usr/bin/perl
> > >
> > > Bang!
> Hm, difficult say *after* the detonation, but judging from the
> placement of the explosive, my best guess is that AJF was missing
> a -T switch in your script.
It wasn't my first reaction; but sure, CGI scripting is sufficiently
fraught that I would want to use every bit of help that the language
offers...
> I don't see any reason to turn taint
> mode on in this script though.
Put it this way. I'd want to see a good reason for _not_ turning it
on, before I would agree to not use it - at least at the testing
stage[1].
> Maybe he thought you should have
> used strict and warnings?
I'd say that is a sine qua non for any answer on this group, and I
reckon the regulars would back me up on that...
> Or maybe he didn't like [...]
Maybe he thought that an appropriate module (such as CGI.pm or
friends) would be a cleaner and more reliable answer? And then we
wouldn't need to get bogged down in details such as why was the string
not properly HTML-encoded before using it as an HTML attribute value?
And what the heck was the point of sending an HTTP header with a
content-type of text/html and without a charset, and then smuggling
the charset into a META HTTP-EQUIV? We don't need no steenking
HTTP-EQUIV substitutes, when we are in a position to produce _real_
HTTP headers. But that area would be better covered on a group which
had WWW and CGI in its name. The Perl-language specific part would,
as I say, seem to be be warnings, strict and probably taint. This
answer fell at that first hurdle.
[1]There are diverging views on whether it's appropriate to leave
debugging features in a script once it's been developed and is going
into production. If the debugging output is liable to be revealed to
an attacker, then it could represent a security exposure, helping them
to understand how the script works and to identify a point of weakness
where they can break-in to it. If you're careful to only let that
stuff go into an error log, then it _could_ help you to find bugs that
hadn't been anticipated at the testing stage.
------------------------------
Date: Sat, 16 Jun 2001 02:53:39 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: looping through text to get info
Message-Id: <3B2B0273.B272F2AF@earthlink.net>
my $dbcc_consistency = do {
open my $fh, "/path/to/command -args" or die ...;
my $val = <$fh>;
close $fh or die sprintf "%s %04x", $!, $?;
$val };
study $dbcc_consistency;
while( $dbcc_consistency =~ m[\G
^Reporting.configuration.information.of.database (.*)\.\n
\n
.Parameter.Name\ * Value\ * Size\n
\n
\ database.name\ * \1\ * (\d+)K\n
\ dbcc.named.cache\ * (.*)\ * (\d+)K\n
\ scan.workspace\ * (.*)\ * (\d+)K\n
\ text.workspace\ * (.*)\ * (\d+)K\n
\ OAM.count.threshold\ * (\d+)%\n
\ IO.error.abort\ * (\d+)\n
\ linkage.error.abort\ * (\d+)\n
\ max.worker.processes\ * (\d+)\n
\ operation.sequence.number\ * (\d+)\n
\(return status\ =\ (\d+)\)\n
(.*)\n
DBCC.execution.completed..If.DBCC.printed.error.messages,.
contact.a.user.with.System.Administrator.\(SA\).role.\n
Checking.\1\n
Storage.checks.for.'\1'.are.complete..DBCC.is.now.recording.
the.results.in.the.dbccdb.database.\n
DBCC.CHECKSTORAGE.for.database.'\1'.sequence.(\d+).completed.
at.(.*)..(\d+).faults.and.(\d+).suspect.conditions.
were.located..(\d+).checks.were.aborted..You.should.
investigate.the.recorded.faults,.and.plan.a.course.of.
action.that.will.correct.them.\n
Verifying faults for '\1'.
DBCC CHECKVERIFY.for.database.'\1'.sequence.(\d+).completed.
at.(.*)..(\d+).suspect.conditions.were.resolved.as.
faults,.and.(\d+).suspect.conditions.were.resolved.as.
harmless..(\d+).objects.could.not.be.checked.\n
DBCC.execution.completed..If.DBCC.printed.error.messages,.
contact.a.user.with.System.Administrator.\(SA\).role.\n
(.*)\n
\n
Database\ Name\ :\ \1\n
\n
Table.Name\ +Index\ +Type.Code\ +Description\ +Page\ Number\n
-{30}. -{6}. -{11}. -{50}. -{11} \n
((: .{31} .{7} .{12} .{51} .{11}\n :)*)
\n
\(return.status\ =\ (\d+)\)\n
Generating.'Fault.Report'.for.object.(.*).in.database.\1.\n
\n
((: Type.Code:.*\n
Page.format.*\n
page.id:.*\n
page.header:.*\n
Header:.*\n
.time.*\n
.free.*\n
\n :)*)
]xgm )
{
# do stuff with $1 .. $31
}
if( pos($dbcc_consistency) != length($dbcc_consistency) ) {
die "Unparsed text in consistency check, starting at ".
"character " . pos( $dbcc_consistency );
}
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 16 Jun 2001 07:20:20 GMT
From: mouse.news@mousetrap.net (frater mus)
Subject: Re: Newbie Post : Flushing output for long scripts
Message-Id: <Xns90C21809A6747ssbmousetrapnet@151.164.30.58>
15 Jun 2001: redsquirreldesign@yahoo.com (Dave Hoover) wrote
> but I think it will do what
> you're looking for. I couldn't think of a way to do it in Perl, but
> I'd bet there's some way to do it...probably more than one!
Unbuffer the output, have the script make its announcement about the delay,
/then/ start the lengthy crunching. Works fine.
I suppose if it was going to take a long while and there were many users, it
might be easier on the server to email a results URL upon completion (or
something) instead of keeping a million waiting connections running...
--
L.V.X., frater mus
Old Time Radio (OTR) OpenNap network http://www.mousetrap.net/OTRnap/
the Great Work, CDR, OTR, scanning http://www.mousetrap.net/~mouse/
RioVolt mini-review http://www.mousetrap.net/~mouse/otr/riovolt.html
------------------------------
Date: Sat, 16 Jun 2001 07:23:52 GMT
From: mouse.news@mousetrap.net (frater mus)
Subject: Re: Newbie Post : Flushing output for long scripts
Message-Id: <Xns90C218A381AF4ssbmousetrapnet@151.164.30.58>
[posted and mailed]
31 May 2001: "Ben Holness" <bholness@nortelnetworks.com> wrote
> Just a few words at the top of the page that say "Script is running,
> please be patient" would be fine.
>
> I put this in a print statement, but that doesn't seem to get to the
> browser until the whole script is finished, or the user presses stop.
Try out the $| (pipe-symbol) special variable. Should allow the output to
"stream out" like you want.
$| = 1; #verily, let it flow like a mighty river
--
L.V.X., frater mus
Old Time Radio (OTR) OpenNap network http://www.mousetrap.net/OTRnap/
the Great Work, CDR, OTR, scanning http://www.mousetrap.net/~mouse/
RioVolt mini-review http://www.mousetrap.net/~mouse/otr/riovolt.html
------------------------------
Date: Sat, 16 Jun 2001 03:14:49 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Perl question
Message-Id: <3B2B0769.1F21F2EB@earthlink.net>
Eric Sokolowsky wrote:
[snip]
> The parser used by this script returns a hierarchical structure, so
> that I can directly access the first file format in the following way:
>
> $form = $document{'WMT_MS_Capabilities'}{'Capability'}{'Request'}
> {'GetMap'}{'_Format'}[0]{'CDATA'};
[snip]
> How do I loop through the different CDATA portions
> of the Format tags?
my $formats = $document{WMT_MS_Capabilities}{Capability}
{Request}{GetMap}{_Format};
foreach( map { $_->{CDATA} } @$formats ) {
# do stuff with $_
}
--
The longer a man is wrong, the surer he is that he's right.
------------------------------
Date: Sat, 16 Jun 2001 12:18:42 +0200
From: "Yo" <miriam@allfineantz.nl>
Subject: picture pushing
Message-Id: <%lGW6.90045$qM3.11130964@news.soneraplaza.nl>
Hi group,
Is it possible to make a script that pushes a picture to the visitor's
browser, without the surrounding html?
following url would do the trick: http://wwwdomain.com/test.jpg for instance
but the url needs to be HIDDEN!!!
not even there!!
------------------------------
Date: Sat, 16 Jun 2001 08:33:29 +0200
From: Philip Newton <pne-news-20010616@newton.digitalspace.net>
Subject: Re: Porblem: Predesignated Runtime?
Message-Id: <obqlito17go8v4fct2bvn7oj4q6euo194b@4ax.com>
On 15 Jun 2001 17:05:43 -0700, tangfan01@hotmail.com (Kyle Vinson)
wrote:
> Even having cron do something that the script can then detect would
> require it to constantly check this time, right?
Not necessarily. You could try using signals, for example -- have cron
send, say, SIGUSR1 to your script and install a handler for $SIG{'USR1'}
that sets a flag.
If your main "loop" is a sleep, then the signal should interrupt the
sleep. Then check the flag, and if it is set, make your announcement.
Then reset the flag.
> The way I have it set now, it looks for IRC messages occuring within a
> certain timeframe (IE, it checks time whenever there's a message, and
> announces when it sees a message in the times I've set), but this is
> clunky and unreliable at best, along with annoying, as it will often
> blither at every message that falls within this time frame (normally a
> minute long), which causes the Channel OPs to punt the bot.
Again, set a flag after you have made your announcement once. If the
flag is already set, don't make the announcement again.
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sat, 16 Jun 2001 06:56:23 +0200
From: Philip Newton <pne-news-20010616@newton.digitalspace.net>
Subject: Re: Q: How to make a Perlscript Shareware?
Message-Id: <gdplits1aq4cm92tg2cskbhto81kl0ls13@4ax.com>
On Fri, 15 Jun 2001 20:22:03 +0200, "Tom Klinger" <admin@the-piper.net>
wrote:
> how to write such a piece of code which is first protecting your script
> with an expiration code
This is probably the easier bit. For example, you could encrypt the
expiration date and compare the current date against the decrypted date,
or calculate a hash value on the file containing the expiration date to
ensure it hasn't been tampered with.
> and second hide it in such a way that it will be worth to buy it than
> rather try to onvest a couple of times and remove that pieces of code.
And this is probably the harder bit. Most likely easier to put a "good"
(read: legally valid) software license on your code and trust the
customer.
> I still try to get some examplecodes but my search is, unfortunatly, not
> very successful.
So hire a programmer :)
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Sat, 16 Jun 2001 13:04:02 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: quick array question
Message-Id: <96fmitsfml6lbkc7jibuusgt0i4qp1fdhp@4ax.com>
On Fri, 15 Jun 2001 16:21:12 GMT, echang@netstorm.net (E.Chang) wrote:
> Martin Bower <Martin.Bower@ib.bankgesellschaft.de> wrote in
> <3B2A2F35.6C28A57@ib.bankgesellschaft.de>:
>
> > I have an array @rows , and each row contains a number of "~"
> > seperated fields.
> > e.g "first~second~name~end" , "first~last"
> >
> > is there a quick way to tell the highest number of fields for the
> > entire array ?
>
> Here's one way - simple though not necessarily the quickest.
#!/usr/bin/perl -wl
use strict;
> @rows = ("A~B~C~D", "A~B~C~D~E", "A~B");
my @rows = ("A~B~C~D", "A~B~C~D~E", "A~B");
> my $fields;
my $fields = 0; # to avoid warning
> foreach (@rows) {
> $fields = $tildes if (my $tildes = tr/~/~/) > $fields;
> }
> print $fields+1;
Global symbol "$tildes" requires explicit package name at try.pl line 7.
The if-statement modifier can't 'create' a lexical for the statement
itself. You'll have to declare it before the statement + modifier.
foreach (@rows) {
my $tildes;
$fields = $tildes if ($tildes = tr/~/~/) > $fields;
}
> Output is 5.
Nope, the output (on my machine) is: 1
--
Good luck, Abe
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -we '$_="rekcah lreP rehtona tsuJ";print$2while s/(.*)(.)/$1/g'
------------------------------
Date: Sat, 16 Jun 2001 08:33:28 +0200
From: Philip Newton <pne-news-20010616@newton.digitalspace.net>
Subject: Re: replace with logic in Perl
Message-Id: <9tplitoj0kl5kn5m3b43m92uh5sisrgu79@4ax.com>
On Fri, 15 Jun 2001 16:06:50 GMT, echang@netstorm.net (E.Chang) wrote:
> balpaudl@iohk.com (Bal Paudyal) wrote in
> <82c9c59d.0106150732.3d0ab8e3@posting.google.com>:
>
> > I would like to replace all non word with a space except @ and dot.
>
> make a character class that negates the characters you want.
>
> s/[^\w.@]/ /g;
Or use tr (though then you no longer have access to \w):
tr/a-zA-Z0-9@./ /c;
(add an underscore if you consider that a "word" character; \w does but
you may not here.)
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: 16 Jun 2001 00:36:11 -0700
From: ronlanderson@earthlink.net (Ron Anderson)
Subject: Sorting hash
Message-Id: <e86e87fc.0106152336.6e9973d1@posting.google.com>
Using the sample hash below:
$shash{"student1"} = join("\t", ("bob", "larson", "room5"));
$shash{"student2"} = join("\t", ("ron", "anderson", "room4"));
$shash{"student3"} = join("\t", ("dave", "lee", "room2"));
$shash{"student4"} = join("\t", ("tim", "barker", "room3"));
$shash{"student5"} = join("\t", ("roger", "farley", "room1"));
how could I sort this %shash by last name?
Thanks!
------------------------------
Date: 16 Jun 2001 07:54:41 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Sorting hash
Message-Id: <slrn9im461.mdt.sholden@pgrad.cs.usyd.edu.au>
On 16 Jun 2001 00:36:11 -0700, Ron Anderson <ronlanderson@earthlink.net> wrote:
>Using the sample hash below:
>
>$shash{"student1"} = join("\t", ("bob", "larson", "room5"));
>$shash{"student2"} = join("\t", ("ron", "anderson", "room4"));
>$shash{"student3"} = join("\t", ("dave", "lee", "room2"));
>$shash{"student4"} = join("\t", ("tim", "barker", "room3"));
>$shash{"student5"} = join("\t", ("roger", "farley", "room1"));
>
>how could I sort this %shash by last name?
perlfaq4:
How do I sort a hash (optionally by value instead of key)?
Go and read it. It's on the web, and on the computer you run perl code on.
Once you've red it read on (if you wish)...
You'd be much better off with something like:
$shash{"student1"} = {fname => 'bob', lname => 'larson', room => 'room5'};
And with keys like student1, student2, why not just use an array? Something
like:
@students = ( {fname => 'bob', lname => 'larson', room => 'room5'},
{fname => 'ron', lname => 'anderson', room => 'room4'},
{fname => 'dave', lname => 'lee', room => 'room2'},
# and so on...
);
@sorted = sort {$a->{lname} cmp $b->{lname}} @students;
--
Sam
"... the whole documentation is not unreasonably transportable in a
student's briefcase." - John Lions describing UNIX 6th Edition
"This has since been fixed in recent versions." - Kernighan & Pike
------------------------------
Date: 16 Jun 2001 07:56:11 GMT
From: sholden@pgrad.cs.usyd.edu.au (Sam Holden)
Subject: Re: Sorting hash
Message-Id: <slrn9im48r.mec.sholden@pgrad.cs.usyd.edu.au>
On 16 Jun 2001 07:54:41 GMT, Sam Holden <sholden@pgrad.cs.usyd.edu.au> wrote:
>
>Once you've red it read on (if you wish)...
My mangling of the English language sometimes amazes even me...
--
Sam
i am not grouchy. i have a personality deficiency.
-- Uri Guttman in <x7k8tshmn9.fsf@home.sysarch.com>
------------------------------
Date: Sat, 16 Jun 2001 12:12:20 +0100
From: Mark Grimshaw <m.grimshaw@salford.ac.uk>
Subject: Re: Sorting hash
Message-Id: <3B2B3F14.B6E9CFF9@salford.ac.uk>
Ron Anderson wrote:
>
> Using the sample hash below:
>
> $shash{"student1"} = join("\t", ("bob", "larson", "room5"));
> $shash{"student2"} = join("\t", ("ron", "anderson", "room4"));
> $shash{"student3"} = join("\t", ("dave", "lee", "room2"));
> $shash{"student4"} = join("\t", ("tim", "barker", "room3"));
> $shash{"student5"} = join("\t", ("roger", "farley", "room1"));
>
> how could I sort this %shash by last name?
>
> Thanks!
I asked the same thing a few weeks back (how to sort a hash on _part_ of
its value) and was given the following by some kind soul which is very
efficient compared to the mangled code I had before (I was sorting on
numbers so I've changed the <=> to cmp):
@names = sort {(split(/¬/, $shash{$b}))[1] cmp (split(/¬/,
$shash{$a}))[1] } keys %shash;
my delimiter was '¬' - you'd need to change the delimiter in the split
above to what you have.
------------------------------
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.
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 V10 Issue 1144
***************************************