[6546] in Perl-Users-Digest
Perl-Users Digest, Issue: 171 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 24 22:08:19 1997
Date: Mon, 24 Mar 97 19:00:24 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 24 Mar 1997 Volume: 8 Number: 171
Today's topics:
Re: "guaranteed" exit routine? (Ron Mayer)
Re: A Call for Clarity (Schwartzian Transform Considere (Abigail)
Re: ADVICE NEEDED: timelocal() prints wrong result (Simon Hyde (aka Jeckyll))
Re: Calling Perl Script from ServerSide Java applets??? (Nathan V. Patwardhan)
Re: Can someone explain this behaviour? <vladimir@cs.ualberta.ca>
Re: Can someone explain this behaviour? (Simon Hyde (aka Jeckyll))
Re: Can someone explain this behaviour? <dbenhur@egames.com>
Re: Can someone explain this behaviour? <eryq@enteract.com>
Re: Casting in Perl <merlyn@stonehenge.com>
Re: Creating arrays... (Simon Hyde (aka Jeckyll))
Re: Executing a script??? <gabriele@clotho.com>
Re: foreach loop and printing (Simon Hyde (aka Jeckyll))
Re: help: 2d perl array doesn't work! <gabriele@clotho.com>
Is there a better way to do this? (Dirk Bergstrom)
Re: Little beginers question (Simon Hyde (aka Jeckyll))
Re: Little beginers question <gabriele@clotho.com>
PARADOX <lewis@nexusint.com>
Re: Perl training in Australia? <tchrist@mox.perl.com>
Re: Posting to Newsgroups from Perl Script (Nathan V. Patwardhan)
Problems loading Socket module <rod@cs.colgate.edu>
Re: Problems loading Socket module (Nathan V. Patwardhan)
Re: term 'regular expressions' considered undesirable <vladimir@cs.ualberta.ca>
Re: term 'regular expressions' considered undesirable <vladimir@cs.ualberta.ca>
Re: term 'regular expressions' considered undesirable (Jeffrey)
Re: term 'regular expressions' considered undesirable (Jeffrey)
two perl4->perl5 gotchas not in perltrap manpage? (Dave Steiner)
undef in Arguments to Subroutines <neiled@enteract.com>
What's a bad Perl book? (revjack@radix.net)
Re: What's a good Perl book? <WSpencer@storm.sparks.nv.us>
When reversed<> used What is happening? (Geoffrey Hebert)
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 24 Mar 1997 17:03:22 -0800
From: mayer@mpactinc.com (Ron Mayer)
Subject: Re: "guaranteed" exit routine?
Message-Id: <wy4te0srz9.fsf@tonga.mpactinc.com>
Tom Phoenix <rootbeer@teleport.com>:
>On 22 Mar 1997, Rich Schramm wrote:
>
>> I was told by someone that perl has a facility that will allow me to run
>> a an exit routine, even if perl were to abort abnormally.
>
>They lied. :-) If Perl dumps core, if your process is the target of 'kill
>-9', if smoke comes out of the rear vents, all bets are off.
>
>But if you define code in an END block, Perl promises to _try_ [...]
This:
#!/usr/local/bin/perl
sub whatever {print STDERR "Hi\n"; kill(9,$$); print STDERR "There\n";}
sub cleanup {print STDERR "Bye\n";}
whatever unless (fork);
cleanup;
could be used to clean up some things; but obviously wouldn't
know the state things set up by &whatever();
Ron
------------------------------
Date: Tue, 25 Mar 1997 01:38:15 GMT
From: abigail@ny.fnx.com (Abigail)
Subject: Re: A Call for Clarity (Schwartzian Transform Considered Scary)
Message-Id: <E7KsJr.Mys@nonexistent.com>
On Mon, 24 Mar 1997 09:21:40 -0600, arw@dante.mh.lucent.com wrote in comp.lang.perl.misc:
++ In article <5h5jbq$6n7$1@jupiter.sdd.cegelecproj.co.uk>,
++ Steve_Kilbane@cegelecproj.co.uk wrote:
++ > The ST is a little dodgy, because it's quite large - if you're reading
++ > someone else's code, you'd have to check that it *does* do ST, rather
++ > than saying "map...sort..map..uh, ST" - the maps could be there for
++ > other purposes. For that reason, I'd say "comment it". Say it's an ST,
++ > and what it's sorting on, and give some pointer or explanation for the
++ > ST itself. But that's not a reason for dumbing it down.
++
++ No. Don't dumb it down. Heaven forbid! Implement it ONCE
++ as a higher order function and be done with it.
++
++ SortedListByMetric = SchwartzianTransform(List, Metric)
As in:
sub st_sort ($@) {
my $metric = shift;
map {$_ -> [0];}
sort {$a -> [1] <=> $b -> [1];}
map {[$_, &{$metric} ($_)];} @_;
}
my @list = st_sort sub {$_ [0] =~ /(\d+)/; $1;},
qw (foo23 bar345foo 49 73xyxxy yes1);
But there are many variations on the ST. This one uses just one metric,
mapping to numbers. However, other variants compare mapped values, and
if they are equal compare the unmapped values, or variants using cmp in
stead of <=>, those mapping to two, or three different metrics etc.
Surely, we can capture all that by passing the sort routine as an
anonymous subfunction as well, but then we haven't gained anything on
speed, readability, or characters to type.
Abigail
------------------------------
Date: Tue, 25 Mar 1997 00:17:36 GMT
From: shyde@poboxes.com (Simon Hyde (aka Jeckyll))
Subject: Re: ADVICE NEEDED: timelocal() prints wrong result
Message-Id: <333b14db.28670623@news.uni-stuttgart.de>
On Mon, 24 Mar 1997 05:38:19 GMT, dusanb@syd.csa.com.au (VK2COT)
wrote:
>Hello,
>
>I admit I might be over-streched atd exhausted at the present time. I hope
>I am not asking a question that has obvious answer.
*snip*
Sorry, i'm arfraid you are :-)
>#!/opt/local/bin/perl
>use Time::Local;
>$AAA = timelocal(37, 53, 04, 1, Mar, 97);
>$BBB = timelocal(37, 53, 22, 27, Feb, 97);
timelocal takes numerical parameters, not strings, once passed to
timelocal the strings you supply for the months are simply converted
to 0 (since they contain no numbers) which is January, you need to do
something like:
@months = ('jan', 'feb', 'mar', 'apr','may','jun','jul','aug', 'sep',
'oct', 'nov', 'dec');
foreach(0..$#months){
$MONTHS{"\U$months[$_]"} = $_;
$MONTHS{"\u\L$months[$_]"} = $_;
$MONTHS{"\L$months[$_]"} = $_;
}
$AAA = timelocal(37, 53, 04, 1, $MONTHS{'Mar'}, 97);
$BBB = timelocal(37, 53, 22, 27, $MONTHS{'Feb'}, 97);
This should work just fine :-)
---
Yours Sincerely,
,
() o /| | |
/\ _ _ _ __ _ _ |___| __| _
/ \| / |/ |/ | / \_/ |/ | | |\| | / | |/
/(__/|_/ | | |_/\__/ | |_/ | |/ \_/|/\_/|_/|__/
/|
\|
(Simon Hyde)
------------------------------
Date: 25 Mar 1997 01:43:33 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: Calling Perl Script from ServerSide Java applets???????
Message-Id: <5h7ak5$bjt@fridge-nf0.shore.net>
Michael Shannon (mshannon@lds.com) wrote:
: I have written a Server side Java applet for the Netscape Environment. I
: had to do this because I need to invoke a client/server Java application
[snip]
Woah! Sounds like a job for comp.infosystems.www.authoring.cgi or
even one of the Java groups.
A quick search through Dejanews might help you with an answer, because
I recall having seen questions like this before.
--
Nathan V. Patwardhan
nvp@shore.net
------------------------------
Date: 24 Mar 1997 16:49:29 -0700
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
To: Ken Anderson <anderson@necsys.gsfc.nasa.gov>
Subject: Re: Can someone explain this behaviour?
Message-Id: <ombu88x33r.fsf@tees.cs.ualberta.ca>
Just read the description of reverse:
reverse LIST
In a list context, returns a list value consisting of the elements of
LIST in the opposite order. In a scalar context, returns a string
value consisting of the bytes of the first element of LIST in the
opposite order.
print reverse(@foo),bar gives reverse list context
print reverse(@foo).bar gives reverse scalar context, because . works on
scalars only
My favorite use of reverse:
%hash=reverse(%hash)
inverts the hash, so that keys become values and values become keys.
------------------------------
Date: Tue, 25 Mar 1997 00:17:32 GMT
From: shyde@poboxes.com (Simon Hyde (aka Jeckyll))
Subject: Re: Can someone explain this behaviour?
Message-Id: <33380c29.26444799@news.uni-stuttgart.de>
On 24 Mar 1997 15:55:33 -0500, Ken Anderson
<anderson@necsys.gsfc.nasa.gov> wrote:
>Hi,
>
>i ran into some curious behaviour that i just can't explain.
>check this out:
>
>
> % foo
> string list? (Exit with ^D):1 2 3 4
> 1 2 3 4
> 1 2 3 4
> 1 2 3 4
> 4 3 2 1
>
>
>as a simple example, this script asks for input line(s) (1 2 3 4)
>here is the script that did this:
>
>
> print "string list? (Exit with ^D):"; chop(@str=<STDIN>);
> print @str,"\n";
> print $str[0],"\n";
> print reverse(@str),"\n";
> print reverse(@str)."\n";
>
>
>As you can see, it puts the line into an array .
>The curious behaviour comes in the following print statements as i print
>the array, @str. Of course, the 1st line is in $str[0] and reverse does
>not reverse the one element. Now, why is the . (concat) operator getting
>perl to "reverse" the one, and only one, element in the array? Concat with
>a null string does this too.
To understand this you have to take a look at the reverse section of
the perlfunc manpage where it says: "In a list context, returns a list
value consisting of the elements of LIST in the opposite order. In a
scalar context, concatenates the elements of LIST, and returns a
string value consisting of those bytes, but in the opposite order. "
In the first example reverse is evaluated as a list and so it simply
reverses the array and what you end up with is a:
print '4 3 2 1', '1 2 3 4', '1 2 3 4', '1 2 3 4', '1 2 3 4', "\n";
However the concat operator in the second print forces the reverse to
be evaluated in a scalar context, forcing reverse to convert the input
to '1 2 3 41 2 3 41 2 3 41 2 3 44 3 2 1' and then reverse it to give:
'1 2 3 44 3 2 14 3 2 14 3 2 14 3 2 1'
This would give you exactly the same result as doing a:
print scalar(reverse(@str)), "\n";
> In fact, if you go
>
> print "help".reverse(@str),"\n";
>
>you get this:
>
> help4 3 2 1
>
You seem to have run this under perl 4 after having run the others
under perl 5, since under perl5 this produces the same as the second.
However under perl 4, according to the manpages reverse should "In an
array context, returns an array value consisting of the elements of
LIST in the opposite order. In a scalar context, returns a string
value consisting of the bytes of the first element of LIST in the
opposite order.", which is rather wierd since my DOS port of perl4
seems to be insisting on reversing the last element of the array, but
that's a different matter intirely :-)
---
Yours Sincerely,
,
() o /| | |
/\ _ _ _ __ _ _ |___| __| _
/ \| / |/ |/ | / \_/ |/ | | |\| | / | |/
/(__/|_/ | | |_/\__/ | |_/ | |/ \_/|/\_/|_/|__/
/|
\|
(Simon Hyde)
------------------------------
Date: Mon, 24 Mar 1997 17:20:29 -0800
From: Devin Ben-Hur <dbenhur@egames.com>
To: Ken Anderson <anderson@necsys.gsfc.nasa.gov>
Subject: Re: Can someone explain this behaviour?
Message-Id: <3337285C.246F@egames.com>
[mail&post]
Ken Anderson wrote:
> i ran into some curious behaviour that i just can't explain.
> check this out:
> % foo
> string list? (Exit with ^D):1 2 3 4
> 1 2 3 4
> 4 3 2 1
> as a simple example, this script asks for input line(s) (1 2 3 4)
> here is the script that did this:
> print "string list? (Exit with ^D):"; chop(@str=<STDIN>);
> print reverse(@str),"\n";
> print reverse(@str)."\n";
>
> [snip] Now, why is the . (concat) operator getting
> perl to "reverse" the one, and only one, element in the array?
See Camel p.207 or man perlfunc.
Specifically:
In scalar context, the function (reverse) concatenates
all the elements of LIST together and then returns the
reverse of that, character by character.
The concat operator puts your reverse in scalar context
explaining the behavior you observed.
HTH
--
Devin Ben-Hur <dbenhur@egames.com>
eGames.com, Inc. http://www.egames.com/
eMarketing, Inc. http://www.emarket.com/
"It's better to be lucky than good." -- Elizabeth Bourne
------------------------------
Date: Mon, 24 Mar 1997 19:59:45 -0600
From: Eryq <eryq@enteract.com>
Subject: Re: Can someone explain this behaviour?
Message-Id: <33373191.4A161B07@enteract.com>
Vladimir Alexiev wrote:
> My favorite use of reverse:
> %hash=reverse(%hash)
> inverts the hash, so that keys become values and values become keys.
As in:
%y_to_x = reverse(%x_to_y);
For code that needs to do bidirectional 1-to-1 mappings,
that is just *too* damn cute. One more snippet for the
bag of tricks... :-)
--
___ _ _ _ _ ___ _ Eryq (eryq@enteract.com)
/ _ \| '_| | | |/ _ ' / Hughes STX, NASA/Goddard Space Flight Cntr.
| __/| | | |_| | |_| | http://www.enteract.com/~eryq
\___||_| \__, |\__, |___/\ Visit STREETWISE, Chicago's newspaper by/
|___/ |______/ of the homeless: http://www.streetwise.org
------------------------------
Date: 24 Mar 1997 18:04:41 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
To: German Cancio Melia <German.CancioMelia@cern.ch>
Subject: Re: Casting in Perl
Message-Id: <8cafnsg4t2.fsf@gadget.cscaper.com>
>>>>> "German" == German Cancio Melia <German.CancioMelia@cern.ch> writes:
German> Hello,
German> I'm in need of doing a casting in Perl but I don't know how.
German> Suppose I've got a class A, with methods M1 and M2, and M1 calling
German> M2.
German> Now, I create a class B which inherits from A, also with methods M1 and
German> M2 which override M1 and M2 from A. In method M1 I have to call
A-> M1. The problem is that M1 from A will call M2 from B where it is
German> supposed to call M2 from A!!
German> Is there a way to avoid this??
Yes. Proper Design.
:-)
Hint: if the M2 in B cannot be called on objects of class B in place
of the M2 of A, then M2 is not really overridden properly.
print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,495.69 collected, $182,159.85 spent; just 525 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me
------------------------------
Date: Tue, 25 Mar 1997 00:17:30 GMT
From: shyde@poboxes.com (Simon Hyde (aka Jeckyll))
Subject: Re: Creating arrays...
Message-Id: <33370852.25461787@news.uni-stuttgart.de>
On 24 Mar 1997 17:57:00 GMT, "Stephen Hill" <scs@huron.net> wrote:
>@sentence = split(/ /, $string);
>
>
>The above will split the string at whatever you specify in between the "/
>/", leave a space if that is what you want or commas.
>I am not sure, but you may need brackets around the $string, ...try it :-)
>
>To print it you would use
>print @sentence[0]
>print @sentence[1]
>.......
>..
>.
You would???
I think you might want to look at how arrays work again, to print you
would do:
print $sentence[0];
print $sentence[1];
etc...
---
Yours Sincerely,
,
() o /| | |
/\ _ _ _ __ _ _ |___| __| _
/ \| / |/ |/ | / \_/ |/ | | |\| | / | |/
/(__/|_/ | | |_/\__/ | |_/ | |/ \_/|/\_/|_/|__/
/|
\|
(Simon Hyde)
------------------------------
Date: Mon, 24 Mar 1997 20:25:01 -0600
From: Gabriele R Fariello - 608-576-8660 <gabriele@clotho.com>
To: Stephen Hill <scs@huron.net>
Subject: Re: Executing a script???
Message-Id: <3337377D.74BE2D54@clotho.com>
Stephen Hill wrote:
>
> How can you have a script execute automatically when your page is
> loaded....SSI works if the script is on the same server as the page, but
> does not seem to work if the html page and the script are on different
> servers.
>
> Is there some way you can use the image tag to do this?
Hmm. How about having a script local to the server #1 which goes out,
fetches the script output from server #2 through standard HTTP protocols
and pipes the output to STDOUT which in turn will, of course, get put
into your page as if the server #1 had executed the remote CGI script?
Just a though. LWP is great for that ;)
-Gabriele
P.S. Could you subject a post like this something like "Executing Remote
scripts through SSI" in the future? "Executing a script" sounds like you
might have forgotten to change the privilages of or include the
#!/usr/bin/perl line in your scripts. Thanks.
--
Gabriele R. Fariello | Clotho Internet Consulting
gabriele@clotho.com | 33 University Square No. 251
(608)-576-8660 | Madison, WI 53715
------------------------------
Date: Tue, 25 Mar 1997 00:17:37 GMT
From: shyde@poboxes.com (Simon Hyde (aka Jeckyll))
Subject: Re: foreach loop and printing
Message-Id: <333d191b.29759366@news.uni-stuttgart.de>
On 24 Mar 1997 22:03:27 GMT, "Kaushal Shah" <kaushal@nwdc.com> wrote:
>Why won't the following code create a file called uid.log assuming there is
>some data in
>%dir?
>
>@dirs = keys (%dir);
>open (LOG, ">uid.log");
>foreach $entry (@dirs) {
> print LOG $entry." ".$dir{$entry}."\n";
>}
>close (LOG);
>
Sounds like an error opening the file, nothing to do with the foreach,
try changing the open to:
open (LOG, ">uid.log") || die("Error opening uid.log: $");
---
Yours Sincerely,
,
() o /| | |
/\ _ _ _ __ _ _ |___| __| _
/ \| / |/ |/ | / \_/ |/ | | |\| | / | |/
/(__/|_/ | | |_/\__/ | |_/ | |/ \_/|/\_/|_/|__/
/|
\|
(Simon Hyde)
------------------------------
Date: Mon, 24 Mar 1997 20:06:59 -0600
From: Gabriele R Fariello - 608-576-8660 <gabriele@clotho.com>
To: Apple-O <appleo@cybercomm.net>
Subject: Re: help: 2d perl array doesn't work!
Message-Id: <33373343.571DA35C@clotho.com>
Apple-O wrote:
>
> can anyone can explain why this script doesn't work?
> (other than the fact that i'm a bonehead programmer)
> :/)
>
Well, it's kindof a mess.... Your for loop is messed up and your
de-referencing is incorrect. Take a look at this modified script and see
if it clarifies things a bit (Note that my comments are preceded by a
##).
#!/usr/bin/perl5
# fill up a 2d array and print it back out:
# a i e
# d f b
# g c h
# (eg w/coords (x,y) the char at pos (1,3) = g)
# sort by column 1 gives:
# a i e
# d f b
# g c h
# sort by column 2 gives:
# should look like:
# g c h
# d f b
# a i e
# sort by column 3 gives:
# d f b
# a i e
# g c h
print "twodim2.pl, trying to run it right!\n\n";
## This works also and is much pretier ;)
%two_d_hash = ('1' => { '1' => 'a',
'2' => 'b',
'3' => 'c', },
'2' => { '1' => 'd',
'2' => 'e',
'3' => 'f', },
'3' => { '1' => 'g',
'2' => 'h',
'3' => 'i', },);
## But I left this in any way. Still works if you comment out the
## above statment.
$two_d_hash{'1'}{'1'} = "a";
$two_d_hash{'1'}{'2'} = "b";
$two_d_hash{'1'}{'3'} = "c";
$two_d_hash{'2'}{'1'} = "d";
$two_d_hash{'2'}{'2'} = "e";
$two_d_hash{'2'}{'3'} = "f";
$two_d_hash{'3'}{'1'} = "g";
$two_d_hash{'3'}{'2'} = "h";
$two_d_hash{'3'}{'3'} = "i";
## This printout looks messy, and it should.
print "#====================\n",%two_d_hash,"\n#====================\n";
## This prints out more information than you might ever want to see ;)
print "\n\nMy way, complex....\n";
foreach $hash_key (sort keys %two_d_hash)
{
print "Trying \$hash_key = '$hash_key'\n";
print " \$two_d_hash{\$hash_key} = $two_d_hash{$hash_key}\n";
print " \%{\$two_d_hash{\$hash_key}} =
".(%{$two_d_hash{$hash_key}})."\n";
print " \$two_d_hash{\$hash_key}{'1'} =
$two_d_hash{$hash_key}{'1'}\n";
foreach (sort keys %{$two_d_hash{$hash_key}})
{
print ${$two_d_hash{$hash_key}}{$_},"\n";
}
}
## But if you just want a b c d e f g h i...
print "\n\nMy way, simple....\n";
foreach $hash_key (sort keys %two_d_hash)
{
foreach (sort keys %{$two_d_hash{$hash_key}})
{
print ${$two_d_hash{$hash_key}}{$_}," ";
}
}
## For more space ;)
print "\n\nYour way, sort of...\n";
## and if you insist on using a numerical for loop REMEMBER that
## the TEST condition is second, NOT last ;) Why do you go all the
## Way to 9? There are only 3 elements to each hash (3X3 = 9) and 9
## in the whole 2D hash. You also need to start at 1 not 0 and test
## for < 4 not < 3 and certainly not < 9.
for ($x=1; $x<4; $x++)
{
for ($y=1; $y<4; $y++)
{
print ${$two_d_hash{$x}}{$y}," ";
}
}
print "\nBYE BYE!\n";
__END__
Hope this helps :*)
-Gabriele
--
Gabriele R. Fariello | Clotho Internet Consulting
gabriele@clotho.com | 33 University Square No. 251
(608)-576-8660 | Madison, WI 53715
------------------------------
Date: 24 Mar 1997 17:05:20 -0800
From: krid@ng.netgate.net (Dirk Bergstrom)
Subject: Is there a better way to do this?
Message-Id: <5h78cg$dpj@ng.netgate.net>
So I've got this code, and I can't shake the feeling that there's a
better way.
The program is a CGI routine which automates posting of newsletter
articles (I'm tired of formatting them every week, so I'll let the
user do it...). I need to add in <P> tags between paragraphs, but not
in the middle of <table>s or <pre>formatted text. My solution is to
pull all the tables into an array, and replace them with placeholders
(%%%% in this case). After I deal with the <P> tags, I reverse the
procedure (which works fine). Here's how I do it now:
my @table = ($body =~ /(<pre>.*<\/pre>|<table>.*<\/table>)/gis);
$body =~ s/(?:<pre>.*<\/pre>|<table>.*<\/table>)/%%%%/gis;
I just don't think that running a pattern against a (possibly large)
scalar *twice* is the way to go. Is there some crafty trick I'm
missing, or is this it (or should I have done the whole thing some
other way...)?
Thanks
Dirk Bergstrom
krid@netgate.net
------------------------------
Date: Tue, 25 Mar 1997 00:17:34 GMT
From: shyde@poboxes.com (Simon Hyde (aka Jeckyll))
Subject: Re: Little beginers question
Message-Id: <333911c9.27884353@news.uni-stuttgart.de>
On 24 Mar 1997 18:30:16 GMT, "Josep Jover" <jjover@correu.andorra.ad>
wrote:
>Hello,
>Just two little questions to help a beginner that doesn't write english
>very well ;-)
>
>How can I retrieve the current directory where I am ('chdir' is only use to
>change it ?)
use Cwd;
$current_dir = getcwd || cwd;
>How can I know the number of elements in a table. I try $len=@array but it
>don't seems to work well.
That should work absolutely fine, you might also want to try $#array
which will give you the index to the last element in the array
(usually 1 less than the scalar(@array) which you use when doing
$len=@array)
>With the regular expression $body=~/xxx/ we can know if the expression xxx
>is contained in $body, how can I know the number of times, and the location
>to retrieve a part of the string ?
>
$times = 0
@positions = ();
while ($body =~ /xxx/g){$times++; push(@positions, pos($body));}
print "xxx found $times times at: " , join (", ", @positions);
---
Yours Sincerely,
,
() o /| | |
/\ _ _ _ __ _ _ |___| __| _
/ \| / |/ |/ | / \_/ |/ | | |\| | / | |/
/(__/|_/ | | |_/\__/ | |_/ | |/ \_/|/\_/|_/|__/
/|
\|
(Simon Hyde)
------------------------------
Date: Mon, 24 Mar 1997 19:20:49 -0600
From: Gabriele R Fariello - 608-576-8660 <gabriele@clotho.com>
To: Josep Jover <jjover@correu.andorra.ad>
Subject: Re: Little beginers question
Message-Id: <33372871.7432894F@clotho.com>
Josep Jover wrote:
>
> Hello,
> Just two little questions to help a beginner that doesn't write english
> very well ;-)
Well, these are answered in the Perl Programming "Camel" Book, but they
are
not bad questions.
> How can I retrieve the current directory where I am ('chdir' is only use to
> change it ?)
Try
use Cwd;
$dir = cwd();
Taken straight from "Programming Perl" by Larry Wall, Tom Christiansen
and Randal L. Schwartz pp. 386-387 (Great book - buy it, love it and
lookup path in index)
> How can I know the number of elements in a table. I try $len=@array but it
> don't seems to work well.
Hmmm, that should not be. Can we see the code? FYI:
($#array + 1)
gets you the number of elements in the array and so will
$len = (@array);
but that list context stuff can be confusing. Note that
($len) = (@array);
and
($len) = @array;
are the same as
$len = $array[0];
which is no good, unless of course you want to get the first element in
that array, but then I think you should use the latter and not the
former ;)
Try this script:
#/usr/local/bin/perl
print "Oh that list context thing....\n";
@array = ('a','b','c','d',);
print '$len = @array = ';
$len = @array;
print "'$len'\n\n";
print '($len) = @array = ';
($len) = @array;
print "'$len'\n\n";
print '$len = (@array) = ';
$len = (@array);
print "'$len'\n\n";
print '($len) = (@array) = ';
($len) = (@array);
print "'$len'\n\n";
print '$len = $#array + 1 = ';
$len = $#array + 1;
print "'$len'\n\n";
print "all done ;)\n";
__END__
And you will get this:
Oh that list context thing....
$len = @array = '4'
($len) = @array = 'a'
$len = (@array) = '4'
($len) = (@array) = 'a'
$len = $#array + 1 = '4'
all done ;)
If you don't get this, let me know what you do get....
> With the regular expression $body=~/xxx/ we can know if the expression xxx
> is contained in $body, how can I know the number of times, and the location
> to retrieve a part of the string ?
Hmm. I always have to look this one up myself. It's in the Perl
Programming Book, but I'm not up for searching right now. If some one
else wants to...
> Best reguards
> Josep Jover
> jjover@andorra.ad
Hope this helps enough for now ;)
Take care....
-Gabriele
--
Gabriele R. Fariello | Clotho Internet Consulting
gabriele@clotho.com | 33 University Square No. 251
(608)-576-8660 | Madison, WI 53715
------------------------------
Date: Mon, 24 Mar 1997 17:44:10 -0800
From: Lewis Taylor <lewis@nexusint.com>
Subject: PARADOX
Message-Id: <33372DEA.69B1@nexusint.com>
I am currently writing a prog which will allow you to edit HTML files
from a browser. The following is an except from the program to generate
a TEXTBOX containing the file $file_to_edit
print "<TEXTAREA NAME=\"text\" ROWS=15 COLS=50>\n\n\n";
open(FILE,"$file_to_edit");
while (<FILE>)
{
print;
}
close(FILE);
print "\n\n\n</TEXTAREA>\n";
This would work fine but for the fact that at the end of $file_to_edit
there are two tags </BODY> and </HTML> These are parsed screwing up the
generated page completly. The following, at first glance, appears to be
a viable solution:
print "<TEXTAREA NAME=\"text\" ROWS=15 COLS=50>\n\n\n";
#-- UNLESS WE USE ANNOTATION HERE </BODY></HTML> TAGS WILL INTERPRETED
#-- THIS IS INTERESTING SINCE THE FILE IS STILL DISPLAYED DISPITE BEING
#-- COMMENTED OUT WITHIN THE SCOPE OF HTML
print "<!--";
open(FILE,"$file_to_edit");
while (<FILE>)
{
print;
}
close(FILE);
print "-->";
print "\n\n\n</TEXTAREA>\n";
Now we have the text we want in the box. However since the text is
annotated the value of the form variable "text" is now NULL.
So therefore my question to you would be how do I get the file
displayed in the TEXTBOX in such a way that when I submit it is passed
to the form variable "text"
Thanks in advance,
Lewis
------------------------------
Date: 25 Mar 1997 01:51:28 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Perl training in Australia?
Message-Id: <5h7b30$8iu$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
garyaj@geko.com.au (Gary Ashton-Jones) writes:
:Does anyone know of anyone teaching Perl in Australia to commercial
:organisations?
I group have been known to come to Oz for such purposes. It
just takes lead time. See http://www.perl.com/perl/info/training.html
for details.
--tom
--
Tom Christiansen tchrist@jhereg.perl.com
X-Windows: Flawed beyond belief.
--Jamie Zawinski
------------------------------
Date: 25 Mar 1997 01:37:57 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: Posting to Newsgroups from Perl Script
Message-Id: <5h7a9l$bjt@fridge-nf0.shore.net>
Marc Dworkin (marcus@force.stwing.upenn.edu) wrote:
: (How) Can I post to a Newsgroup from a Perl script?
Get the NNTPClient modules from: http://www.perl.com/CPAN/
and read the pod documentation in NNTPClient!
Please, no spamming with NNTPClient. :-) Hope this helps!
--
Nathan V. Patwardhan
nvp@shore.net
------------------------------
Date: Mon, 24 Mar 1997 18:53:22 -0500
From: Roderick Moten <rod@cs.colgate.edu>
Subject: Problems loading Socket module
Message-Id: <333713F2.66DA3821@cs.colgate.edu>
For some reason, I was unable to successfully load the the Socket
module. The Perl script
#/usr/local/bin/perl
require 5.003;
use strict;
use Socket;
generated the following output.
Can't load '/usr/local/lib/perl5/next/5.003/auto/Socket/Socket.so' for
module Socket: rld(): Undefined symbols:
_Perl_croak
_errno
_strcmp
_Perl_stack_sp
_Perl_markstack_ptr
_Perl_stack_base
_Perl_na
_Perl_sv_2pv
_Perl_sv_2iv
_Perl_sv_newmortal
_Perl_sv_setnv
_gethostbyname
_bcopy
_inet_addr
_Perl_sv_setpvn
_inet_ntoa
_strlen
_Perl_newSVpv
_Perl_sv_2mortal
_memset
_memcpy
_htons
_ntohs
_Perl_stack_max
_Perl_stack_grow
_Perl_newSViv
_htonl
_Perl_Sv
_sprintf
_perl_get_sv
_Perl_newXS
_Perl_sv_yes
at /usr/local/lib/perl5/Socket.pm line 272.
I'm running Perl on a NeXT workstation. Could some one tell me why I'm
unable to load the module?
Rod
------------------------------
Date: 25 Mar 1997 01:50:38 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: Problems loading Socket module
Message-Id: <5h7b1e$bjt@fridge-nf0.shore.net>
Roderick Moten (rod@cs.colgate.edu) wrote:
: For some reason, I was unable to successfully load the the Socket
: module. The Perl script
: #/usr/local/bin/perl
Did you build Perl yourself? If not, it's possible that the person
who built it didn't enable dynamic loading. If you did build Perl,
I'd build it again with dynamic loading (if Next supports it - I
apologize for not knowing anything about Next), and watch 'make test'
very closely to see that everything is working okay before you install
it globally.
--
Nathan V. Patwardhan
nvp@shore.net
------------------------------
Date: 24 Mar 1997 16:31:12 -0700
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <omend4x3y7.fsf@tees.cs.ualberta.ca>
In article <5h2sgm$pcv@bug.rahul.net> c.c.eiftj@33.usenet.us.com (Rahul Dhesi) writes:
> >You don't need backtracking if you have NFA in your disposal, and I think
> >you agreed that NFAs are still regular.
> Are you claiming that every perl search can be implemented as an NFA?
No. But YOU claimed that every language that requires backtracking (in a
certain IMPLEMENTATION you seem to have in mind) is not regular, which is not
true. And you gave several examples, which all fail to be non-regular.
> If not, then I fail to see the relevance of the above argument.
Perl regexps do not specify regular languages. But not for the reasons you
believe them to.
------------------------------
Date: 24 Mar 1997 16:35:34 -0700
From: Vladimir Alexiev <vladimir@cs.ualberta.ca>
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <omd8sox3qx.fsf@tees.cs.ualberta.ca>
In article <5h6gq8$g4l@samba.rahul.net> c.c.eiftj@33.usenet.us.com (Rahul Dhesi) writes:
>
> > /^a.*?b.*?c/ # non-greedy, no backtracking, fast, regular expression
>
> To see why /^a.*?b.*?c/ does not require backtracking, implement it as a
> finite state machine, as described below.
And exactly the same FA works for the greedy ^a.*b.*c
------------------------------
Date: 23 Mar 1997 15:55:28 GMT
From: jfriedl@tubby.nff.ncl.omron.co.jp (Jeffrey)
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <JFRIEDL.97Mar24005528@tubby.nff.ncl.omron.co.jp>
Clay Irving <clay@panix.com> wrote:
|> >with history. I suppose you could start a crusade to use a different
|> >phrase. How about 'whachamacallit'? :-)
|>
|> And I highly recommend the second edition of Jeffrey's Book called,
|> "Mastering Whachamacallits"
Mmmm, somehow this reminds me of the last line of the first paragraph
on page 71 (of the first edition :-)
Jeffrey
----------------------------------------------------------------------------
Jeffrey Friedl <jfriedl@ora.com>
O'Reilly & Associates' _Mastering Regular Expressions_
http://enterprise.ic.gc.ca/~jfriedl/regex/
------------------------------
Date: 23 Mar 1997 16:43:05 GMT
From: jfriedl@tubby.nff.ncl.omron.co.jp (Jeffrey)
To: c.c.eiftj@33.usenet.us.com (Rahul Dhesi)
Subject: Re: term 'regular expressions' considered undesirable
Message-Id: <JFRIEDL.97Mar24014305@tubby.nff.ncl.omron.co.jp>
[mail and post and cc]
Rahul Dhesi <c.c.eiftj@33.usenet.us.com> wrote:
|> I got email from Jeffrey Friedl and I'm not sure if it was private email
|> or a copy of a posting. In any case, based on his comment's here's a
|> followup posting.
I wish you would have posted my original note, since your conclusions were
pretty unrelated to what I wrote. ["private email" refers to the method of
sending, not the contents -- like with all other means of human
communication, I rely only on your sense of right and wrong to not divulge
that which I intend to be private between you and me -- obviously, that
doesn't apply here, so you should have felt free to post it if you weren't
sure if it had been. This is a mail and post, though, so need here]
|> /^a.*b.*c/ # greedy, needs backtracking, slow, not regular expression
|> /^a.*?b.*?c/ # non-greedy, no backtracking, fast, regular expression
|>
|> Now the second one needs no backtracking:
|>
|> find an 'a' or fail;
The "or fail" is, by definition, backtracking. As I told you in my earlier
message, both will backtrack. There are situations where the 2nd will
backtrack more than the 1st. If there is no match, they'll both backtrack
exactly the same number of times (since they both match the same possible
sets of strings, but in different orders, they both must test all
permutations before a non-match is reported, and the order doesn't
influence the number).
|> I am assuming that one-character lookahead is not considered to be
|> backtracking.
That's a silly assumption, but also irrelevant to the problem at hand. The
``lookahead'' for the first .*?, for example, is /b.*?c/, not simply one
character.
|> If one-character lookahead is backtracking, then here are better
|> examples:
|>
|> /^a[^b]*b[^c]*c/ # non-greedy, no backtracking, fast, regular expression
This indeed will match faster, in general, but still requires backtracking.
Any match will, by definition, have entailed 2 backtracks.
|> The important point in all this is that some patterns are regular
|> expressions and others are not, and calling all of them regular
|> expressions is not a good idea.
I don't know what this comment has to do with the rest of your post (is it
supposed to be related?), but to a preponderance of people that *use* these
things, ``regular expression'' has just as much implied meaning as ``bob'',
so it's not a hindrance. Sure, it's not what the algebraic term ``regular
expression'' means, but so what? I guess if it hits your hot button you'd
care. I don't happen to think it's a big deal, but don't deny you your right
to think it is :-)
|> The term 'search pattern' or just
|> 'pattern' is nicely generic and does not cause any confusion.
Indeed.
|> If we must use a mathematical term, we should use it correctly.
You mean like ``addition'' and the like? The computation meaning of
these terms is SUBSTANTIALLY different from the real meaning -- in
real life, mathematical ops don't lose precision, overflow, etc.
Seems to be in the same league to me.
Vladimir Alexiev <vladimir@cs.ualberta.ca> followed up:
|> > /^a[^b]*?b[^c]*?c/ # non-greedy, no backtracking, fast, regular expression
|> This one won't need backtracking indeed.
Sure it does... when the [^b] fails, it can backtrack to the ``gee, because
of the * I didn't really need to attempt [^b], so let's skip it'' state.
|> How a matcher is implemented (backtracing etc) has no bearing on the
|> langauge it recognizes (unless it's implemented incorrectly :-).
|> Consider this: DFA (deterministic automata) and NFA (non-deterministic
|> ones) have the same expressive power
In theory (and some practice), an NFA and DFA can be made to match the
exact same language, but in common practice, an ``NFA'' is usually endowed
with a much more expressive language.
To a DFA (awk, lex, etc.), /a|and/ and /and|a/ and /a(nd?)/ are *exactly*
the same. (Internally, they boil down to the same thing.) Yet they're
quite different with Perl and the majority of regex engines out there.
(Quiz: the 3rd will match the same as which of the first two?)
|> Regular languages are ones that can be produced using regular operations:
|> empty, letter, concatenation, alternative, and star. The greedy/non-greedy
|> distinction doesn't come into play here at all.
Oh, yes it does! (unless you're talking only calculus theory and not
considering common practice). If they're the same, then so are /.*/ and
/.*?/, and if the target string has any [non-newline] characters they're
guaranteed to be different.
Rahul Dhesi <c.c.eiftj@33.usenet.us.com> wrote:
|> Because to implement backtracking you must augment the DFA or NFA with
|> memory, and once you add memory, it's no longer a DFA or NFA.
``memory'' is simply a method of implementation. If you implemented a true
NFA and made the right decision at each step, you'd need no memory. Pretty
similar to a maze. A DFA is an NFA with the right decision guaranteed in
practice, much like a maze with all wrong-paths closed off.
|> >You don't need backtracking if you have NFA in your disposal, and I think
|> >you agreed that NFAs are still regular.
|>
|> Are you claiming that every perl search can be implemented as an NFA?
|> If not, then I fail to see the relevance of the above argument.
I think there are a lot of problems with semantics here (which is, I believe,
the whole reason you brought up this thread :-). In calculus theory,
NFAs and DFAs provide different ways to view the same thing. In computing
*practice*, the terms ``NFA'' and ``DFA'' are not necessiarly strongly
related to their mathematical background. Perhaps ``NFA style'' etc., would
be better.
You're certainly right that many Perl regex constructs can not be
represented with a (calculoulsly-pure) NFA (or DFA, or anything else
regular, since Perl regular expressions aren't regular -- as you well know :-).
But then, that's OK with me -- Perl regular expressions are not calculus,
they're a programming language within the Perl programming language.
Tom C jumped in with:
|> Don't we have anything more important to argue about?
Like how to use the term ``email'' in English? /-:
Jeffrey
----------------------------------------------------------------------------
Jeffrey Friedl <jfriedl@ora.com>
O'Reilly & Associates' _Mastering Regular Expressions_
http://enterprise.ic.gc.ca/~jfriedl/regex/
------------------------------
Date: 24 Mar 1997 18:35:55 -0500
From: steiner@klinzhai.rutgers.edu (Dave Steiner)
Subject: two perl4->perl5 gotchas not in perltrap manpage?
Message-Id: <5h734r$q00@klinzhai.rutgers.edu>
I'm finally getting around to moving a large (several 1000 line) perl
script from perl4 to perl5. I initially checked for all the kinds of
problems listed in the "perl4 traps" section of the book. After that,
in testing and use I found the following two problems:
I have something like:
print "$user@$host\n";
and -w never complained but it certainly didn't print what I expected
since @$host was undefined. \@ fixed the problem. I suspect this
can't be found via -w although maybe it could warn you during
run-time.
The second one is a different behavior in accessing non-defined hash
elements. Perl4 would return '' but perl5 returns undef. I think the
perl5 behavior makes more sense but I didn't find any reference to
this change in behavior in the book or manpages.
Here's my test program called 'testp':
--------------------
%hash = ('foo', 1, 'bar', 2);
sub test
{
local(*array, $index, $newp) = @_;
local($temp_info);
if ((($temp_info=$array{$index}) eq "") && (!$newp)) {
print "Record not found.\n";
return undef;
} else {
if (($temp_info ne "") && $newp) {
print "This should not happen!\n";
}
return $temp_info;
}
}
print "Should return '2'\n";
$ret = &test(*hash, 'bar', 0);
print "Returned '", (defined($ret)) ? $ret : "undef", "'\n\n";
print "Should return 'Return not found' and 'undef'\n";
$ret = &test(*hash, 'baz', 0);
print "Returned '", (defined($ret)) ? $ret : "undef", "'\n\n";
print "Should return 'This should not happen' and '2'\n";
$ret = &test(*hash, 'bar', 1);
print "Returned '", (defined($ret)) ? $ret : "undef", "'\n\n";
print "Should return ''\n";
$ret = &test(*hash, 'baz', 1);
print "Returned '", (defined($ret)) ? $ret : "undef", "'\n\n";
--------------------
Here's the old behavior under perl4 (the "Should return" messages are
clearly for the perl4 version):
steiner> perl4.036 testp
Should return '2'
Returned '2'
Should return 'Return not found' and 'undef'
Record not found.
Returned 'undef'
Should return 'This should not happen' and '2'
This should not happen!
Returned '2'
Should return ''
Returned ''
And here's the perl5 version. The last subroutine call now returns
undef instead of ''.
steiner> perl5.002 testp
Should return '2'
Returned '2'
Should return 'Return not found' and 'undef'
Record not found.
Returned 'undef'
Should return 'This should not happen' and '2'
This should not happen!
Returned '2'
Should return ''
Returned 'undef'
Have I just missed these in the book and perltrap manpage or should
mention of them be added?
thanks!
ds
--
Dave Steiner
Associate Director of Network Services, Rutgers University Computing Services
steiner@bakerst.rutgers.edu or steiner@nbcs-ns.rutgers.edu
------------------------------
Date: 25 Mar 1997 00:56:44 GMT
From: "Neil Edmondson" <neiled@enteract.com>
Subject: undef in Arguments to Subroutines
Message-Id: <01bc38b7$20eced20$a69a70cf@default>
This will drive me more crazy. What am I missing?
I call the subroutine using the following....
&Make_Log_Entry($logfile,$email,,,,$comments);
The subroutine has the following for loop.
sub Make_Log_Entry {
.
.
$record = "";
$arglength = @_;
for ($arg = 0; $arg < $arglength; $arg++) {
if (defined $_[$arg]) {
$record .= "$_[$arg]|";
} else {
$record .= "|";
}
}
.
.
other good stuff
.
.
}
#############################
I had hoped $record would end up looking like this:
logfile text|emailaddress||||comment text|
but instead it comes out like this:
logfile text|emailaddress|comment text|
Shouldn't the null arguments come across as undef, and shouldn't the "if
defined" catch them??
TIA, email to neiled@enteract.com appreciated.
---------------------------------
Collectible baskets on the web at
http://www.weaveware.com
---------------------------------
------------------------------
Date: Tue, 25 Mar 1997 01:40:20 GMT
From: revjack@radix.net (revjack@radix.net)
Subject: What's a bad Perl book?
Message-Id: <33422cc1.4916831@news1.radix.net>
Keywords: Hexapodia as the key insight
Sorry, all those other threads were getting to me.
_________________
revjack@radix.net
------------------------------
Date: Mon, 24 Mar 1997 16:32:16 -0800
From: Greg Spencer <WSpencer@storm.sparks.nv.us>
Subject: Re: What's a good Perl book?
Message-Id: <33371D10.23E40A50@storm.sparks.nv.us>
Tom Christiansen wrote:
> In comp.lang.perl.misc,
> "Aaron, Penny, Jaymee Minner" <badsmrfs@stratos.net> writes:
> :When I was particularly frustrated at not being able to find
> :something, it seemed like more effort was put into the
> :cute comments than making the book intuitively useful.
>
> Why don't you tell us what you had trouble finding/learning
> so that we can address these in a future reprint?
>
> --tom
In the interest of "Making the World a Better Place"
I am just learning Perl, and bought the Camal book because
the book store was sold out of the Llama book.
I have 2 suggestions for future reprints:
1.) A quick referance
2.) On page 92 you list a number of "Assignment Operators"
but some of them are never mentioned again in the
text. (I'm still confused. but that is a steady
state for me... ).
--
|----------------------------------|
| W. Greg Spencer |
| wspencer@storm.sparks.nv.us |
| KC7SZJ |
|----------------------------------|
------------------------------
Date: Mon, 24 Mar 1997 23:57:48 GMT
From: soccer@microserve.net (Geoffrey Hebert)
Subject: When reversed<> used What is happening?
Message-Id: <5h73gc$r97$1@news3.microserve.net>
Is the software/hardware smart enough to position the readhead at the
end of the file and actually read block by block backwards?
Or is this just a software trick to read in the whole file and then
present it to the script backwards?
I am thinking of using this, but if it is just a software trick I'll
skip it. I prefer to have something like that obvious. When I run
out of some resource I want to have some control over it.
Thanks
email please heberts@microserve.net
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V8 Issue 171
*************************************