[7662] in Perl-Users-Digest
Perl-Users Digest, Issue: 1288 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 7 22:13:40 1997
Date: Fri, 7 Nov 97 19:00:25 -0800
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, 7 Nov 1997 Volume: 8 Number: 1288
Today's topics:
Re: An excellent example of perl madness (or "split stu (Paul Colquhoun)
Re: Better Way in Perl <zenin@best.com>
Re: Better Way in Perl <jbokma@caiw.nl>
Re: Chomp vs Chop <markm@nortel.ca>
Re: Chomp vs Chop (Tad McClellan)
Re: Decimal rounding and lining up decimals? <markm@nortel.ca>
Re: Decimal rounding and lining up decimals? (Tad McClellan)
Re: eliminate all but a certain string (Tad McClellan)
Re: File handles and XSUBs (Robert G. Ferrell)
Re: file not found error? (Tad McClellan)
Re: glob not working (Tad McClellan)
HELP - deleting an array element out of a "non-associat <moody@roguewave.com>
Re: HELP - deleting an array element out of a "non-asso <zenin@best.com>
Re: HELP - deleting an array element out of a "non-asso <moody@roguewave.com>
Re: Intricate Sort help needed (Daniel E. Macks)
Re: Intricate Sort help needed (Andrew M. Langmead)
Re: Intricate Sort help needed (Tad McClellan)
Re: is it possible to reference a sub-array? <zenin@best.com>
Re: is it possible to reference a sub-array? <markm@nortel.ca>
Re: One slice of hash, please... <zenin@best.com>
Re: One slice of hash, please... <cmargoli@world.northgrum.com>
Re: One slice of hash, please... (Toutatis)
Re: One slice of hash, please... <markm@nortel.ca>
Re: other language for perl (Robert G. Ferrell)
PERL interpreter in Java? <jjv@netapp-nospam.com>
Perl web page counter troubles (Bruce Seiler)
Re: Regular Expressions (Tad McClellan)
scalar function weird mehta@mama.indstate.edu
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 8 Nov 1997 00:55:18 GMT
From: paulcol+usenet@andor.dropbear.id.au (Paul Colquhoun)
Subject: Re: An excellent example of perl madness (or "split stupidity")
Message-Id: <slrn667e3m.vk8.paulcol+usenet@linux.andor.dropbear.id.au>
On Thu, 06 Nov 1997 17:25:30 +0100, Gary Howland <ghowland@hotlava.com> wrote:
|It's not a bug, it's a feature! There's more than one way to do it!
|
|This example helps to show what a monster of a language perl really is,
|with unexpected (inconsistent?) behaviour not found anywhere else. The
|follow-ups will demonstrate how the perl community think that this is a
|good thing, all part of perls eclectic nature (as if that is some sort
|of virtue), and how features such as this are certainly more important
|than threads or typed exceptions...
|
|Three of the following work as expected. A pint for anyone who can tell
|my which three (without running the code):
As expected by who? Since it would take a mind-reader to tell what *you*
expected them to do, I would expect most people to do rather poorly in
this test.
|print join(", ", split('|', "hello|world")), "\n";
|print join(", ", split("|", "hello|world")), "\n";
|print join(", ", split(/|/, "hello|world")), "\n";
|print join(", ", split(/\|/, "hello|world")), "\n";
|print join(", ", split("\|", "hello|world")), "\n";
|print join(", ", split('\|', "hello|world")), "\n";
|my $sep = "|"; print join(", ", split($sep, "hello|world")), "\n";
|my $sep = "\|"; print join(", ", split($sep, "hello|world")), "\n";
|my $sep = '|'; print join(", ", split($sep, "hello|world")), "\n";
|my $sep = '\|'; print join(", ", split($sep, "hello|world")), "\n";
OK, I'll admit that, being a relative beginner, I didn't predict the actual
behaviour of most of these correctly. But once I had observed the actual
behaviour it wasn't hard to see why it happened.
Little gotchas like this is why I test code before useing it seriously...
--
Reverend Paul Colquhoun, paulcol+sig@andor.dropbear.id.au
Universal Life Church http://andor.dropbear.id.au/~paulcol
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
------------------------------
Date: 7 Nov 1997 23:48:38 GMT
From: Zenin <zenin@best.com>
Subject: Re: Better Way in Perl
Message-Id: <6409cm$omb$4@nntp2.ba.best.com>
Walker Curtis <curtis@ei.kodak.com> wrote:
: The closest competition is a 42 character non-perl runner up.
: ls -1 *.cin|sed 's/\(.*\)\..*/mv & \1/'|sh
FYI, the -1 is the default when used in a pipe, so this
is really only:
ls *.cin|sed 's/\(.*\)\..*/mv & \1/'|sh
Beating you by one char. :-)
--
-Zenin (zenin@best.com)
The Bawdy Caste (San Jose, CA) http://www.netmagic.net/~dmcgrath/bawdy/
Barely Legal (Berzerkly, CA) http://www.barelylegal.org/
Zenin's Rocky Archive (Moving soon!) http://www.best.com/~zenin/
------------------------------
Date: 8 Nov 1997 00:18:43 GMT
From: "John Bokma" <jbokma@caiw.nl>
Subject: Re: Better Way in Perl
Message-Id: <01bcebdb$b7f54140$a80ab2c2@tschai>
Zenin <zenin@best.com> wrote in article <6409cm$omb$4@nntp2.ba.best.com>...
> Walker Curtis <curtis@ei.kodak.com> wrote:
> : The closest competition is a 42 character non-perl runner up.
> : ls -1 *.cin|sed 's/\(.*\)\..*/mv & \1/'|sh
>
> FYI, the -1 is the default when used in a pipe, so this
> is really only:
>
> ls *.cin|sed 's/\(.*\)\..*/mv & \1/'|sh
>
> Beating you by one char. :-)
Three, one space, - and 1!
John
--
----------------------------------------------------------------------------
C A S T L E A M B E R Freelance Software Development
(Java/Perl/C/CGI)
http://www.caiw.nl/~jbokma/ [Java demo's,Perl scripts,Xara
tutorials,Resume]
mail:jbokma@caiw.nl phone: +31 10 4291827
------------------------------
Date: 07 Nov 1997 18:08:02 -0500
From: Mark Mielke <markm@nortel.ca>
Subject: Re: Chomp vs Chop
Message-Id: <lq1k9ek9ub1.fsf@bmers2e5.i-did-not-set--mail-host-address--so-shoot-me>
faust@wwa.com (Faust Gertz) writes:
> On Fri, 07 Nov 1997 12:40:41 -0500, Matt Pezzuto <mpezzuto@link.com>
> wrote:
> >While I was learning perl I learned it was safer to use
> >chomp if your only purpose is to get ride of an eol.
> Did you also learn TIMTOWTDI?
it is safer.
> >I asked my friend why he didn't use chomp. He didn't know about it
> >and asked what it did and I told him.
> Some guru.
funny :-)
> >I failed to come up with an example and he claimed that using chomp was half-hazard
> >way of programming because the programmer should know whether or
> >not an eol is expected to not and chop is quicker than chomp because
> >it doesn't have to check to make sure the last character is an eol.
>
> I don't know if it is 'half-hazard' way of programming. *chop* and
> *chomp* are usually used to remove the newline character from the end
> of a string and are probably almost always more efficient than
> s/\n$//. *chop* removes (and returns) the last character of the
> string passed to it. *chomp* removes the record separator, the value
> of $/, and returns the number of characters removed. So they do
> different things and return different things. It is my feeling that
> *chomp* is probably better to use than *chop* for removing newline
> characters as it will only chop off the last character(s) if equal to
> the value of $/ and thus takes care of some cross-platform issues. On
> Windows it will remove both the return character and the newline
> character instead of just lobbing off the last character. This might
> be the type of example your guru friend was seeking.
> >I replied with then why did they make chomp? I do not totally agree
> >with him but I have to admit that he has a point. Who is right, the
> >newbie or the guru?
> You are both wrong. :-)
They did indeed make chomp() because chop() was ineffective. True programs
that will not crash in wierd places were always doing s/\n$// or similar
constructs. The problem is that that is SLOW.
What about the case where a user could have touched the file? And there
is no newline on the end of the file because their editor (i.e. emacs)
allows such a thing? Same situation presents itself in the following...
I don't know HOW many people always do it this way. I think several books
actually document this as acceptable.
while (<STDIN>) {
}
Hmmm... interesting... what if one of the lines is a sole "0"... good-bye
stable under all conditions script. Another case, argument parsing...
i've seen this many times as well:
while ($_ = shift) {
}
Hmmm... interesting... and what if one of the arguments is a "0"? time to
stop parsing the rest of the options? :-) nice... the proper way all should
be doing the above examples are:
while (defined($_ = <STDIN>)) {
}
and,
while (defined($_ = shift)) {
}
and whenever you KNOW you want to get rid of newlines at the end of a line,
how can you NOT want to be sure? Just assuming your some smartass programmer
and you'll never make a mistake is not good enough. Use techniques that do
not ALLOW for programming error... :-)
while (defined($_ = <myfile>)) {
chomp $_;
# do stuff...
}
using chop() instead of chomp() is pretty near comparable to choosing to
leave out parentheses in complex expressions... like
my($second, $minute, $hour) = localtime();
my $second_of_day = ((($hour * 60) + $minute) * 60) + $second;
What's the big deal? Why do some people find it so necessary to do:
my $second_of_day = ($hour * 60 + $minute) * 60 + $second;
and this is a rather simple example. why make things hard on yourself
just so that you can be a "guru". When i do algebra etc. in school
i put in all the steps, i try not to miss steps. It's too easy to make
a non-intentional error by skipping steps in your head. go figure? :-)
Tell your "guru" friend this. It's not worth leaving the logic to the programmer.
Programmers make mistakes... computers only do as they are told.
hope this helps!
mark
-- _________________________
. . _ ._ . . .__ . . ._. .__ . . . .__ | Northern Telecom Ltd. |
|\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | Box 3511, Station 'C' |
| | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, ON K1Y 4H7 |
markm@nortel.ca / al278@freenet.carleton.ca |_______________________|
------------------------------
Date: Fri, 7 Nov 1997 15:33:34 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Chomp vs Chop
Message-Id: <ef1046.801.ln@localhost>
Matt Pezzuto (mpezzuto@link.com) wrote:
: My one friend and I got in an
: argument about chomp vs chop.
: My one friend is an perl guru
: and I am a newbie and I'm
: reading books and stuff. Well,
: I few weeks ago I had my friend
: write a perl script for me and
: he used chop to get ride of an
: eol. While I was learning perl
: I learned it was safer to use
: chomp if your only purpose is
: to get ride of an eol.
You learned correctly.
: I asked
: my friend why he didn't use
: chomp. He didn't know about it
^^^^^^^^^^^^^^^^^^^^^^^
So he must be a Perl 4 guru then...
: and asked what it did and I told
: him. But after thinking for a
: second he replied by saying,
: "When would ever not know what
: your chopping off?" I failed to
: come up with an example and he
^^^^^^^^^^
When the $/ special variable (the input record separator) has been set
to something other than its default of a newline.
In a multi-person development environment, your component containing
the chop() may be called after someone else's component has had their
way with the special variables.
Maybe your "guru" is really a "knows a lot of perl, but has never
worked on a truly large Perl programming project guy" ;-)
: claimed that using chomp was half-hazard
: way of programming because the
: programmer should know whether or
: not an eol is expected to not and
"not expected" and "can never happen" are not synonymous.
I would be suspicious of granting the title of "guru" so easily ;-)
: chop is quicker than chomp because
: it doesn't have to check to make
: sure the last character is an eol.
That is likely to be true.
: I replied with then why did they
: make chomp? I do not totally agree
: with him but I have to admit that
: he has a point. Who is right, the
: newbie or the guru?
You are right.
Make him buy you a beer (and suggest that he start using chomp() to
remove newlines ;-)
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: 07 Nov 1997 18:52:15 -0500
From: Mark Mielke <markm@nortel.ca>
Subject: Re: Decimal rounding and lining up decimals?
Message-Id: <lq1g1p89s9c.fsf@bmers2e5.i-did-not-set--mail-host-address--so-shoot-me>
jhughes@iastate.edu (Jeff Hughes) writes:
> I'm working with dollar amounts, which sometimes get multiplied and
> result in something like 45.375 Other than breaking it apart and putting
> it back together as a string, what's the quickest way to turn it into 45.38?
> I have something right now, but it just looks/seems too slow.
>
> Also the Camel book mentions output formatting using # and . for lining
> up numerical values but doesn't give any suggestions. Nothing I've tried
> seems to work.
>
> Please mail any suggestions, thanks.
>
>
> Jeff
I don't know if there's some standard package for doing this kinda thing, but
i would be tempted to do this: (if there's an easier way to do it than my
just written rounding functions... i'd be interested :-) )
$value = "45.375";
$value = round_simple($value * 100) / 100;
sub round_simple
{
my $sign = ($_[0] <=> 0);
my $integer = int(abs($_[0]));
my $fraction = abs($_[0]) - $integer;
$sign * $integer + (($fraction >= 0.5) ? $sign : 0);
}
sub round_ieee
{
my $sign = ($_[0] <=> 0);
my $integer = int(abs($_[0]));
my $fraction = abs($_[0]) - $integer;
$sign * $integer + (($fraction==0.5 ? $integer%2 : $fraction>=0.5) ? $sign : 0);
}
The simple rounding method just says if the fraction is greater than 0.5 than
bump it to the next integer. (note: not +1)
The ieee method takes into account the fraction being exactly 0.5. The rule here
is that it the "rounded result is even".
hope this helps!
mark
-- _________________________
. . _ ._ . . .__ . . ._. .__ . . . .__ | Northern Telecom Ltd. |
|\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | Box 3511, Station 'C' |
| | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, ON K1Y 4H7 |
markm@nortel.ca / al278@freenet.carleton.ca |_______________________|
------------------------------
Date: Fri, 7 Nov 1997 18:03:26 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Decimal rounding and lining up decimals?
Message-Id: <e8a046.gf1.ln@localhost>
Jeff Hughes (jhughes@iastate.edu) wrote:
: I'm working with dollar amounts
So you are dealing with all sorts of round-off errors and issues of
precision and accuracy then (aka number theory)?
It is either that, or you are content to "be off" by some unspecified amount
occasionally (I hate to lose money for no reason).
You might consider working with integer cents instead, and converting
to floating point dollars only for final output.
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 7 Nov 1997 17:56:27 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: eliminate all but a certain string
Message-Id: <br9046.gf1.ln@localhost>
jsd@bud.com wrote:
: i just looked in the faq but i didn't find what i needed.
That is an exceedingly short list of references you have there.
You should check these (I use them in the order given):
1) the man pages that are shipped with the perl distribution
2) the Perl FAQ
3) an O'Reilly book (Camel, Llama, ...)
4) a Dejanews search
5) post a question to Usenet
: i have an html file and i want to find all the comments (actually
: just certain ones).
: i tried this:
: perl -pe 's:.*(<\!--include virtual.*-->?):$1:s' psx/index.html
: but it didn't work. what am i doing wrong?
^^^^^^^^^^^^^^^^^^^^^
Not checking to see if this question has already been discussed here?
: i also tried with the s and g options to the replace but that didn't
: work either.
A Dejanews search, restricted to the comp.lang.perl.misc newsgroup,
for 'strip HTML comments' finds 24 hits...
consider this legal HTML comment:
<!-- yo ho -- -- ho and --
--a bottle of rum
--
>
;-)
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: 8 Nov 1997 00:19:00 GMT
From: robertf@geminet.com (Robert G. Ferrell)
Subject: Re: File handles and XSUBs
Message-Id: <640b5k$kc8@clarknet.clark.net>
In article <slrn65lfi1.f4.ljz@ljz.asfast.net>, ljz@asfast.com says...
>
>I'm looking for some documentation or examples which explain how to
>pass a file handle as an argument to an XSUB, and then to have the
>XSUB read from or write to this file handle.
Covered in "Advanced Perl Programming," especially pp. 353-354.
Robert G. Ferrell
rferrell@clark.net
------------------------------
Date: Fri, 7 Nov 1997 15:20:28 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: file not found error?
Message-Id: <sm0046.2u.ln@localhost>
Brad Bradley (brad.bradley@bridge.bellsouth.com) wrote:
: > I have a snippet of code:
: >
: > chdir("/export/0/webman/checkin_server/data/") || print "1\n\n";
: > @sched = `/bin/cat $username.sched`; print @sched;
^ ^
^ ^
: It looks like the single quotes are what is throwing you off. Perl won't
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: interpret any variable stored inside the single quotes, thuis your username
: never gets trenslated during runtime. Try "" to set off the command and you
: should have no problem.
Those aren't single quotes.
Those are backticks.
backticks already are "double quotish"...
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 7 Nov 1997 14:33:08 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: glob not working
Message-Id: <4utv36.gq.ln@localhost>
Umm (l41484@alfa.ist.utl.pt) wrote:
: Is there any reason, why glob doesn't work???
You have not given us enough information to even tell if it is
working or not.
The result you give is "working right" if there are no files with
a '.pl' filename extension in the current directory.
Are there really some files named that way in the current directory?
Have you printed out what directory you are in?
: $tst = glob("*.pl");
glob() may return more than one filename.
Where would they go? (answer: the first one goes in $tst, the rest
are discarded...)
Maybe you want an array there instead?
@tst = glob("*.pl");
: puts "" $tst, any clues??
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 07 Nov 1997 15:48:23 -0800
From: Steve Moody <moody@roguewave.com>
Subject: HELP - deleting an array element out of a "non-associative" array
Message-Id: <3463A8C7.7FD1@roguewave.com>
I need some help with an array problem.
I have already seen the delete command for use with associative
arrays, but I have a non associative array that I need to be able
to remove a specific element from easily.
I have a foreach loop stepping through the array
when a certain condition is reached I want to be able
to remove that element currently being tested from the array
and then proceed with the next element.
I already know I could create a new array and "push" the good elements
onto it, but I was hoping that there was a clever way to just
remove the element from the current array.
Please email as well as post, and thanks in advance
-Steve
--
***************************
Steve Moody
Configuration Librarian
Rogue Wave Software
moody@roguewave.com
(541) - 754 - 3297
***************************
------------------------------
Date: 8 Nov 1997 00:04:18 GMT
From: Zenin <zenin@best.com>
Subject: Re: HELP - deleting an array element out of a "non-associative" array
Message-Id: <640aa2$omb$6@nntp2.ba.best.com>
Steve Moody <moody@roguewave.com> wrote:
: I have a foreach loop stepping through the array
: when a certain condition is reached I want to be able
: to remove that element currently being tested from the array
: and then proceed with the next element.
foreach my $i (0..$#myArray) {
if (whatever()) {
splice @myArray, $i, 1;
redo;
}
}
--
-Zenin (zenin@best.com)
The Bawdy Caste (San Jose, CA) http://www.netmagic.net/~dmcgrath/bawdy/
Barely Legal (Berzerkly, CA) http://www.barelylegal.org/
Zenin's Rocky Archive (Moving soon!) http://www.best.com/~zenin/
------------------------------
Date: Fri, 07 Nov 1997 16:06:38 -0800
From: Steve Moody <moody@roguewave.com>
Subject: Re: HELP - deleting an array element out of a "non-associative" array
Message-Id: <3463AD0E.404E@roguewave.com>
Its pretty bad when you re: your own post.. I have realized
sorry (stupidity comes in spurts) that YOU DO NOT
want to muck with an array during an itteration of that array.
Therefore my problem probably becomes moot. I can only
envision a solution of creating a temporary array (by using push)
during the itteration of the good elements, and then assign the
new array to the old name, to keep the rest of my program happy.
Now if someone knows a clever way of taking this outside of the
itterative loop, and say with my bad elements captured to a string
tell me a slick way to remove specific elements from a non itterative
array, that might be helpful
Any advice is appreciated ;-)
Thanks
-Steve
Steve Moody wrote:
>
> I need some help with an array problem.
>
> I have already seen the delete command for use with associative
> arrays, but I have a non associative array that I need to be able
> to remove a specific element from easily.
>
> I have a foreach loop stepping through the array
> when a certain condition is reached I want to be able
> to remove that element currently being tested from the array
> and then proceed with the next element.
>
> I already know I could create a new array and "push" the good elements
> onto it, but I was hoping that there was a clever way to just
> remove the element from the current array.
>
> Please email as well as post, and thanks in advance
> -Steve
> --
> ***************************
> Steve Moody
> Configuration Librarian
> Rogue Wave Software
> moody@roguewave.com
> (541) - 754 - 3297
> ***************************
--
***************************
Steve Moody
Configuration Librarian
Rogue Wave Software
moody@roguewave.com
(541) - 754 - 3297
***************************
------------------------------
Date: 7 Nov 1997 23:45:56 GMT
From: dmacks@sas.upenn.edu (Daniel E. Macks)
Subject: Re: Intricate Sort help needed
Message-Id: <64097k$a9q$1@netnews.upenn.edu>
Bruce P. Schuck (hd-fxsts@liii.com) said:
:
: I tried to install the Sort-PolySort module, but had problem but the
: "perl Makefile.PL" failed:
I screwed up the Makefile.PL. All it does is copy PolySort.pm into a
directory called Sort in your lib, so you can do it by hand. There're
a bunch of different parsing routines, and you could probably fix one
up to do what you want. Probably use a regexp like /(....)(..)/ or
substr() or maybe even unpack to pull apart fixed-width strings like
yours.
Or you could just write your own sort sub...same principle.
dan
--
Daniel Macks
dmacks@a.chem.upenn.edu
dmacks@netspace.org
http://www.netspace.org/~dmacks
------------------------------
Date: Fri, 7 Nov 1997 23:55:00 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Intricate Sort help needed
Message-Id: <EJAvrp.D5q@world.std.com>
"Bruce P. Schuck" <hd-fxsts@liii.com> writes:
>012001
>012002
>012003
>011001
>011002
>013001
>013002
>The need has arisen that at one time, I need to sort this list where the
>section numbers ascend and the sequence numbers ascend; and at another
>time the section numbers ascend, but the sequence descends.
Use the techniques described in the FAQ entry "How do I sort an array
by (anything)" available at:
<URL:http://www.perl.com/CPAN/doc/manual/html/pod/perlfaq4/
How_do_I_sort_an_aray_by_anyth.html>
#split the article IDs into section and sequence and store the data
# away for later retrieval.
for $entry (keys %articleID) {
($section, $sequence) = unpack 'A3A3', $entry;
$section{$entry} = $section;
$sequence{$entry} = $sequence;
}
for $entry (sort by_section_asc_seq_desc keys %articleID) {
print "$entry\n";
}
# compares each entry first by section. If one section is greater than another
# then it returns a flag to notify sort of the greater section. If the
# section numbers are equal, then it compares the sequence number, and returns
# the flag for the lesser section.
sub by_section_asc_seq_desc {
$section{$a} <=> $section{$b} or # sort by section numerically ascending
$sequence{$b} <=> $sequence{$a} # sort by seq. descending for each section
}
The FAQ methods are slightly faster since they use parallel arrays or
arrays of references to arrays instead of hashes. Hash lookups don't
take that much time, but when you do it N * log(N) times, it starts to
add up. The FAQ examples have the same basic principle of splitting
the data once before sorting instead of splitting for each comparison,
because if the total time for hash lookups start to be too time
consuming, the time for repeatedly unpacking will be astronomical.
The technique that uses references is commonly called the Scwartzian
Transform. It is a very compact representation of what needs to be
done to sort a construct like this, and about equal in speed to the
parallel array version, but will have you scratching your head trying
to figure out how it does it.
for $entry( map { $_->[0] }
sort { $a->[1] <=> $b->[1] or $b->[2] <=> $a->[2] }
map { [ $_, unpack 'A3A3' ] } keys %articleID ) {
print "$entry\n";
}
--
Andrew Langmead
------------------------------
Date: Fri, 7 Nov 1997 18:18:17 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Intricate Sort help needed
Message-Id: <94b046.7i1.ln@localhost>
Bruce P. Schuck (hd-fxsts@liii.com) wrote:
: The need has arisen that at one time, I need to sort this list where the
: section numbers ascend and the sequence numbers ascend; and at another
: time the section numbers ascend, but the sequence descends. The first
: scenario is easy, just sort the hash table and I'd get:
: 011001
: 011002
: 012001
: 012002
: 012003
: 013001
: 013002
: *But* I need to easily get this list to sort as:
: 011002
: 011001
: 012003
: 012002
: 012001
: 013002
: 013001
: So perhaps someone perhaps knows of a tricky way to do this?
That would be a Schwartzian Transform. Someone will likely follow
up with one.
If you only have a few thousand array elements, this should work OK:
-----------------------------------------------
#!/usr/bin/perl -w
foreach (sort like_bruce_wants <DATA>) {
print;
}
sub like_bruce_wants {
my($sect_a, $seq_a) = $a =~ /(...)(...)/;
my($sect_b, $seq_b) = $b =~ /(...)(...)/;
return $sect_a <=> $sect_b unless $sect_a eq $sect_b;
return $seq_b <=> $seq_a;
}
__DATA__
012001
012002
012003
011001
011002
013001
013002
-----------------------------------------------
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: 7 Nov 1997 23:34:36 GMT
From: Zenin <zenin@best.com>
Subject: Re: is it possible to reference a sub-array?
Message-Id: <6408ic$omb$3@nntp2.ba.best.com>
Umm <l41484@alfa.ist.utl.pt> wrote:
: I want to do something like:
: &function(\@ARGV[2]);
: is it possible???
function \$ARGV[2];
And, if you _really_ want to play with fire:
function [ \(@ARGV[2..5]) ];
--
-Zenin (zenin@best.com)
The Bawdy Caste (San Jose, CA) http://www.netmagic.net/~dmcgrath/bawdy/
Barely Legal (Berzerkly, CA) http://www.barelylegal.org/
Zenin's Rocky Archive (Moving soon!) http://www.best.com/~zenin/
------------------------------
Date: 07 Nov 1997 18:55:28 -0500
From: Mark Mielke <markm@nortel.ca>
Subject: Re: is it possible to reference a sub-array?
Message-Id: <lq1btzw9s3z.fsf@bmers2e5.i-did-not-set--mail-host-address--so-shoot-me>
l41484@alfa.ist.utl.pt (Umm) writes:
> I want to do something like:
> &function(\@ARGV[2]);
> is it possible???
> i mean directly without:
> shift;shift;
> &function(\@ARGV);
No.
What you may wish to try is:
function(@ARGV[2..$#ARGV]); # pass array in.
or,
function( [ @ARGV[2..$#ARGV] ] ); # pass by reference.
hope this helps!
mark
-- _________________________
. . _ ._ . . .__ . . ._. .__ . . . .__ | Northern Telecom Ltd. |
|\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | Box 3511, Station 'C' |
| | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, ON K1Y 4H7 |
markm@nortel.ca / al278@freenet.carleton.ca |_______________________|
------------------------------
Date: 7 Nov 1997 23:25:58 GMT
From: Zenin <zenin@best.com>
Subject: Re: One slice of hash, please...
Message-Id: <640826$omb$2@nntp2.ba.best.com>
Phil R Lawrence <prl2@lehigh.edu> wrote:
: I am doing something like:
: print "$hash{key5} $hash{key2} $hash{key8} $hash{key4} $hash{key12}\n";
: but I tire of typing that hash name over and over. Is there anything like:
: print "$hash{key5, key2, key8, key4, key12}\n"; # ?
Close:
print "@hash{'key5', 'key2', 'key8', 'key4', 'key12'}\n";
or even better (IMHO):
print "@hash{qw( key5 key2 key8 key4 key12 )}\n";
--
-Zenin (zenin@best.com)
The Bawdy Caste (San Jose, CA) http://www.netmagic.net/~dmcgrath/bawdy/
Barely Legal (Berzerkly, CA) http://www.barelylegal.org/
Zenin's Rocky Archive (Moving soon!) http://www.best.com/~zenin/
------------------------------
Date: Fri, 7 Nov 1997 23:26:16 GMT
From: Charles Margolin <cmargoli@world.northgrum.com>
Subject: Re: One slice of hash, please...
Message-Id: <3463A398.6766@world.northgrum.com>
Phil R Lawrence wrote:
>
> I am doing something like:
> print "$hash{key5} $hash{key2} $hash{key8} $hash{key4} $hash{key12}\n";
>
> but I tire of typing that hash name over and over. Is there anything like:
> print "$hash{key5, key2, key8, key4, key12}\n"; # ?
Close. For a slice, use @ rather than $ and remember that a slice
is a list.
print join(' ', @hash{qw(key5 key2 key8 key4 key12)}), "\n";
--
Charles G. Margolin DSSD Internal Information Services
cmargoli@world.northgrum.com Northrop Grumman Corp. 0624/23
margolin@acm.org Hawthorne, California 90250-3277
------------------------------
Date: 7 Nov 1997 23:47:13 GMT
From: toutatis@_SPAMTRAP_toutatis.net (Toutatis)
Subject: Re: One slice of hash, please...
Message-Id: <toutatis-ya023180000811970047110001@news.euro.net>
Phil R Lawrence" <prl2@lehigh.edu> wrote:
> I am doing something like:
> print "$hash{key5} $hash{key2} $hash{key8} $hash{key4}
$hash{key12}\n";
>
> but I tire of typing that hash name over and over. Is there anything like:
> print "$hash{key5, key2, key8, key4, key12}\n"; # ?
probably
print @hash{map{"key$_"}(5,2,8,4,12)},"\n";
will do what you want.
--
Toutatis
------------------------------
Date: 07 Nov 1997 18:59:15 -0500
From: Mark Mielke <markm@nortel.ca>
Subject: Re: One slice of hash, please...
Message-Id: <lq17mak9rxo.fsf@bmers2e5.i-did-not-set--mail-host-address--so-shoot-me>
"Phil R Lawrence" <prl2@lehigh.edu> writes:
> I am doing something like:
> print "$hash{key5} $hash{key2} $hash{key8} $hash{key4} $hash{key12}\n";
>
> but I tire of typing that hash name over and over. Is there anything like:
> print "$hash{key5, key2, key8, key4, key12}\n"; # ?
Yes... it's wierd... but you can do it:
print "@hash{key5,key2,key8,key4,key12}\n";
These are equivalent:
@hash{key1,key2,key3} = (value1, value2, value3);
($hash{key1}, $hash{key2}, $hash{key4}) = (value1, value2, value4);
hope this helps!
mark
-- _________________________
. . _ ._ . . .__ . . ._. .__ . . . .__ | Northern Telecom Ltd. |
|\/| |_| |_| |/ |_ |\/| | |_ | |/ |_ | Box 3511, Station 'C' |
| | | | | \ | \ |__ . | | .|. |__ |__ | \ |__ | Ottawa, ON K1Y 4H7 |
markm@nortel.ca / al278@freenet.carleton.ca |_______________________|
------------------------------
Date: 8 Nov 1997 00:13:27 GMT
From: robertf@geminet.com (Robert G. Ferrell)
Subject: Re: other language for perl
Message-Id: <640ar7$kc8@clarknet.clark.net>
In article <34515BDE.7EC1@all-soccer.com>, soccer@all-soccer.com says...
>I wish to translate my site to another language.
>Most of the site is html created from perl cgi.
>
>Could someone tell me how the process might work?
You need to be just a tad more specific. The only script I saw in a
brief perusal of your site was continue.pl; is this the one you want to
convert? Convert to what? Javascript? C++? Java? Why would anyone
voluntarily abandon Perl for some lesser language? :-)
Robert G. Ferrell
rferrell@clark.net
------------------------------
Date: Fri, 07 Nov 1997 15:26:16 -0800
From: Jim Voll <jjv@netapp-nospam.com>
Subject: PERL interpreter in Java?
Message-Id: <3463A398.4DCA@netapp-nospam.com>
This may seem silly, but does anyone know of a PERL interpreter
written in Java?
------------------------------
Date: Fri, 07 Nov 1997 22:46:43 GMT
From: bjs@intac.com (Bruce Seiler)
Subject: Perl web page counter troubles
Message-Id: <3463925b.5961640@news.intac.com>
I'm trying to use the following script to record hits to my
web page.
#!/usr/local/bin/perl
print "Content-type: text/plain", "\n\n";
open (COUNT, "+<ezcount.dat") || die "Cannot read
from ezcount.dat.\n";
$visitors = <COUNT>;
$visitors = $visitors + 1;
print $visitors;
seek (COUNT, 0, 0);
print COUNT $visitors;
close (COUNT);
I call it as a server side include from my home page
index.shtml using:
<P><!--#exec cgi="/cgi-local/ezcount.pl" --></P>
It works fine for a while, recording hits and incrementing
the count as expected. But every couple of days, the
count gets lost, and starts again from zero. I can't figure
out why. Could there be a problem when two people hit
the page at the same time? If so, can it be fixed? Or
maybe it's something else. I have the feeling I'm
overlooking something fundamental. I'd really
appreciate help.
Thanks,
Bruce Seiler
bjs@intac.com
------------------------------
Date: Fri, 7 Nov 1997 17:44:03 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Regular Expressions
Message-Id: <349046.ae1.ln@localhost>
Gary C. New (gnew@southernvirginia.edu) wrote:
: Bart Lateur wrote:
: >
: > >In article <comdog-ya02408000R0611971707560001@news.panix.com>,
: > comdog@computerdog.com (brian d foy) writes:
: > > i hate this sort of data manipulation - it seems that a lot of systems
: > > do this - and i should know :)
: >
: > "Brian D Foy"?
: >
: > mgjv@comdyn.com.au (Martien Verbruggen) wrote:
: > >Yeah,
: > >
: > >what if your last name is McBeth, O'Donnell, or Lloyd Webber ?
: >
: > Lettin' [ s/(/w+)/\u\L$1/g ] loose on these, I get:
: >
: > Mcbeth (oops)
: > O'Donnell (ok)
: > Lloyd Webber (ok)
: >
: > Bart.
: How would it be possible to write the expression to include names such
: as McBeth also?
It is hopeless.
Give up.
After someone posts one that handles McBeth, someone else will
post yet another one that needs fixing...
... like: de Angelo
Hire a college student at $6.00/hr. Some things just cannot be programmed.
--
Tad McClellan SGML Consulting
tadmc@flash.net Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 07 Nov 1997 17:31:56 -0600
From: mehta@mama.indstate.edu
Subject: scalar function weird
Message-Id: <878944806.28455@dejanews.com>
#!/usr/bin/perl
print ">>$/<<\n";
$/ = "";
open (IN, "<in.txt") or die "cannot open file in.txt $!\n";
if (1 == 1) {
local($all) = scalar (<IN>);
print "ALL: $all";
close (IN);
}
-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.misc (and this Digest), send your
article to perl-users@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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 1288
**************************************