[22845] in Perl-Users-Digest
Perl-Users Digest, Issue: 5066 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 3 00:05:45 2003
Date: Mon, 2 Jun 2003 21:05:06 -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 Mon, 2 Jun 2003 Volume: 10 Number: 5066
Today's topics:
Re: Adding to @INC <thepotplants@yahoo.com>
Re: Adding to @INC <matthew@weierophinney.net>
Re: Adding to @INC <noreply@gunnar.cc>
CFV: comp.databases.berkeley-db <dave@technopagan.org>
Re: Doing substitutions in a while loop (while /.../g) <ben.goldberg@hotpop.com>
Re: Doing substitutions in a while loop (while /.../g) <ben.goldberg@hotpop.com>
Re: Help: Sort HOH by value? ctcgag@hotmail.com
Re: Help: Sort HOH by value? <email_entropy123@yahoo.com>
Re: Help: Sort HOH by value? ctcgag@hotmail.com
Re: linklint.pl (website linkchecker) and onclick <ben.goldberg@hotpop.com>
perl script for level 0 and incremental backups <hugo@geoinformex.com>
Regular expression help <imarinejt@hotmail.com>
Re: Regular expression help <grazz@pobox.com>
Resolve UNIX environment variable within an array (adans)
Re: Sorting hash <ben.goldberg@hotpop.com>
Re: Sorting hash <noreply@gunnar.cc>
Re: uninitialized value in eval block? <ben.goldberg@hotpop.com>
Re: uninitialized value in eval block? <ben.goldberg@hotpop.com>
Re: what does KEYS return? <notspam@spamfree.dud>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 3 Jun 2003 14:02:55 +1200
From: "ThePotPlants" <thepotplants@yahoo.com>
Subject: Re: Adding to @INC
Message-Id: <nlTCa.6761$JA5.118453@news.xtra.co.nz>
Err.. I think I have it..
I'm not certain I underderstand the doc page. I'm assuming I substitute
'lib' with the library I want. (in this case DBI)
I now get a different error message so assum it's using the DBI package as
hoped. However there is still an error message on line 2 where I have
defined my package.
# test connection
use DBI 'C:\Perl\site\lib\' ;
## define handle
my $dbh = DBI ->connect("dbi:Oracle:OMDEV1","scott","tiger");
#test
my $sth = $dbh->prepare( "SELECT SYSDATE FROM DUAL") ;
$sth execute;
exit ;
D:\dbdb\bin>perl tcon.pl
Can't find string terminator "'" anywhere before EOF at tcon.pl line 2.
Is there another way of simply adding the directory to the other paths?
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:bbgqgd$937lb$1@ID-184292.news.dfncis.de...
> ThePotPlants wrote:
> > How do I add locations/directories to @INC so It finds the required
> > package, and/or where is it described in the docs (I couldn't find
> > it.)
>
> http://www.perldoc.com/perl5.8.0/lib/lib.html
>
> / Gunnar
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>
------------------------------
Date: Tue, 03 Jun 2003 02:18:17 GMT
From: Matthew Weier O'Phinney <matthew@weierophinney.net>
Subject: Re: Adding to @INC
Message-Id: <slrnbdo1b9.16p.matthew@kavalier.weierophinney.net>
* ThePotPlants <thepotplants@yahoo.com>:
> Err.. I think I have it..
> I'm not certain I underderstand the doc page. I'm assuming I substitute
> 'lib' with the library I want. (in this case DBI)
No, 'lib' is a standard perl module used to modify @INC, which, as you
noted in your original post, is much like $PATH in many shells: it tells
perl where to look for libraries. Its usage is:
use lib qw(/path/to/libraries);
Note that it takes a list; thus you usually call it with qw(). For more
info, see perldoc lib.
> I now get a different error message so assum it's using the DBI package as
> hoped. However there is still an error message on line 2 where I have
> defined my package.
>
># test connection
> use DBI 'C:\Perl\site\lib\' ;
And this was where I discovered that you hadn't looked up lib in the
perldocs. ;-) Use perldoc whenever you can; it's your friend. The one
that would have answered your question immediately was "perldoc -q
'include path'"; after Gunnar responded to you, "perldoc lib" would have
given you your info.
> "Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
> news:bbgqgd$937lb$1@ID-184292.news.dfncis.de...
>> ThePotPlants wrote:
>> > How do I add locations/directories to @INC so It finds the required
>> > package, and/or where is it described in the docs (I couldn't find
>> > it.)
> >
>> http://www.perldoc.com/perl5.8.0/lib/lib.html
--
Matthew Weier O'Phinney
matthew@weierophinney.net
http://matthew.weierophinney.net
------------------------------
Date: Tue, 03 Jun 2003 04:43:47 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Adding to @INC
Message-Id: <bbh2gp$9drf4$1@ID-184292.news.dfncis.de>
ThePotPlants wrote:
> use DBI 'C:\Perl\site\lib\' ;
No. This is how you make use of lib.pm to change @INC:
use lib 'C:/Perl/site/lib';
use DBI;
> Is there another way of simply adding the directory to the other
> paths?
Can it be simpler than that? :)
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Tue, 03 Jun 2003 02:11:54 +0000
From: David E. Smith <dave@technopagan.org>
Subject: CFV: comp.databases.berkeley-db
Message-Id: <1054606314.6246@isc.org>
FIRST CALL FOR VOTES (of 2)
unmoderated group comp.databases.berkeley-db
Newsgroups line:
comp.databases.berkeley-db The Berkeley DB libraries.
Votes must be received by 23:59:59 UTC, 24 Jun 2003.
This vote is being conducted by a neutral third party. Questions
about the proposed group should be directed to the proponent.
Proponent: Philip Greer <philip@tildesoftware.com>
Proponent: Dave Segleau <dave@sleepycat.com>
Votetaker: David E. Smith <dave@technopagan.org>
RATIONALE: comp.databases.berkeley-db
There is not a specific Usenet location for discussions on berkeley
database management software (from this point forward I'll refer to as
just 'berkeley-db'). Discussion pertaining to the library occurs on
many other other Usenet forums. Often writings are specific to
berkeley-db itself and does not pertain to the subject matter of the
group the discussion falls within. Not only would a berkeley-db
specific forum provide a central location for todays frequent Usenet
users, it would also provide an on-line research tool for those
seeking on-line issue assistance with the library. Entities such as
Google will provide archives of group postings, and search tools for
those seeking answers to berkeley-db questions - drawing those on-line
that were not aware of the Usenet group (or of net news as a whole).
>From the "Berkeley DB Reference Guide: Introduction": '[berkeley-db]
runs under almost all UNIX and Linux variants, Windows, and a number
of embedded real-time operating systems. It runs on both 32-bit and
64-bit systems. It has been deployed on high-end Internet servers,
desktop machines, and on palmtop computers, set-top boxes, in network
switches, and elsewhere.' As well, berkeley-db has been a tool
available to the programming community for over ten years and
is used on a global scale with over 200 million installations. The
need for an on-line, central location for community discussions has
been long standing. Usenet is the best tool to fulfill this need and
allows addressing this need on a global scale.
CHARTER: comp.databases.berkeley-db
The newsgroup comp.databases.berkeley-db is a news forum devoted to
the discussion of the berkeley database management library. Discussion
entails, but is not limited to: its usage in all programming languages
that can utilize it, bugs, patches, tricks, techniques, support,
announcements; all in a free and open atmosphere.
All postings should be in ordinary text format only. Postings of
binaries (or encoding thereof) is highly discouraged. Cross-posting of
articles irrelevant to the berkeley database library is also
discouraged. The berkeley-db newsgroup is not moderated. Therefore it
is up to the global user base to police its usage.
END CHARTER.
IMPORTANT VOTING PROCEDURE NOTES: READ THIS BEFORE VOTING
The purpose of a Usenet vote is to determine the genuine interest in
reading the proposed newsgroup, and soliciting votes from uninterested
parties defeats this purpose. Do *not* distribute this CFV; instead,
direct people to the official CFV as posted to news.announce.newgroups.
Distributing specific voting instructions or pre-marked copies of
this CFV is considered vote fraud.
This is a public vote: All email addresses, names and votes will be
listed in the final RESULT post. The name used may be either a real
name or an established Usenet handle.
At most one vote is allowed per person or per account. Duplicate
votes will be resolved in favor of the most recent valid vote.
Voters must mail their ballots directly to the votetaker. Anonymous,
forwarded, or proxy votes are not valid, nor are votes mailed from
WWW/HTML/CGI forms (which should not exist). Votes from nonexistent
accounts or with munged, spam-blocked, or undeliverable addresses are
invalid and will NOT be counted.
Please direct any questions to the votetaker at <dave@technopagan.org>
HOW TO VOTE:
Extract the ballot from the CFV by deleting everything before and after
the "BEGINNING OF BALLOT" and "END OF BALLOT" lines. Don't worry about
the spacing of the columns or any quote characters (">") that your
reply inserts. Please, DO NOT send the entire CFV back to me!
Fill in the ballot as shown below. Please provide your real name
(or established Usenet handle) and indicate your desired vote in the
appropriate locations inside the ballot.
Examples of how to properly indicate your vote:
[ YES ] example.yes.vote
[ NO ] example.no.vote
[ ABSTAIN ] example.abstention
[ CANCEL ] example.cancellation
DO NOT modify, alter or delete any information in this ballot!
If you do, the voting software will probably reject your ballot.
When finished, MAIL the vote to <cdbvote@technopagan.org>
Just "replying" to this message should work, but check the "To:" line.
Votes mailed to any other email address, including that of the votetaker,
will NOT be counted.
If you do not receive an acknowledgment of your vote within three
days contact the votetaker about the problem. You are responsible
for reading your ack and making sure your vote is registered correctly.
If these instructions are unclear, please consult the Introduction to
Usenet Voting or the Usenet Voting FAQ at http://www.stanford.edu/~neilc/uvv.
======== BEGINNING OF BALLOT: Delete everything before this line =======
.-----------------------------------------------------------------------
| 1ST CALL FOR VOTES: comp.databases.berkeley-db
| Official Usenet Voting Ballot [CDB-42-1] (Do not remove this line!)
|-----------------------------------------------------------------------
| Please provide your real name, or your vote may be rejected. Established
| Usenet handles are also acceptable. Place ONLY your name (ie. do NOT
| include your e-mail address or any other information; ONLY your name)
| after the colon on the following line:
Voter Name:
| Insert YES, NO, ABSTAIN, or CANCEL inside the brackets for each
| newsgroup listed below (do not delete the newsgroup name):
Your Vote Newsgroup
--------- -----------------------------------------------------------
[ ] comp.databases.berkeley-db
======== END OF BALLOT: Delete everything after this line ==============
DISTRIBUTION:
This CFV will be posted to the following newsgroups:
news.announce.newgroups
news.groups
comp.databases
comp.lang.perl.misc
comp.lang.python
Pointers to the CFV will be posted to the following newsgroups:
alt.perl
comp.lang.java.databases
comp.lang.perl.modules
comp.mail.imap
comp.mail.sendmail
comp.unix.programmer
------------------------------
Date: Mon, 02 Jun 2003 21:29:15 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Doing substitutions in a while loop (while /.../g)
Message-Id: <3EDBF9EB.352A389F@hotpop.com>
Arvin Portlock wrote:
>
> What is the most elegant way of performing substitutions if
> you're matching patterns in a while loop running through a
> big old block of slurped text?
>
> For example suppose I have a bunch of values in an array
> I want to swap in for markers in an HTML file (which can
> span multiple lines). This is not the actual application
> I want to do but hopefully describes what I want more
> clearly.
>
> local $/ = undef;
> my $text = <>;
> while ($text =~ /<!-- ([^<>]+) -->/g) {
> my $subst_val = pop @array;
> ## Here, somehow replace $1 with $subst_val
> }
Approxomately two ways:
$text =~ s
[<!-- ([^<>]+) -->]
{
my $subs_val = pop @array;
"<!-- $subs_val -->";
};
Or:
while( $text =~ /<!-- ([^<>]+) -->/g ) {
my $oldpos = pos($text);
my $oldlen = length $1;
my $subst_val = pop @array;
substr( $text, $-[1], $oldlen ) = $subst_val;
pos($text) = $oldpos + length($subst_val) - $oldlen;
}
> ## Write $text to a new file
>
> I find myself wanting to do this all the time for a
> variety of applications. Can somebody tell me a fast,
> elegant way to do it?
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Mon, 02 Jun 2003 21:33:42 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Doing substitutions in a while loop (while /.../g)
Message-Id: <3EDBFAF6.97B67CB@hotpop.com>
Arvin Portlock wrote:
>
> >What is the most elegant way of performing substitutions if
> >> you're matching patterns in a while loop running through a
> >> big old block of slurped text?
> >> while ($text =~ /<!-- ([^<>]+) -->/g) {
> >> my $subst_val = pop @array;
> >> ## Here, somehow replace $1 with $subst_val
> >> }
>
> $text =~ s/<!-- ([^<>]+) -->/ '<!-- ' . pop(@array) . ' -->' /ge;
>
> Hmmm, so if I want to do something more complicated than
> pop ()--much more complicated as is generally the case--I can
> use a subroutine:
>
> $text =~ s/<!--([^<>]+)-->/'<!--' . &complicated_sub($1) . '-->'/ge;
You can, but generally don't need to. The replacement expression *is*
allowed to span multiple lines, after all.
Also, remember that you can use other delimiters than "/" for the s///
operator. In particular, it's often convienent to use paired
delimiters, such as <>, [], {} or ().
> This never occurred to me. Thanks! This is a pretty useful tool
> for me to be adding to my arsenal.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: 03 Jun 2003 01:18:39 GMT
From: ctcgag@hotmail.com
Subject: Re: Help: Sort HOH by value?
Message-Id: <20030602211839.935$tQ@newsreader.com>
email_entropy123@yahoo.com (entropy123) wrote:
> Hey all
>
> I have a %HoH with $HoH{$key1}{$key2}=$value is there any way to sort
> this according to $value?
There are many ways, depending on what you mean by that.
> I'm pulling out $key2 as part of a foreach
> statement
Why not just give the foreach statement? It would probably be more
concise, and then we would know what you are talking about.
> and would like to be sure that $HoH
I have no idea what $HoH refers to. This is the first time you mentioned
it.
> is equal to a specific
> $value for the very first key pulled by the foreach...
Or else what?
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Mon, 02 Jun 2003 20:32:07 -0500
From: "ent123" <email_entropy123@yahoo.com>
Subject: Re: Help: Sort HOH by value?
Message-Id: <pan.2003.06.03.01.32.00.413407@yahoo.com>
>
> Yes.
>
> How about letting us know which efforts you made to find out yourself
> before you posted here?
>
> / Gunnar
Hmmmm......ok...
I searched the usenet and googled it. Also, I read through the HoH section
on CPAN 'perldoc -q Hoh sort'.... This is my usual routine...I also have a
few perl books 'Learning Perl' and 'Mastering Algorithms with Perl' <=this
one is a little questionable...but the jury is still out IMHO. I wish I had a copy of the perl
cookbook....
CPAN covers printing HoHs but not much else. If you know a good source of
info on HoHs - especially sorting by values, keys and determining number
of $keys1 or $keys2 that would be awesome.
thanks,
entropy
------------------------------
Date: 03 Jun 2003 01:54:15 GMT
From: ctcgag@hotmail.com
Subject: Re: Help: Sort HOH by value?
Message-Id: <20030602215415.185$GK@newsreader.com>
"ent123" <email_entropy123@yahoo.com> wrote:
> >
> > Yes.
> >
> > How about letting us know which efforts you made to find out yourself
> > before you posted here?
> >
> > / Gunnar
>
> Hmmmm......ok...
>
> I searched the usenet and googled it. Also, I read through the HoH
> section on CPAN 'perldoc -q Hoh sort'.... This is my usual routine...I
> also have a few perl books 'Learning Perl' and 'Mastering Algorithms with
> Perl' <=this one is a little questionable...but the jury is still out
> IMHO. I wish I had a copy of the perl cookbook....
>
> CPAN covers printing HoHs but not much else.
Reading perldoc and applying yourself is entirely sufficient.
> If you know a good source of
> info on HoHs -
perldoc is a good source of this info.
> especially sorting by values, keys
You do not sort hashes themselves, you sort some kind of list which
may have come out of a hash.
@sorted_keys=sort keys %hash;
@keys_sorted_by_values_they_correspond_to=
sort {$hash{$a} <=> $hash{$b}} keys %hash;
> and determining number
> of $keys1 or $keys2 that would be awesome.
The number of $keys1 or $keys2 has only to do with the scopes of your
program, and nothing to do with hashes. For example, most of my programs
have zero $keys1 or $keys2. If I have 3 lexical scopes that "my" a $keys1,
then I have three $keys1.
Perhaps you meant "How to tell how many keys a hash has", which is simply:
scalar keys %hash;
And "How many keys a hash whose reference is contained in another hash
has", which is simply:
scalar keys %{$hash{$key1}};
Note that this second one is nothing magical, it is simply taking the prior
case and then applying what you (should) know about dereferencing to it.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
------------------------------
Date: Mon, 02 Jun 2003 22:08:10 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: linklint.pl (website linkchecker) and onclick
Message-Id: <3EDC030A.B0DECB72@hotpop.com>
Bart Lateur wrote:
>
> zzapper wrote:
>
> >Does anyone know the tool well enough to suggest how I can modify the
> >above to recognise links of the type
> >
> >onClick="window.location.href='store/wigs.html'"
>
> I'm not familiar with it, but anyway... It can even get more nasty than
> this. How about if you get:
>
> <script>
> function goto (url) {
> this.location = url;
> }
> </script>
>
> <button onclick="goto('store/wigs.html')">go to
> <b>Wigs</b></button>
>
> I think that, to truely get a decent link follower, you will might
> need more than just a few regular expressions. You might need a
> Javascript parser, in order to derive what you just did mentally in
> the above example. 't Is not easy.
"Not easy" is an understatement.
Given that JavaScript is Turing Complete, it could require solving the
halting problem in order to know that a particular string can ever
potentially be used as a url.
> At least, it needs *some* basic Javascript knowledge. A daunting task.
Actually, it "merely" needs one find a Javascript language definition (a
grammer in some kind of normal form or other), and rewrite it to suite
Parse::RecDescent. *Then* one might worry about actually understanding
Javascript.
As a simple hueristic (once you've got a grammer), you could find all
constant strings (which is just 'store/wigs.html' in your example code),
use a regex to see if they "look like" urls, and attempt to see if they
go anywhere.
This will fail in many, many cases, including for example:
<script>
function goto (url) {
this.location = url + ".html";
}
</script>
<button onclick="goto('store/wigs')">go to <b>Wigs</b></button>
But at least it would be better than nothing.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Tue, 3 Jun 2003 10:21:58 +0800
From: "Hugo Bouckaert" <hugo@geoinformex.com>
Subject: perl script for level 0 and incremental backups
Message-Id: <3edc066f$0$18548@echo-01.iinet.net.au>
Hi
Does anyone know of a perl script that does level 0 and incremental backups
for a directory and its subdirectories from one location on a disk to
another?
Each incremental backup should check the level 0 backup and only backup
files with later timestamps. Preferably each backup should bzip up each file
within the directory, with the whole backup also being a tar zipped file
with the date of the backup inserted in the filename.
Does anyone kow of a program that does all (or most) of the above?
Any help will be greatly appreciated.
Thanks
Hugo
------------------------------
Date: Tue, 03 Jun 2003 03:04:37 GMT
From: J <imarinejt@hotmail.com>
Subject: Regular expression help
Message-Id: <su3odv047r1fak3tdt5adj63njvl07r8rp@4ax.com>
Hi,
I have the following piece of code:
if ($getreq[$i] =~ /^GET/) {
}
And I get the following error:
Use of uninitialized value in pattern match (m//)
I cannot see what the error is. Any ideas?
Cheers
J
------------------------------
Date: Tue, 03 Jun 2003 03:55:38 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: Regular expression help
Message-Id: <__UCa.33752$ca5.9659@nwrdny02.gnilink.net>
J <imarinejt@hotmail.com> wrote:
>
> if ($getreq[$i] =~ /^GET/) {
>
> }
>
> And I get the following error:
>
> Use of uninitialized value in pattern match (m//)
It's a warning, not an error, and it warns you that
$getreq[$i] was undefined when you used it in the
pattern match.
If there's a problem, it wouldn't be in the code you
posted but in the bit that sets @getreq and $i.
--
Steve
------------------------------
Date: 2 Jun 2003 21:00:42 -0700
From: adans@billiongroup.com (adans)
Subject: Resolve UNIX environment variable within an array
Message-Id: <f3c037ce.0306022000.48c92093@posting.google.com>
I have problem to resolve any UNIX environment variables when the
environment variable is the content of an array. I understood to use
$ENV(env_variable) for resolving the environment variable if it is a
scalar.
------------------------------
Date: Mon, 02 Jun 2003 21:18:28 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: Sorting hash
Message-Id: <3EDBF764.3397A192@hotpop.com>
Gunnar Hjalmarsson wrote:
>
> I'm sorting the keys of a hash by (part of) the hash values like this:
>
> @sortedkeys = sort {
> ($hash{$b} =~ /^([\d.-]+)\|/)[0]
> <=>
> ($hash{$a} =~ /^([\d.-]+)\|/)[0]
> } keys %hash;
>
> The portion of a hash value before the '|' character is a floating
> number that may be negative.
>
> This is about what happens if two values would be identical. Basically
> I don't care in _which_ order the corresponding keys are stored in the
> array, but the sort will be done many times, and I want the keys to be
> stored in _the same_ order each time as long as the hash isn't
> changed.
Then you should stabilize the sort, eg:
@sortedkeys = sort {
($hash{$b} =~ /^([\d.-]+)\|/)[0]
<=>
($hash{$a} =~ /^([\d.-]+)\|/)[0]
or
$a cmp $b
} keys %hash;
Personally, I would prefer to rewrite this sort as an ST:
@sortedkeys = map $$_[0], sort {
$$a[1] <=> $$b[1] or $$a[0] cmp $$b[0]
} map [$_, $hash{$_} =~ /([\d.-]*)/], keys %hash;
This factors out the regex match so it's only done once for each key of
the hash, and makes the sorting itself simpler.
> My question is: Can I trust that the keys will appear in @sortedkeys
> in _the same_ order, even if there would be occurrences of identical
> hash values, or do I need to add another sort criterion to be sure?
If %hash never changes (that is, if the number of buckets in it is
always the same, and if the set of keys is always the same, and the
values associated with those keys is always the same), then the
expression (keys %hash) will always produce the same list.
Although perl5.6.1's sort isn't stable, it won't ever give two different
output lists on two different occasions of sorting a particular
<sortsub, list> tuple. That is, if you do:
my @input = ...;
my @out1 = sort &somesub, @input;
... time passes ...
my @out2 = sort &somesub, @input;
As long as @input is the same, and as long as &somesub is the same, and
if &somesub produces results which are fully determined by $a and $b,
and possibly some *constant* pieces of other data, then @out1 and @out2
will contain the same list.
Note, however, what I said about "If %hash never changes" up above. If
you add keys to %hash (and delete them later) then keys(%hash) might
give a different list. If you do keys(%hash)=10000, then keys(%hash)
may give a different list. If the order of the elements of the list
returned by keys(%hash) changes, then with your original sorting sub,
@sortedkeys *may* contain a different list.
By having "or $a cmp $b" as a last clause of the sortsub, (or in my
case, $$a[0] cmp $$b[0]), we assure that if the *set* of keys of %hash
is the same (regardless of order), the resulting list will have a
definite order.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Tue, 03 Jun 2003 04:19:39 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Sorting hash
Message-Id: <bbh13f$9e7v6$1@ID-184292.news.dfncis.de>
Thanks for your exhaustive reply, Ben. Much appreciated. Nevertheless,
I find it somewhat contradictory.
Benjamin Goldberg wrote:
> Gunnar Hjalmarsson wrote:
>> I don't care in _which_ order the corresponding keys are stored
>> in the array, but the sort will be done many times, and I want
>> the keys to be stored in _the same_ order each time as long as
>> the hash isn't changed.
>
> Then you should stabilize the sort, eg:
<snip>
> If %hash never changes (that is, if the number of buckets in it is
> always the same, and if the set of keys is always the same, and
> the values associated with those keys is always the same), then the
> expression (keys %hash) will always produce the same list.
You seem to be in doubt as regards whether the hash _really_ is
unchanged between the sorts. Let me expand on that by outlining what
the program actually does.
Data is stored in the SDBM database 'data'. The program is executed
multiple times, and the database files are indeed not changed between
the executions. This is the relevant code:
tie %tmphash, 'SDBM_File', 'data', O_RDONLY, 0644 or die $!;
%hash = %tmphash;
untie %tmphash;
# The reason for copying the tied hash to a 'normal' hash is
# that the code includes testing of whether the hash is empty:
#
# unless (%hash) { ... }
#
# and I've found that such a simple test doesn't work on a
# tied hash (i.e. it seems to return an undefined value in
# scalar context even if the hash isn't empty).
@sortedkeys = sort {
($hash{$b} =~ /^([\d.-]+)\|/)[0]
<=>
($hash{$a} =~ /^([\d.-]+)\|/)[0]
} keys %hash;
Accordingly, provided that the OS or the tie function doesn't play a
trick on me, %hash should be identical each time the program is executed.
Considering the above, do you still recommend that the sort is
stabilized, or would such a measure be redundant?
/ Gunnar
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Mon, 02 Jun 2003 21:45:31 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: uninitialized value in eval block?
Message-Id: <3EDBFDBB.E5CD7B38@hotpop.com>
SeeMySig wrote:
>
> In the process of removing all warning violations from my CGI script, I
> find the following gets passed during warningsToBrowser(1):
>
> <!-- warning: Use of uninitialized value in concatenation (.) or
> string at (eval 18) line 32. -->
This means the 32nd line of the 18th string-eval. It does not refer to
either of lines 18 or 32 of your main code.
Is this a FAQ?
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Mon, 02 Jun 2003 21:55:39 -0400
From: Benjamin Goldberg <ben.goldberg@hotpop.com>
Subject: Re: uninitialized value in eval block?
Message-Id: <3EDC001B.15EEC230@hotpop.com>
SeeMySig wrote:
> Tad McClellan writes:
>> SeeMySig wrote:
>>> Tad McClellan writes
>>>> SeeMySig wrote:
>>>>> <!-- warning: Use of uninitialized value in concatenation (.) or
>>>>> string at (eval 18) line 32. -->
>>>>>
> [ Stuff that's not relevant anymore was here... ]
>
> > How is it that you are doing so many evals anyway?
> >
> > Are they all of the "eval BLOCK" form rather than the "eval EXPR"
> > form?
> See below.
> >
> [ And here ]
>
>>> But I have to ask why do most other warnings localise to an actual
>>> line in the script, and so are pretty easy to correct, but this one
>>> does a cryptic 'you-may-find-it-if-you-look-hard-enough'?
>>
>>
>> Because you put it in an eval.
>>
>> You are running perl inside of perl, that makes it "hard(er)".
>
> In fact my cgi script is written as a series of if, elsif, elsif, etc
> statements, each one pointing to a different sub-routine to handle SQL
> processing and subsequent HTML generation (using CGI.pm of course). I
> find this easier as I can follow the logic that way - the HTML
> response depends on the params passed from the web-form, like this:
>
> eval {
> if ($in{'param_1'}) { &sub_routine_1 } # eg param_1 is 'search'
> elsif ($in{'param_2'}) { &sub_routine_2 } # eg param_2 is 'new record'
> etc.
> };
>
> There are about 25 (so far) of these elsif statements inside the eval,
That's an eval BLOCK, not an eval STRING.
When perl said "(eval 18)", it was talking about the 18th eval STRING.
> and that is all there is - the final 'else' is an 'input not
> recognised' message. All of the logic is done inside the sub-routines.
> It was apparently the 18th line of the eval that upset the parser.
No, it was not. It was the 32nd line of the 18th eval STRING, and NOT
the 18th line of your eval BLOCK.
> It seemed a good idea to put the if/elsif statements inside an eval to
> catch any errors, but I am happy to stand corrected on this
> arrangement.
> I'm not sure if this makes it a perl-within-perl arrangement?
When you write normal code (including eval BLOCK), that is perl. When
you put some of your code inside a string, then eval that string, you
are running perl-within-perl.
> Given the architecture of my script, I still do not see why most other
> warnings localise to a specific line in a specific sub-routine, but
> this one points to an elsif statement inside the eval (which like all
> others has a sub-routine associated with it).
It points to the 32nd line within a string which was passed to eval(),
not to the 18th or 32nd line of your eval BLOCK.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
Date: Tue, 03 Jun 2003 02:21:07 GMT
From: Sean O'Dwyer <notspam@spamfree.dud>
Subject: Re: what does KEYS return?
Message-Id: <notspam-B359AC.22210702062003@syrcnyrdrs-03-ge0.nyroc.rr.com>
In article <20030602173849.852$Ra@newsreader.com>, ctcgag@hotmail.com
wrote:
> I'm going to exercise my ESP and say that you have committed this sin
> upon declaration or initiliazation of your variable:
>
> my %legal_user_names={};
>
> Instead of saying
>
> my %legal_user_names=();
&Xho_ESP returns big fat cigar. Your insight is as remarkabale for its
simple brilliance as my error was remarkable for its blistering dullness.
In peace, I have gone merrily, verily.
Sean
------------------------------
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 5066
***************************************