[23582] in Perl-Users-Digest
Perl-Users Digest, Issue: 5789 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Nov 12 21:05:49 2003
Date: Wed, 12 Nov 2003 18:05:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 12 Nov 2003 Volume: 10 Number: 5789
Today's topics:
Re: "tree" view of directory (Jay Tilton)
Re: Creating and submitting POST form in Perl <syscjm@gwu.edu>
Re: Installation of a perl module <jwillmore@remove.adelphia.net>
Re: my command (sorry newbe question) (Tad McClellan)
Newbie Question about Hashes (Dennis Russo)
Re: Newbie Question about Hashes (Gary E. Ansok)
Re: Newbie Question about Hashes <invalid-email@rochester.rr.com>
Newbie wants to flock <spammerswelcome@aol.com>
Re: Newbie wants to flock <usenet@morrow.me.uk>
Re: Newbie wants to flock <emschwar@pobox.com>
Re: Newbie wants to flock <usenet@morrow.me.uk>
perl script <jamesbond_422@hotmail.com>
Re: print statement spanning multiple lines <bigiain@mightymedia.com.au>
Re: Problem with IO::Socket::INET and pop <usenet@morrow.me.uk>
Re: Shorthand for($scalar) loops and resetting pos($sca (Tad McClellan)
Re: Shorthand for($scalar) loops and resetting pos($sca <usenet@morrow.me.uk>
Re: Shorthand for($scalar) loops and resetting pos($sca <clint@0lsen.net>
Re: Shorthand for($scalar) loops and resetting pos($sca <pinyaj@rpi.edu>
Re: Shorthand for($scalar) loops and resetting pos($sca <clint@0lsen.net>
Re: Shorthand for($scalar) loops and resetting pos($sca <pinyaj@rpi.edu>
Re: Sorting a multi-dimensional hash (ifiaz)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 12 Nov 2003 23:37:56 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: "tree" view of directory
Message-Id: <3fb2abfe.777054@news.erols.com>
"Jeff Thies" <nospam@nospam.net> wrote:
: I'd like to have a tree like listing of a directories contents and it's
: subdirectories. I'd like to do that with the modules from the standard perl
: distribution.
Is there a compelling reason not to use other modules?
: It doesn't appear that I can do that with File::Find.
How do you figure that?
: I'm also unsure of
: how to sort a readdir so that it lists directories first.
my @files = sort { -d "$dir/$b" <=> -d "$dir/$a" } readdir DIR;
: Surely this (tree view) has been done many times already!
Without a doubt, and some propellerhead has surely put a module to do it
on CPAN. Even if you're reluctant to use a non-standard module, its
license would probably allow you to crib the code into your own program.
: (this doesn't work, it recurses back up and then forever)
The thing that makes writing a recursive subroutine tricky is keeping
the recursive calls from trampling each others' data. This is normally
done by limiting the scopes of variables to the subroutine.
As it is, each invocation of your subroutine is drawing from and
modifying the same global @regular_files and @directories arrays, so one
call ends up re-doing everything that was done by the previous.
Your code comes within inches of its goal. Just uncomment the line
declaring those arrays and shift it up by one line.
: sub readADir{
: my $dir=shift;
: print '<ul>';
:
: opendir DIR, $dir or die "Cannot open dir $!";
: my @files = readdir DIR;
: closedir DIR;
my( @regular_files, @directories );
: foreach my $file(@files){
: # my @regular_files=();my @directories=();
: if(-d "$dir/$file"){unless($file=~/^\./){push @directories, "$dir/$file"}}
: if(-f "$dir/$file"){push @regular_files, $file;}
: }
:
: @directories=sort @directories;
: foreach my $directory(@directories){
: if($dir ne $directory){readADir($directory)};
: }
:
: @regular_files=sort @regular_files;
: foreach my $regular_file(@regular_files){
: print qq{<li> $regular_file </li>};
: }
: print '</ul>';
: }
------------------------------
Date: Wed, 12 Nov 2003 18:10:30 -0500
From: Chris Mattern <syscjm@gwu.edu>
Subject: Re: Creating and submitting POST form in Perl
Message-Id: <3FB2BDE6.9010300@gwu.edu>
Please don't top-post. That makes life easier for everybody.
Thank you.
J.P. wrote:
> Mr. McClellan,
>
> Maybe you should attempt to be a bit less high and mighty...and
> actually help eliminate problems rather than adding to them.
If you had read his answer instead of being offended that he didn't
bother to stoke your self-esteem, you would have noticed that he *did*
answer your question. Even better, he told you how you can answer
such questions for yourself in the future.
>
> As I said, I scoured the area (TO INCLUDE my hard drive)...and I have
> NOT found an answer yet.
Because you didn't know how to look. Tad told you how to look.
> Although I can do the auto-submission, I
> still haven't created the request. I don't want to display a
> page...just create a request with all my info and POST it forward.
>
> I think that the Perl world could use a lot fewer "self-righteous"
> people...and a few more truly helpful individuals.
There are a lot of truly helpful individuals, including Tad. Unfortunately,
you probably won't be able to ask them for help any more, because you just
got listed in a lot of killfiles.
>
> Just remember: when you were born, you didn't know Perl. Other
> newbies are trying to get to where you are at, skill-wise. Why don't
> you help repay what others before you helped you to become.
They did. Tad told you how to find answers for yourself. You get
where the experts are not by knowing all the answers but by knowing
how to get the answers.
Chris Mattern
------------------------------
Date: Thu, 13 Nov 2003 01:30:29 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Installation of a perl module
Message-Id: <20031112202807.2e4f9135.jwillmore@remove.adelphia.net>
On 12 Nov 2003 02:35:43 -0800
ranjithvenkatesh@hotmail.com (mistletoe) wrote:
> Command : ppm install DBI.ppd
>
> Error: No valid repositories: Error: 500 Can't connect to
> ppm.ActiveState.com:80 (connect: Unknown error) at C:/Program
> Files/Perl/site/lib/PPM/Repository.pm line 84 Error: 500 Can't
> connect to ppm.ActiveState.com:80 (connect: Unknown error) at
> C:/Program Files/Perl/site/lib/PPM/Repository.pm line 84
>
> Line 84: "my $soap_result = $client->ppm_protocol;"
>
> How do I correct this error?
Check the ActiveState support site. That's where I'd check.
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
"I wouldn't recommend sex, drugs or insanity for everyone, but
<they've always worked for me." -- Hunter S. Thompson
------------------------------
Date: Wed, 12 Nov 2003 17:32:37 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: my command (sorry newbe question)
Message-Id: <slrnbr5gol.jou.tadmc@magna.augustmail.com>
Chris Mattern <syscjm@gwu.edu> wrote:
> Making your variables local when possible is always a good idea.
Except we can't use the usual meaning of "local" because of its
_un_usual meaning in the Perl world.
In Perl, local() variables are *global* variables!
(it is the variable's _value_ that is "local")
Restricting the scope of variables when possible is always a good idea.
:-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 12 Nov 2003 16:56:12 -0800
From: denrusso@yahoo.com (Dennis Russo)
Subject: Newbie Question about Hashes
Message-Id: <b3bec8d2.0311121656.369213e7@posting.google.com>
A little new to perl (and maybe hashes). I have two seperate arrays
that are in order, and I would like to create a hash table to combine
the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
following code does not work (it only prints two blannk lines):
%hash = ();
for($i=0; $i<10; $i++) {
$a[$i]=$i;
}
for($i=10; $i<20; $i++) {
$b[$i]=$i;
}
$i=0;
foreach (@a) {
$hash{$_}=$b[$i];
$i++;
}
print "$hash{$a[0]}\n";
print "$hash{$a[1]}\n";
I am expecting the following output:
10
11
Any thoughts or inputs would be greatly appreciated!!
Thanks,
dr
------------------------------
Date: Thu, 13 Nov 2003 01:15:59 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: Newbie Question about Hashes
Message-Id: <boum0e$jeq$1@naig.caltech.edu>
In article <b3bec8d2.0311121656.369213e7@posting.google.com>,
Dennis Russo <denrusso@yahoo.com> wrote:
>A little new to perl (and maybe hashes). I have two seperate arrays
>that are in order, and I would like to create a hash table to combine
>the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
>following code does not work (it only prints two blannk lines):
>
I would add
use strict;
use warnings;
here, even for a short script like this.
>%hash = ();
>
>for($i=0; $i<10; $i++) {
> $a[$i]=$i;
>}
Nothing really wrong with this but it would be more commonly written as
for my $i (0..9) {
$a[$i] = $i;
}
or even
$a[$_] = $_ foreach (0..9);
>for($i=10; $i<20; $i++) {
> $b[$i]=$i;
>}
Same comments apply here.
>$i=0;
>foreach (@a) {
> $hash{$_}=$b[$i];
> $i++;
>}
Here's your problem -- the first time through the loop,
$_ is $a[0], which is 0, and $i is 0. So the assignment
works out to
$hash{0}=$b[0]
but you didn't assign a value to $b[0], only to $b[10] through $b[19],
so $hash{0} gets the undefined value. Perhaps that first line in
this clip should be "$i = 10"?
>print "$hash{$a[0]}\n";
>print "$hash{$a[1]}\n";
These do indeed work out to $hash{0} and $hash{1}, but as I
mentioned before, these don't have a defined value. If you'd
had "use warnings" or -w enabled, you would have gotten a message
about an undefined value (whether that would help in this case
I couldn't say). So just the newline is printed.
Gary
--
'Normal' is just a setting on the washing machine.
------------------------------
Date: Thu, 13 Nov 2003 01:58:43 GMT
From: Bob Walton <invalid-email@rochester.rr.com>
Subject: Re: Newbie Question about Hashes
Message-Id: <3FB2E4FF.7000201@rochester.rr.com>
Dennis Russo wrote:
> A little new to perl (and maybe hashes). I have two seperate arrays
> that are in order, and I would like to create a hash table to combine
> the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
> following code does not work (it only prints two blannk lines):
>
> %hash = ();
>
> for($i=0; $i<10; $i++) {
> $a[$i]=$i;
> }
>
> for($i=10; $i<20; $i++) {
> $b[$i]=$i;
-------^^
That should probably be $b[$i-10]=$i; -- as written, you are defining
$b[10] through $b[19], which probably isn't what you really wanted.
> }
>
> $i=0;
> foreach (@a) {
> $hash{$_}=$b[$i];
Note that $b[0] through $b[9] are reference here, and have undef as
their value.
> $i++;
> }
>
> print "$hash{$a[0]}\n";
> print "$hash{$a[1]}\n";
>
> I am expecting the following output:
> 10
> 11
>
> Any thoughts or inputs would be greatly appreciated!!
>
>
> Thanks,
> dr
>
Why don't you do:
use warnings;
use strict;
my %hash = ();
my @a=(0..9);
my @b=(10..19);
@hash{@a}=@b;
use Data::Dumper;
print Dumper(\%hash);
That's a lot more Perlish and it also works.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Thu, 13 Nov 2003 00:36:39 -0000
From: "Dick Rosser" <spammerswelcome@aol.com>
Subject: Newbie wants to flock
Message-Id: <DmAsb.402$535.715497@newsfep1-win.server.ntli.net>
Greetings
My perl abilities are limited - to put it mildly!
I have obtained a flat-file database search script
that fills my needs perfectly, but from what I remember
in my old Paradox days it does not seem to have a
table locking mechanism.
Does the simple addition of $flock = 1; do the trick or
does it need something a little more sophisticated.
Server is Unix. Which of these is correct:
$flock = 1;
$flock = "1"; or are they both as good?
Regards
Dick Rosser
webmasterone@kidwatch-uk.net
------------------------------
Date: Thu, 13 Nov 2003 01:47:30 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Newbie wants to flock
Message-Id: <bounri$eni$1@wisteria.csv.warwick.ac.uk>
"Dick Rosser" <spammerswelcome@aol.com> wrote:
> My perl abilities are limited - to put it mildly!
> I have obtained a flat-file database search script
> that fills my needs perfectly, but from what I remember
> in my old Paradox days it does not seem to have a
> table locking mechanism.
>
> Does the simple addition of $flock = 1; do the trick or
> does it need something a little more sophisticated.
> Server is Unix. Which of these is correct:
> $flock = 1;
> $flock = "1"; or are they both as good?
Neither. Both simply assign a value to the variable $flock.
How you do locking depends on what you are trying to lock. If by a
'table locking mechanism' you mean a lock on an entire file, and this
script (or other Perl scripts you write yourself) are the only things
reading or writing that file, then you want to read 'perldoc -f
flock'.
If, OTOH, there are other programs reading or writing this file that
you need to synchronise with, you need to know how they do their
locking: the locks Perl sets with flock() are advisory only, so they
won't lock out anyone who doesn't cooperate. When you know what sort
of locking you are trying to use, ask again and someone can tell you
how to do it in Perl.
Ben
--
EAT
KIDS (...er, whoops...)
FOR ben@morrow.me.uk
99p
------------------------------
Date: Wed, 12 Nov 2003 18:48:21 -0700
From: Eric Schwartz <emschwar@pobox.com>
Subject: Re: Newbie wants to flock
Message-Id: <eto4qx982e2.fsf@fc.hp.com>
"Dick Rosser" <spammerswelcome@aol.com> writes:
> My perl abilities are limited - to put it mildly!
> I have obtained a flat-file database search script
> that fills my needs perfectly, but from what I remember
> in my old Paradox days it does not seem to have a
> table locking mechanism.
Does it need one? I'm all for locking everywhere it's needed, but you
should only lock when you have multiple processes updating your
database at the same time. A query probably won't update a database
(unless, I suppose, you're doing something nifty like tracking the top
5 most popular searches, or some such), so ideally the only process
writing to your table will be the part that scans your site and
updates the flat file for you.
> Does the simple addition of $flock = 1; do the trick or
> does it need something a little more sophisticated.
> Server is Unix. Which of these is correct:
> $flock = 1;
> $flock = "1"; or are they both as good?
They're equivalent in both function and form. That is to say, all
they do is set the scalar variable '$flock' to 1. Or "1", as Perl
automagically converts between numeric and string context for you.
As I'm sure you can surmise, setting a variable will not, by itself,
cause any locking to be done. It is possible that your script,
which no-one here has probably ever seen, does something like:
flock(FH, LOCK_EX) if $flock;
But then again, it may not. You should be able to tell if your
program calls flock() though-- just grep for 'flock' in the code, and
that should help you figure it out. To learn more about locking
files, and how and why, see 'perldoc -q lock' and 'perldoc -f flock'
-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
------------------------------
Date: Thu, 13 Nov 2003 01:53:56 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Newbie wants to flock
Message-Id: <bouo7k$eni$2@wisteria.csv.warwick.ac.uk>
Eric Schwartz <emschwar@pobox.com> wrote:
> "Dick Rosser" <spammerswelcome@aol.com> writes:
> > My perl abilities are limited - to put it mildly!
> > I have obtained a flat-file database search script
> > that fills my needs perfectly, but from what I remember
> > in my old Paradox days it does not seem to have a
> > table locking mechanism.
>
> Does it need one? I'm all for locking everywhere it's needed, but you
> should only lock when you have multiple processes updating your
> database at the same time. A query probably won't update a database
> (unless, I suppose, you're doing something nifty like tracking the top
> 5 most popular searches, or some such), so ideally the only process
> writing to your table will be the part that scans your site and
> updates the flat file for you.
(I note you assume this is in a CGI context :)
You need locking whenever one process might be reading at the same
time as another is writing. Even if there is only one update process,
all the queries need to be locked out while that update is going on or
the database will be in an inconsistent state and the query will
(probably) fail. This means everyone needs to lock: readers need to
LOCK_SH, and writers need to LOCK_EX.
Ben
--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." - C.S.Lewis
------------------------------
Date: Thu, 13 Nov 2003 00:27:33 +0000 (UTC)
From: "Jerry Maguire" <jamesbond_422@hotmail.com>
Subject: perl script
Message-Id: <bouj5l$mdc$1@hercules.btinternet.com>
Hi,
I want to extract a tomcat log file with XML packets into a csv file. I have
concenated all the log files into one and grep all the xml packets.
Now i have to exttracted only selected xml values and save it in a csv file
format.
Can you please help me with extractingxml values using perl?
Thanks
jerry
------------------------------
Date: Thu, 13 Nov 2003 13:03:20 +1100
From: Iain Chalmers <bigiain@mightymedia.com.au>
Subject: Re: print statement spanning multiple lines
Message-Id: <bigiain-636E8C.13032013112003@news.fu-berlin.de>
In article <FEisb.7553$aT.2479@news-server.bigpond.net.au>,
"Voitec" <voitec@zzzzzzzzz.com> wrote:
> *snip*
> > No, I believe the first example is what the OP wanted. He wants to
> > indent the code, not the output.
> >
> > Thus;
> >
> > print "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
> > "bbbbbbbbbbbbbbbbbbbbbb";
> >
> > would indeed seem to do the trick.
> >
> > Z
>
> Thanks. I guess I could have been a little clearer, sorry.
> But the intention was to indent the code not the output so the comma
> character is exactly what I was looking for.
Keep in mind that while both the comma and the dot will work, they're
doing slightly different things... The dot is concatenating all the
separate quoted strings together into one big string then printing it,
while the comma is treating each quoted string as a "list item" and
joining them with the current value of $,
see perldoc -f print
big
--
'When I first met Katho, she had a meat cleaver in one hand and
half a sheep in the other. "Come in", she says, "Hammo's not here.
I hope you like meat.' Sharkey in aus.moto
------------------------------
Date: Wed, 12 Nov 2003 23:12:05 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Problem with IO::Socket::INET and pop
Message-Id: <boueo5$9cp$1@wisteria.csv.warwick.ac.uk>
unixverse@yahoo.com (Joe) wrote:
> # Connect to the POP server using port 110
> $SocketHandle = IO::Socket::INET->new (Proto=>"tcp",
> PeerAddr=>$ServerName, PeerPort=>$Port);
>
> if (!$SocketHandle)
> {
> # Customized Error Function
> Error("cannot connect to port <B>$Port</B> on <B>$ServerName</B>",
> "Back");
>
> exit(0);
> }
>
> The web hosting company keeps telling me that they didn't change
> anything and that they can connect fine via telnet ServerName 110.
>
> Was this code correct to begin with? Maybe I used some functionality
> that has been deprecated since? Is there a way to better debug and
> find out why !$SocketHandle is returned?
You need to include $! in the error message so you (and we) have a
better idea of what's going wrong.
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| ben@morrow.me.uk
------------------------------
Date: Wed, 12 Nov 2003 17:38:58 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <slrnbr5h4i.jou.tadmc@magna.augustmail.com>
Clint Olsen <clint@0lsen.net> wrote:
> However, I've noticed a technique in Parse::Yapp which does the following:
>
> LOOP: for ($scalar) {
>
> /whitespace/ and next LOOP;
> /pattern/ and this;
> /patern1/ and that;
> }
>
> What exactly is the for() loop doing that's different than $_ = $scalar?
Nothing.
It just saves you from typing a lot of "$scalar =~" thingies.
ie:
$scalar =~ /whitespace/ and next LOOP;
$scalar =~ /pattern/ and this;
$scalar =~ /patern1/ and that;
> Generally a for loop consists of an initialization, a
> test, and an optional chunk to do at the end of the code BLOCK.
The "for" and "foreach" keywords are interchangeable in Perl,
so the above is the same as:
LOOP: foreach ($scalar) {
only with less typing.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 12 Nov 2003 23:25:19 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <boufgv$9sq$1@wisteria.csv.warwick.ac.uk>
Clint Olsen <clint@0lsen.net> wrote:
> LOOP: for ($scalar) {
>
> /whitespace/ and next LOOP;
> /pattern/ and this;
> /patern1/ and that;
> }
>
> What exactly is the for() loop doing that's different than $_ = $scalar?
1. Giving you a loop to 'next' to,
2. localising $_,
3. aliasing $_ rather than simply assigning it: ie. modifying $_ in
the loop will modify $scalar outside it.
The loop could be rewritten
LOOP: {
local *_ = \$scalar;
...
}
but that's a little *too* incomprehensible, even for me :).
This is quite a common idiom in Perl: it's sort-of like the 'with'
keyword in VB et al.
> The documentation is pretty scant on this subject - conditions under which
> pos() will be reset.
From perlop:
| A failed match normally resets the search position to the beginning
| of the string, but you can avoid that by adding the "/c" modifier
| (e.g. "m//gc"). Modifying the target string also resets the search
| position.
> Generally a for loop consists of an initialization, a test, and an
> optional chunk to do at the end of the code BLOCK.
That is a C-style for() loop, which is not much used in Perl and is
actually a while() loop in disguise :). The for() loop above is the
*other* type of for() loop, which some people call a 'foreach' loop to
disambiguate.
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| ben@morrow.me.uk
------------------------------
Date: Wed, 12 Nov 2003 23:34:29 GMT
From: Clint Olsen <clint@0lsen.net>
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <slrnbr5gs5.vd4.clint@poly.0lsen.net>
On 2003-11-12, Ben Morrow <usenet@morrow.me.uk> wrote:
>
> 1. Giving you a loop to 'next' to,
Ok, makes sense. Although you don't really need the for keyword to do
that, as you have shown in later code.
> 2. localising $_,
I've never used local variables. The documentation says you should
generally avoid them in favor of lexically scoped variables.
> 3. aliasing $_ rather than simply assigning it: ie. modifying $_ in
> the loop will modify $scalar outside it.
How is that different than references? Sorry if my globbing/aliasing
knowledge is a bit sparse. I didn't start using Perl until version 5.X.
> The loop could be rewritten
>
> LOOP: {
> local *_ = \$scalar;
> ...
You don't mean $_?
>| A failed match normally resets the search position to the beginning
>| of the string, but you can avoid that by adding the "/c" modifier
>| (e.g. "m//gc"). Modifying the target string also resets the search
>| position.
Right, but it totally leaves out the case of when assigning a reference to
that scalar loses info or this "local" construct which appears to preserve
pos(). Obviously when you have a copy of a scalar, that scalar's pos()
should be 0 (or undef), but the conditions under which a copy is made is
obviously clouded in ths funky for() loop.
> That is a C-style for() loop, which is not much used in Perl and is
> actually a while() loop in disguise :). The for() loop above is the
> *other* type of for() loop, which some people call a 'foreach' loop to
> disambiguate.
Yeah, it's as clear as mud :) I read page on perlsyn, and the for loop
does mention some similarity between for and foreach and while, but it
doesn't go any further than that.
-Clint
------------------------------
Date: Wed, 12 Nov 2003 18:55:55 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Clint Olsen <clint@0lsen.net>
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <Pine.SGI.3.96.1031112183829.131408A-100000@vcmr-64.server.rpi.edu>
[posted & mailed (maybe?)]
On Wed, 12 Nov 2003, Clint Olsen wrote:
>On 2003-11-12, Ben Morrow <usenet@morrow.me.uk> wrote:
>>
>> 2. localising $_,
>
>I've never used local variables. The documentation says you should
>generally avoid them in favor of lexically scoped variables.
Yes, but there are some variables that CANNOT be lexically scoped; these
are generally punctuation variables, like $_ and @_ and $/.
>> 3. aliasing $_ rather than simply assigning it: ie. modifying $_ in
>> the loop will modify $scalar outside it.
>
>How is that different than references? Sorry if my globbing/aliasing
>knowledge is a bit sparse. I didn't start using Perl until version 5.X.
An alias is another name for a variable, whereas a reference is another
name for the LOCATION of a variable.
$foo = 'blah';
@foo = qw( fee fie foe fum );
*bar = \@foo;
$bar = 'boo';
print "$foo $bar[2]"; # blah foe
The second line makes @bar an alias for @foo; that is, any changes to @foo
change @bar, and vice-versa. If we had written
*bar = \*foo;
instead, then the print() line would have read 'boo foe', because ALL
'bar' variables would be aliases for ALL 'foo' variables. What's
important to realize here is that, because of the use of glob notation,
'bar' here is a global variable; it cannot be lexical, because lexical
variables are not related to each other -- they have no concept of a glob.
>> LOOP: {
>> local *_ = \$scalar;
>> ...
>
>You don't mean $_?
No, that would make $_ a REFERENCE to $scalar. This way makes it an alias
to $scalar. See above.
>>| A failed match normally resets the search position to the beginning
>>| of the string, but you can avoid that by adding the "/c" modifier
>>| (e.g. "m//gc"). Modifying the target string also resets the search
>>| position.
>
>Right, but it totally leaves out the case of when assigning a reference to
>that scalar loses info or this "local" construct which appears to preserve
>pos(). Obviously when you have a copy of a scalar, that scalar's pos()
>should be 0 (or undef), but the conditions under which a copy is made is
>obviously clouded in ths funky for() loop.
It's not funky, it's a Perl-style loop, also known as 'foreach'. I don't
know what you mean, though:
$x = "jeff";
pos($x) = 2;
for ($x) {
print pos($x), " " , pos($_), "\n"; # 2 2
}
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: Thu, 13 Nov 2003 00:29:48 GMT
From: Clint Olsen <clint@0lsen.net>
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <slrnbr5k3s.vd4.clint@poly.0lsen.net>
I did get your email as well. Was there a problem initially sending it?
On 2003-11-12, Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote:
>
> Yes, but there are some variables that CANNOT be lexically scoped; these
> are generally punctuation variables, like $_ and @_ and $/.
Right. I never tried to make them lexically scoped.
> $foo = 'blah';
> @foo = qw( fee fie foe fum );
> *bar = \@foo;
> $bar = 'boo';
>
> print "$foo $bar[2]"; # blah foe
>
> If we had written
>
> *bar = \*foo;
>
> instead, then the print() line would have read 'boo foe', because ALL
> 'bar' variables would be aliases for ALL 'foo' variables.
[some snipping]
Ahh, this is an excellent explanation, thanks.
> No, that would make $_ a REFERENCE to $scalar. This way makes it an alias
> to $scalar. See above.
Ok, makes much more sense now.
> It's not funky, it's a Perl-style loop, also known as 'foreach'. I don't
> know what you mean, though:
>
> $x = "jeff";
> pos($x) = 2;
> for ($x) {
> print pos($x), " " , pos($_), "\n"; # 2 2
> }
What I meant was it wasn't entirely obvious how $_ is being set in the
for() loop, and this is the key to preserving pos() for $x in and out of
the loop. Any attempt to assign $_ without using globbing wouldn't have
worked, right?
Thanks,
-Clint
------------------------------
Date: Wed, 12 Nov 2003 19:50:46 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Clint Olsen <clint@0lsen.net>
Subject: Re: Shorthand for($scalar) loops and resetting pos($scalar)
Message-Id: <Pine.SGI.3.96.1031112195003.131903B-100000@vcmr-64.server.rpi.edu>
[posted & mailed]
On Thu, 13 Nov 2003, Clint Olsen wrote:
>I did get your email as well. Was there a problem initially sending it?
I couldn't be sure if the email address was valid (and was too lazy to
check out olsen.net).
>What I meant was it wasn't entirely obvious how $_ is being set in the
>for() loop, and this is the key to preserving pos() for $x in and out of
>the loop. Any attempt to assign $_ without using globbing wouldn't have
>worked, right?
Right; just copying the contents of $x to $y doesn't transfer $x's pos()
to $y.
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: 12 Nov 2003 17:14:30 -0800
From: ifiaz@hotmail.com (ifiaz)
Subject: Re: Sorting a multi-dimensional hash
Message-Id: <93c1947c.0311121714.5b9b239c@posting.google.com>
> > > sort { $Fnd{$a}{$Cf}{mFullScanTime} <=> $Fnd{$b}{$Cf}{mFullScanTime} }
> > > keys %Fnd;
> > >
> >
>
> The operative term here is "put together". Apparently, every hash
> entry can have multiple mFullScanTime's. The question is, how are
> you going to use these for sorting? Pick one? If so, which one?
> Use the average? Something else?
>
> Without an answer to this there isn't much I can suggest.
>
With help from your previous posting and a bit of trial and error
(actually several trials and errors), I managed to do the sorting.
Here it goes:
foreach $tmpLic (sort keys %Fnd) {
...
foreach $tmpCount (sort byScanTime keys %{$Fnd{$tmpLic}}) {
...
...
write;
}
}
##sort routine
sub byScanTime() {
$Fnd{$tmpLic}{$a}{mFullScanTime} <=>
$Fnd{$tmpLic}{$b}{mFullScanTime}
}
__END__
Although, I don't understand completely how it works, but I guess
I will manage to do the same with trial and error, next time I face it in a
different scenario.
Thank you.
------------------------------
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 5789
***************************************