[23489] in Perl-Users-Digest
Perl-Users Digest, Issue: 5702 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 23 11:05:56 2003
Date: Thu, 23 Oct 2003 08:05:09 -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 Thu, 23 Oct 2003 Volume: 10 Number: 5702
Today's topics:
Re: c programmer in need of perl advise (Mike Deskevich)
Re: DBI & primary keys <bart.lateur@pandora.be>
Embedding in C <volleypb@bellsouth.net>
Re: file redirect within back ticks not working <abc@nowhere.com>
Re: help to understand a broken script (Tad McClellan)
How to catch runtime error? (laszlo)
How to use a web broser as GUI <founder@pege.org>
Re: How to use a web broser as GUI <abigail@abigail.nl>
Re: How to use a web broser as GUI <founder@pege.org>
Increment a string and write it to a file after each re (john brown)
Re: Installing DBD::Sybase on MAC (Vijoy Varghese)
Re: Newbie Regex Help <raisin@delete-this-trash.mts.net>
Re: performance arrays vs hashes <bart.lateur@pandora.be>
Re: Perl and IIS - script runs but 'The page cannot be (Tad McClellan)
Re: Perl and IIS - script runs but 'The page cannot be <kuujinbo@hotmail.com>
Regex Help rmurthy60@hotmail.com
Re: Regex Help <bernard.el-haginDODGE_THIS@lido-tech.net>
Re: Regex Help (Greg Bacon)
strange effect with [:lower:] in perl (T. Sander)
Re: while each hash, why not array <abigail@abigail.nl>
Re: while each hash, why not array <abigail@abigail.nl>
Re: while each hash, why not array (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 23 Oct 2003 06:41:56 -0700
From: mikedeskevich@yahoo.com (Mike Deskevich)
Subject: Re: c programmer in need of perl advise
Message-Id: <71734b21.0310230541.52fb772b@posting.google.com>
chance@austin.rr.com wrote in message news:<bn7k1k$frh$1@localhost.localdomain>...
> >> If you knew in advance the length you could do :
> >>
> >> $xvalue[$vector_len - 1] = 0.0
>
>
> > But what if 0.0 is a legal value?
>
> > How will you know that this is the bogus one?
>
> well ..... was planning on writing over the 0.0 with valid data.
>
> All irrelevant if perl does the allocation in a manner which
> garuantees log(N) allocs though. so there went that guess.
>
> >> if the lengths of your vectors really are unknowable, then you could
> >> at least start pre-allocating chunks in advance and doubling the
> >> size each time you 'run out of room',
>
>
>
> > The only way to know is to profile it, until then we're
> > spinning our wheels.
>
> agreed. was just shooting from the hip.
>
> Mr. Orginal poster: if you do actually profile, I would
> be interested to know where the first bottleneck is.
>
> my money is now on finding a sscanf-ish replacement for the
> split statement. But thats just because I haven't thought of
> anything else.
yes, i agree profiling is the best way to find the bottle neck. i'm
new to perl and don't know all the internals yet. are there built
in functions to help in profiling?
thanks!
mike
------------------------------
Date: Thu, 23 Oct 2003 13:08:50 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: DBI & primary keys
Message-Id: <q7kfpv0ck0jfdp6bju4jn4mtmispjmthsh@4ax.com>
Bigus wrote:
>I'm inserting a new record into a MySQL table using Perl DBI. I need to
>retrieve the unique primary key that's created automatically by the db as
>the new record is inserted. Since this will be a multi-user application, and
>two different users could be creating new records in that db within a
>split-second of each other, I'm not sure whether performing a subsequent
>SELECT statement to find out the max number in that column will be
>water-tight enough.
For mySQL, look up mysql_insert_id, both in the DBD::mysql man page and
on mySQL.com's site. As you can see, this value is unique per database
connection (= per $dbh). As long as you ask for it in the same session,
and you haven't inserted (or updated) anything in another table in this
session :), you're safe.
<http://search.cpan.org/~rudy/DBD-mysql-2.9002/lib/DBD/mysql.pm#DATABASE_HANDLES>
<http://www.mysql.com/doc/en/mysql_insert_id.html>
p.s. Why does almost every reply to every question on MySQL here have to
end up with mySql-bashing?
--
Bart.
------------------------------
Date: Thu, 23 Oct 2003 09:40:46 -0400
From: "Patrick Barrett" <volleypb@bellsouth.net>
Subject: Embedding in C
Message-Id: <bn8log$j9r@niven.ksc.nasa.gov>
I have a question I can really use some help on. I am embedding Perl in C
and have run into a problem with dynamically loading modules, such as the
Socket module. I have read through the documentation, but I am still having
problems and can't load the modules. Suggestions please...
thanks.
------------------------------
Date: Thu, 23 Oct 2003 15:01:42 GMT
From: ktom <abc@nowhere.com>
Subject: Re: file redirect within back ticks not working
Message-Id: <q3Slb.1694$rH6.667@twister.austin.rr.com>
Tad McClellan wrote:
> ktom <abc@nowhere.com> wrote:
>
>>from with a perl script this line does not work
>
> ^^^^^^^^^^^^^
>
>>`sed -e 1,14d $file >> /tmp/junk`;
>>
>>it seems the redirect does not work.
>
> ^^^^^^^^^^^^^
>
> What did you expect it to do?
>
> What did it do instead?
the /tmp/junk file is empty...
i print the $file prior to this command and it has the proper value.
copy the sed line to the command line, replace $file with proper file
name it works.
this seem too silly
using the same line within the system function, all data goes to the
screen instead of the file..
putting the xmgrace line within the system function does not change the
results.
see below the actual snippet
`touch /tmp/psFile.out`;
print "Working on..\n";
foreach my $file ( @fileList ) {
print "\t$file\n";
`xmgrace -nosafe $file`;
( $psFile = $file ) =~ s/xgr/ps/ ;
print "$psFile\n";
# system( "sed -e 1,14d $psFile >> /tmp/psFile.out");
`sed -e 1,14d $psFile >> /tmp/psFile.out`;
>
------------------------------
Date: Thu, 23 Oct 2003 08:42:13 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: help to understand a broken script
Message-Id: <slrnbpfmll.73a.tadmc@magna.augustmail.com>
Chad <chussung@operamail.com> wrote:
> I would be gratefull for
> any insight and undestanding as to why it does not work.
Reading the documentation for the functions that you use often
provides insights. :-)
> opendir(DirNameold,"$olddirname") or die "could not open
^ ^
^ ^
> directory:$olddirname";
What's wrong with always quoting "$vars"?
If it fails, wouldn't you want to know _why_ it failed?
If so, then include $! in your diagnostic message.
> while (defined($file = readdir DirNameold )) {
perldoc -f readdir
... you'd better prepend the directory in question
> copy("$file","$newfilename") unless $file eq $newfilename;
copy("$olddirname/$file", "$newdirname/$newfilename") ...
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 23 Oct 2003 06:52:19 -0700
From: GlgAs@Netscape.net (laszlo)
Subject: How to catch runtime error?
Message-Id: <945e4584.0310230552.2fea5b45@posting.google.com>
Consider the following program
$repeat=1000;
for ($i=0; $i<$repeat;$i++) {println();}
if ($@) {print "$@"; die;}
print "done";
sub println {
my $s = shift;
#if (! defined($s)) {$s = "";}
print "$s\n";
if ($@) {print "$@"; die;}
}
If I run it from command mode with -W parameter I get
"Use of unitialized value in concatenation (.) or string in a.txt line
9.
error message for every execution of the sub
However if I use in IIS (WIN2000 Service pack 4; Active State Perl
5.8 806) inside a larger program as part of an
eval({$program})
if ($@) {report the error; die}
statement, the program does not report any error just dies and the
browser (IE 6.0) shows an irrelevant "the page cannot be displayed"
error message.
If I lower thwe value of $repeat to 100 or less, the program finishes
w/o any error message , also if I remove the # comment before the
#if (! defined($s)) {$s = "";}
line, the program runs w/o any problem for reasonable sizes.
In my website environment the program runs, but no error message is
given
http://lzkiss.netfirms.com/cgi-bin/igperl/igp.pl?dir=test&name=test
Any help appreciated
laszlo
------------------------------
Date: Thu, 23 Oct 2003 14:23:56 +0200
From: =?Windows-1252?Q?Roland_M=F6sl?= <founder@pege.org>
Subject: How to use a web broser as GUI
Message-Id: <3f97dd9b$0$21600$91cee783@newsreader01.highway.telekom.at>
I mean not as a client server system
I mean to use Perl direct as scripting language
like javascript
--
Roland Mösl
http://www.pege.org Clear targets for a confused civilization
http://web-design-suite.com Web Design starts at the search engine
------------------------------
Date: 23 Oct 2003 14:19:17 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: How to use a web broser as GUI
Message-Id: <slrnbpfor5.ib0.abigail@alexandra.abigail.nl>
=?Windows-1252?Q?Roland_M=F6sl?= (founder@pege.org) wrote on MMMDCCV
September MCMXCIII in <URL:news:3f97dd9b$0$21600$91cee783@newsreader01.highway.telekom.at>:
// I mean not as a client server system
//
// I mean to use Perl direct as scripting language
// like javascript
It involves an easy and a difficult step.
The easy step in writing a browser that understands inline Perl.
The difficult step is to convince people to use your browser
instead of IE, Netscape or whatever they use now.
Abigail
--
:$:=~s:$":Just$&another$&:;$:=~s:
:Perl$"Hacker$&:;chop$:;print$:#:
------------------------------
Date: Thu, 23 Oct 2003 16:26:01 +0200
From: =?iso-8859-1?Q?Roland_M=F6sl?= <founder@pege.org>
Subject: Re: How to use a web broser as GUI
Message-Id: <3f97e502$0$40842$91cee783@newsreader01.highway.telekom.at>
> It involves an easy and a difficult step.
>
> The easy step in writing a browser that understands inline Perl.
> The difficult step is to convince people to use your browser
> instead of IE, Netscape or whatever they use now.
It's not for web sites,
it's do develop programs to be installed at the clients.
The idea is to use a browser as the GUI
and writing all in Perl
So what browsers have an direct Perl interface
--
Roland Mösl
http://www.pege.org Clear targets for a confused civilization
http://web-design-suite.com Web Design starts at the search engine
------------------------------
Date: 23 Oct 2003 08:01:29 -0700
From: cglobal25@hotmail.com (john brown)
Subject: Increment a string and write it to a file after each result.
Message-Id: <543320e0.0310230701.567b2441@posting.google.com>
This script receives input from the command line. I put in for
example "www.musicdownload.com/song1.mp3". The script looks for the
".mp3" extension and increments the preceeding number to 10. This
works fine, printing the result to the screen. As you can see, I
would like to write the results to a file called "wget_input". Wget
reads from this file and the songs are retrieved. The problem I have
is with writing all of the results to the file, in this
case/www.musicdownload.com/song10.mp3" gets written to the file only.
I know it must be incremented and written after each result, but I
don't know how to go about constructing the loop. Any ideas?
#! /usr/bin/perl -w
#
# mp3_search
#
print "Input path to video archive\n";
$stdinput = <STDIN> ;
chomp ($stdinput);
$path = "$stdinput";
$count = substr ($path, rindex ($path, ".mp3") - 1, 1);
for ($count .. 10)
{ substr ($path, rindex ($path, ".") - 1, 1, $count++); print
"$path\n"; }
open (OUTPUTFILE,">wget_input")|| die "Failed to open
wget_input\n";
printf OUTPUTFILE "$path";
close OUTPUTFILE;
------------------------------
Date: 23 Oct 2003 07:42:50 -0700
From: viijv@thedifferenZ.com (Vijoy Varghese)
Subject: Re: Installing DBD::Sybase on MAC
Message-Id: <4c08aaff.0310230642.242aef29@posting.google.com>
"Michael Peppler" <mpeppler@peppler.org> wrote in message news:<pan.2003.10.22.23.32.18.265420@peppler.org>...
> On Tue, 21 Oct 2003 22:10:05 -0700, Vijoy Varghese wrote:
> No - this FreeBSD on intel - it won't work on your PPC Mac!
Okay, thanks Micheal, any ways, i dumped this open client from sybase,
and went for freebsd.
If your a person looking for a solution for a similar goad (access
mssql from mac), please check these posts...
http://lists.ibiblio.org/pipermail/freetds/2003q4/014109.html
http://lists.ibiblio.org/pipermail/freetds/2003q4/014139.html
HTH,
Regards
Vijoy~
------------------------------
Date: Thu, 23 Oct 2003 08:20:51 -0500
From: Master Web Surfer <raisin@delete-this-trash.mts.net>
Subject: Re: Newbie Regex Help
Message-Id: <MPG.1a0191aab9f2e7179896af@news.mts.net>
[This followup was posted to comp.lang.perl.misc]
In article <bn7pah$2b6$1@ns2.fe.internet.bosch.com>,
jayanth.vs@in.bosch.com says...
> /main/1a_br_1/tmp_fix_2.2.0/14
>
$data = "/main/1a_br_1/tmp_fix_2.2.0/14";
@parts = split(/\//,$data);
$digits = pop @parts;
------------------------------
Date: Thu, 23 Oct 2003 14:14:00 GMT
From: Bart Lateur <bart.lateur@pandora.be>
Subject: Re: performance arrays vs hashes
Message-Id: <gcofpvsfnmacf8f1ikdp460cm0epljrj7a@4ax.com>
Thomas wrote:
>i dont know enough of perl to describe,
>using hashes is better than using arrays...(or isnt so ?)
From your description of yourself I'd think you don't have enough
experience in perl to even worry about this.
Natively, hash access and array access are quite comparable in speed, I
think hash access could be to 30% slower than array access. But anything
you can do to replace hashes by arrays, when hashes are the best tool
for the job, is bound to be *much slower* than just using a hash.
Don't worry about it. Use the data structure that's best for the job at
hand.
--
Bart.
------------------------------
Date: Thu, 23 Oct 2003 08:48:36 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Perl and IIS - script runs but 'The page cannot be displayed'
Message-Id: <slrnbpfn1k.73a.tadmc@magna.augustmail.com>
Alan J. Flavell <flavell@ph.gla.ac.uk> wrote:
> On Thu, 23 Oct 2003, Abigail exposed the fact that:
>> stew dean (stewart@webslave.dircon.co.uk) wrote on MMMDCCV September
>> MCMXCIII in <URL:news:2b68957a.0310230145.498158bc@posting.google.com>:
>> *PLONK*
>
> Well-plonked. :-}
I did that 10 months ago.
Message-ID: <2b68957a.0212220906.3018939e@posting.google.com>
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 23 Oct 2003 23:09:25 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: Perl and IIS - script runs but 'The page cannot be displayed'
Message-Id: <bn8nea$am6$1@pin3.tky.plala.or.jp>
stew dean wrote:
>> jwillmore@myrealbox.com (James Willmore) wrote:
>>What happened when you ran the script at the command line?
>>
>I've never run a perl script at the command line.
>
>>Why not?
>
> Because I've never had to. Why would I want to?
[snip]
> If I run it from the command line it can't find the input file as the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this is probably important and shouldn't be ignored, see below...
> file is relative to the web server. This is all about reading and
> writing files afterall.
Wow, you have managed to ignore James Willmore's advice and (I think)
answer your own question in one stroke :) When you read/write files you
need to be specific(physical vs. relative path) - the fact that its a
CGI script is irrelevant. And since this the problem *seems* to be
related to reading and writing files, try the following:
use strict;
use warnings;
use CGI;
# use CGI::Carp 'fatalsToBrowser';
my $cgi = CGI->new;
print $cgi->header, $cgi->start_html('test page');
print $cgi->p('blah blah blah');
# open(FH, 'c:/NONEXISTENT_TEXT_FILE') or die $!;
# print while (<FH>);
# close(FH) or die $!;
open(FH, 'c:/FILE') or die $!; # make sure 'FILE' exists!!
print while (<FH>);
close(FH) or die $!;
__END__
First, run the script as is and make sure that you substitute 'c:/FILE'
with a file that exists on your system using the *full/absolute* path -
the 'blah blah blah' is displayed as are the file contents.
Second, uncomment the three lines from where you try to open
'c:/NONEXISTENT_TEXT_FILE' (assuming the file doesn't actually exist).
The 'blah blah blah' is displayed, which could lead you to think that
the script is working, but the contents of the file that does exist are
not. This simulates what happened when you ran your script from the
command line - couldn't open the file.
Do you see what's happening? No *syntax* error, but everything *after*
the first open() is ignored because the script dies. Actually, even if
you don't bother to check the return value of open(), everything would
still be ignored. I would guess the 'Done! message/page that referred to
in your original post comes after your attempts to read/write the XML
file(s)?
[snip]
>>Like I mentioned before, maybe the code _is_ broke somewhere.
>>CGI::Carp can report the errors to the browser (since you seem to not
>>want to run the script at the command line). If something is broke,
>>the error will appear.
>
> If something is broke it gives me the error by passing me the
> headers. Will Carp give me any errors that I don't already receive
> (given that I'm not adding debug messages into the script)?
Yes, it would have been easier just to do it this way. Uncomment the
'use CGI::Carp 'fatalsToBrowser';' line and see what happens.
Even if this doesn't exactly solve your problem hopefully, at least, you
can see how valuable running your script from the command line and
CGI::Carp are.
You probably could have spared yourself a lot of grief if you had posted
some code. And, more unfortunately, you have managed to alienate a lot
of people in the group who could provide you with a lot of *good* advice
- that's too bad :(
HTH - keith
------------------------------
Date: 23 Oct 2003 06:45:29 -0700
From: rmurthy60@hotmail.com
Subject: Regex Help
Message-Id: <4da2b83e.0310230545.27f025a4@posting.google.com>
I tried doing the following to remove the ./ from the file listed
below. I am able to do it in sed but the problem is you cannot use
qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
$filist, but if I hard code the file name in place of $filist. It
works.
next if s?\([ /]\)\./?\1?g;
For some reason it is not removing the ./ from the file. Any
suggestions are
welcome.
The file is in this format
a b ./dsfj/dfl/dksl ./ksdfl/dsld
c d ./sds/dsl/dksld ./kdf/ksd/ksdk
Thanks
Raghu
------------------------------
Date: Thu, 23 Oct 2003 13:54:15 +0000 (UTC)
From: "Bernard El-Hagin" <bernard.el-haginDODGE_THIS@lido-tech.net>
Subject: Re: Regex Help
Message-Id: <Xns941DA194F3484elhber1lidotechnet@62.89.127.66>
rmurthy60@hotmail.com wrote in news:4da2b83e.0310230545.27f025a4
@posting.google.com:
> I tried doing the following to remove the ./ from the file listed
> below. I am able to do it in sed but the problem is you cannot use
> qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
> $filist, but if I hard code the file name in place of $filist. It
> works.
>
> next if s?\([ /]\)\./?\1?g;
>
> For some reason it is not removing the ./ from the file. Any
> suggestions are
> welcome.
> The file is in this format
>
> a b ./dsfj/dfl/dksl ./ksdfl/dsld
>
> c d ./sds/dsl/dksld ./kdf/ksd/ksdk
I'm not sure I understand, but why not simply:
s#\./##g;
?
Cheers,
Bernard
------------------------------
Date: Thu, 23 Oct 2003 13:59:43 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Regex Help
Message-Id: <vpfnmfid2imm2f@corp.supernews.com>
In article <4da2b83e.0310230545.27f025a4@posting.google.com>,
<rmurthy60@hotmail.com> wrote:
: I tried doing the following to remove the ./ from the file listed
: below. I am able to do it in sed but the problem is you cannot use
: qx'sed -e"s?\([ /]\)\./?\1?g" $filist'. It does not allow the use of
: $filist, but if I hard code the file name in place of $filist. It
: works.
$ cat try
#! /usr/local/bin/perl
use warnings;
use strict;
while (<DATA>) {
# delete the /g if you only want the first hit
s!(^|\s+)\./!$1!g;
print;
}
__DATA__
a b ./dsfj/dfl/dksl ./ksdfl/dsld
c d ./sds/dsl/dksld ./kdf/ksd/ksdk
./test
$ ./try
a b dsfj/dfl/dksl ksdfl/dsld
c d sds/dsl/dksld kdf/ksd/ksdk
test
Hope this helps,
Greg
--
The urge to save humanity is almost always a false front for the urge to rule.
-- H.L. Mencken
------------------------------
Date: 23 Oct 2003 07:30:34 -0700
From: roetz7@unicum.de (T. Sander)
Subject: strange effect with [:lower:] in perl
Message-Id: <1b27f62d.0310230630.46157fd4@posting.google.com>
I have a strange problem with the following perl code.
It produces the output :
A : dEf
B : dBf
D : DbF
Why is there no output for the case C?
This must be a bug or what is the explanation for this behaviuor?
When I change $c to "DgF" I get the output line for C.
I think the problem always occur for lower when the lower character is the
successor of the upper-case character.
Why this doesn't happen with the same upper variant?
I have tested this with different perl version 5.5, 5.8 on Solaris and Windows.
--------------------
$a="dEf";
$b="dBf";
if (not ($a=~/[:upper:]/)) {
print "A : $a\n";}
if (not ($b=~/[:upper:]/)) {
print "B : $b\n";}
$c="DeF";
$d="DbF";
if (not ($c=~/[:lower:]/)) {
print "C : $c\n";}
if (not ($d=~/[:lower:]/)) {
print "D : $d\n";}
-------------------
------------------------------
Date: 23 Oct 2003 13:29:16 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: while each hash, why not array
Message-Id: <slrnbpfltc.ib0.abigail@alexandra.abigail.nl>
Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMDCCV September
MCMXCIII in <URL:news:bn8gsj$n8a$2@mamenchi.zrz.TU-Berlin.DE>:
() Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
() >
() >
() > Nah, a hypothetical 'each' for arrays would be:
() >
() > while (my ($key, $value) = each @array) {
() > # $key is the index, $value the value.
() > }
()
() That would be my idea too of what "each" should mean for an array.
() However, there are other interpretations. Besides Malcolm's (essentially
() equivalent to "foreach"), there was
()
() while (my ($key, $value) = each @array) {
() # $key is an even-indexed element, $value an odd-indexed element
() }
()
() which is also a plausible generalization of the behavior of "each",
() though, I believe, not a very useful one.
()
() Maybe this ambiguity is another reason why "each" hasn't been generalized.
My guess the reason 'each' hasn't been generalized is that arrays don't
have slots to keep the state of the iterator. Hashes have them. Adding
them to arrays means adding an extra integer to all AVs. Considering that
'each @array' will be used far less than 'each %hash', the price is not
worth paying.
Abigail
--
perl -Mstrict='}); print "Just another Perl Hacker"; ({' -le1
------------------------------
Date: 23 Oct 2003 13:55:18 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: while each hash, why not array
Message-Id: <slrnbpfne5.ib0.abigail@alexandra.abigail.nl>
Greg Bacon (gbacon@hiwaay.net) wrote on MMMDCCV September MCMXCIII in
<URL:news:vpfin2daahu832@corp.supernews.com>:
`` In article <slrnbpfa8c.7uo.abigail@alexandra.abigail.nl>,
`` Abigail <abigail@abigail.nl> wrote:
``
`` : Nah, a hypothetical 'each' for arrays would be:
`` :
`` : while (my ($key, $value) = each @array) {
`` : # $key is the index, $value the value.
`` : }
``
`` Sweet! Something like this?
I thought of that myself, but it doesn't work. The problem is that
memory addresses aren't unique over time; they are reused:
#!/usr/bin/perl
use strict;
use warnings;
use ArrayEach;
my $h = {a => 1, b => 2, c => 3};
my $a = ['a', 'b', 'c'];
print $h, "\n";
print $a, "\n";
my $c;
my $d;
while (my ($k, $v) = each %$h) {
print "HASH: $k -> $v\n";
last if ++ $c >= 2;
}
while (my ($k, $v) = each @$a) {
print "ARRAY: $k -> $v\n";
last if ++ $d >= 2;
}
undef $h;
undef $a;
$a = ['d', 'e', 'f'];
$h = {d => 4, e => 5, f => 6};
print $h, "\n";
print $a, "\n";
while (my ($k, $v) = each %$h) {
print "HASH: $k -> $v\n";
}
while (my ($k, $v) = each @$a) {
print "ARRAY: $k -> $v\n";
}
__END__
HASH(0x8180d60)
ARRAY(0x81913b0)
HASH: c -> 3
HASH: b -> 2
ARRAY: 0 -> a
ARRAY: 1 -> b
HASH(0x8180d60)
ARRAY(0x81913b0)
HASH: e -> 5
HASH: f -> 6
HASH: d -> 4
ARRAY: 2 -> f
The while is only run once, instead of three times.
Abigail
--
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
print } sub __PACKAGE__ { &
print ( __PACKAGE__)} &
__PACKAGE__
( )
------------------------------
Date: 23 Oct 2003 14:45:09 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: while each hash, why not array
Message-Id: <bn8phl$152$1@mamenchi.zrz.TU-Berlin.DE>
Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMDCCV September
> MCMXCIII in <URL:news:bn8gsj$n8a$2@mamenchi.zrz.TU-Berlin.DE>:
> () Abigail <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> () >
> () >
> () > Nah, a hypothetical 'each' for arrays would be:
> () >
> () > while (my ($key, $value) = each @array) {
> () > # $key is the index, $value the value.
> () > }
> ()
> () That would be my idea too of what "each" should mean for an array.
> () However, there are other interpretations. Besides Malcolm's (essentially
> () equivalent to "foreach"), there was
> ()
> () while (my ($key, $value) = each @array) {
> () # $key is an even-indexed element, $value an odd-indexed element
> () }
> ()
> () which is also a plausible generalization of the behavior of "each",
> () though, I believe, not a very useful one.
> ()
> () Maybe this ambiguity is another reason why "each" hasn't been generalized.
>
>
> My guess the reason 'each' hasn't been generalized is that arrays don't
> have slots to keep the state of the iterator. Hashes have them. Adding
> them to arrays means adding an extra integer to all AVs. Considering that
> 'each @array' will be used far less than 'each %hash', the price is not
> worth paying.
Having the iterator incorporated in the hash itself isn't ideal -- it's
the reason why we can't nest "each"- and "keys"-loops over the same hash.
One could also take the opportunity and implement both iterators differently.
Not bloody likely, I know. I wonder how Perl 6 will behave in that respect.
Anno
------------------------------
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 5702
***************************************