[8962] in Perl-Users-Digest
Perl-Users Digest, Issue: 2581 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 13 12:10:40 1998
Date: Wed, 13 May 98 09:01:53 -0700
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, 13 May 1998 Volume: 8 Number: 2581
Today's topics:
Question on a variation of a counter cgi script <gnielson@charlotte.infi.net>
Re: Question: Finding which element of a list a string <lr@hpl.hp.com>
Re: Randomly sorting an array (Abigail)
Re: Randomly sorting an array (Abigail)
Re: Recursive find <jhoglund@mirage.skypoint.net>
Regular Expression Question <aidan@crux.blackstar.co.uk>
Re: Regular Expression Question (Andy Lester)
Re: Regular Expression Question (Mike Stok)
Re: Small Perl quick-reference? (Shaun Sides)
Tcl and Tk NOT Siamese twins: how do we convince the wo dnadler@linex.com
Re: Tip: Filehandles <tchrist@mox.perl.com>
Re: Tip: Filehandles <gnat@frii.com>
Re: Tip: Filehandles <gnat@frii.com>
Tip: Greedy Or <gnat@frii.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 13 May 1998 11:15:01 -0400
From: Gary Nielson <gnielson@charlotte.infi.net>
Subject: Question on a variation of a counter cgi script
Message-Id: <3559B8F4.AD7D0A32@charlotte.infi.net>
Hey everyone:
Got an interesting request from someone for a counter script that adds
470 each day, weekdays only, during the school year, to indictate the
number of hands shaken by the school master. This would be made simpler
if we were allowed cron jobs, but we aren't on our server. Is there an
example out there of a perl script that can check a file's date, compare
it with the date of another file and if it is one more and a weekday,
add 470? This would be a workaround to not having a cron job. Any help
appreciated; please email reply as well.
Gary
Charlotte NC
------------------------------
Date: Wed, 13 May 1998 08:15:13 -0700
From: "Larry Rosler" <lr@hpl.hp.com>
Subject: Re: Question: Finding which element of a list a string corilates to
Message-Id: <6jcdfb$33l@hplntx.hpl.hp.com>
[posted and emailed]
Jim Allenspach wrote in message <6jc3jr$brn@Jupiter.Mcs.Net>...
>nick@hob.com writes:
>
>>I'm trying to find out what position a given string holds in a list
>
> You might check the Perl FAQ (http://language.perl.com/faq/). It
>has an answer to this very question (look in perlfaq4 for the question
"How
>can I tell whether an array contains a certain element?"). Hope this
helps.
>
>jma
I just (re-)read the FAQ and, as its title states, it tells *whether*
... But the original question was *where*.
The other response to this thread (so far) works for identical-length
array members only --essentially, join(), index(), divide by the length
to get the array index.
Is there an answer to this question better than the obvious and
mundane -- count up a loop?
my $data = 'pow';
my @array = qw( foo bar baz whatever pow bam );
# C style:
my $i;
for ($i = 0; $i < @array; ++$i) { last if $array[$i] eq $data }
# Somewhat more elegantly, for the braces-averse:
# my $i = -1;
# 1 while ++$i < @array and $array[$i] ne $data;
print $i < @array ? $i : 'not found', "\n";
__END__
4
--
Larry Rosler
Hewlett-Packard Laboratories
lr@hpl.hp.com
------------------------------
Date: 13 May 1998 15:38:26 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Randomly sorting an array
Message-Id: <6jcepi$721$1@client3.news.psi.net>
Chip Salzenberg (chip@mail.atlantic.net) wrote on MDCCXV September
MCMXCIII in <URL: news:6jalql$udt$1@cyprus.atlantic.net>:
++ According to tchrist@mox.perl.com (Tom Christiansen):
++ >In comp.lang.perl.misc, chip@mail.atlantic.net (Chip Salzenberg) writes:
++ >:A simpler approach:
++ >: sub shuffle {
++ >: my @new;
++ >: push @new, $_[rand @_] while @_;
++ >: @new;
++ >: }
++ >
++ >And when were you expecting @_ to go false?
++
++ *sigh* I _knew_ I'd forgotten something! When I thought of it I
++ used splice(), but there's many a slip 'twixt cup and lip.... Try:
++
++ sub shuffle {
++ my @new;
++ push @new, splice @_, rand @_, 1 while @_;
++ @new;
++ }
Don't do that! It's horribly inefficient.
Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET", "http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content)) =~ /(.*\))[-\s]+Additional/s) [0]'
------------------------------
Date: 13 May 1998 15:40:32 GMT
From: abigail@fnx.com (Abigail)
Subject: Re: Randomly sorting an array
Message-Id: <6jcetg$721$2@client3.news.psi.net>
Tom Rokicki (rokicki@cello.hpl.hp.com) wrote on MDCCXVI September
MCMXCIII in <URL: news:6javnq$ccp@cello.hpl.hp.com>:
++
++ The only `real' problem is if you suspect that the limited seed/limited
++ random number generator somehow bias the set of shuffles that are
++ possible. If so, this is a problem in your random number generator.
Not generating a subset of the possible permutations *at all*, how
much more bias do you want?
Abigail
--
perl -wle '$, = " "; sub AUTOLOAD {($AUTOLOAD =~ /::(.*)/) [0];}
print+Just (), another (), Perl (), Hacker ();'
------------------------------
Date: 13 May 1998 14:16:51 GMT
From: Jamie Hoglund <jhoglund@mirage.skypoint.net>
Subject: Re: Recursive find
Message-Id: <6jca0j$icr$1@shadow.skypoint.net>
Johannes Rest <rest@Extlap.bi.sdm.de> wrote:
: Hello!
: I'm quite new to perl, and for a start I would like
: to program a recursive find in perl, which also allows
: to search each matching file for a regular expression, i.e.
: find /home/ttt/src *.c int
: which should find all c files containing the string "int".
: Has anybody hints or samples to get me started?
: Thanks in advance
: Johannes
As Tom pointed out, you can use the find module, if you want, there is
a program that comes with perl called "find2perl" that will actually write
a perl script for a particular set of arguments to find.
but if you're going to program it yourself..
I had to do something like that once, (build a recursive file list, but I
knew in advance I only needed to go 2 deep) using a module wasn't an
option because of where it was going to be installed.
The thing that got me should have been obvious, ".." will cd to the
previous directory. (I was trying to use variables, lists and all kinds of
crazy fangled stuff to try to keep the cwd)
This just creates a list of files in a directory, and you will run out
of file handles / memory eventually. (remember, it's very specific to 1
particular application where this wasn't going to be an issue, also, it'll
exclude dot "." files, this was intended)
Hope you can chop it up & use it if modules won't work in your case.
sub dumpid { #{{{
local(*list) = $_[0];
my($dir_recurse)=$_[1];
++$dir_recurse;
my($handle) = "$dir_recurse:DIR";
my($dir);
opendir($handle,".");
while($dir = readdir($handle)){
if($dir =~ /^\./){
next;
}
if(-d $dir){
chdir($dir);
&dumpid(\@list,$dir_recurse);
chdir("..");
}else{
unshift(@list,$dir);
}
}
closedir($handle);
}
Jamie
------------------------------
Date: Wed, 13 May 1998 14:51:38 GMT
From: Aidan Rogers <aidan@crux.blackstar.co.uk>
Subject: Regular Expression Question
Message-Id: <895074702.562091@sparc.tibus.net>
A short question about regular expressions...
is it possible to match from the beginning of the line (the ^) right up
to a specified character? e.g.
Davy "Biscuit" Beattie (DB)
I want to match everything from the start of the line, up to the (
and put it into $1.
Thanks in advance,
Aidan
(a struggling Perlite)
------------------------------
Date: 13 May 1998 15:02:08 GMT
From: petdance@maxx.mc.net (Andy Lester)
Subject: Re: Regular Expression Question
Message-Id: <6jcclg$h6m$1@supernews.com>
: Davy "Biscuit" Beattie (DB)
: I want to match everything from the start of the line, up to the (
: and put it into $1.
my $foo = 'Davy "Biscuit" Beattie (DB)';
$foo =~ /^(.*?)\(/;
my $uptobutnotincludingtheparen = $1;
xoxo,
Andy
--
--
Andy Lester: <andy@petdance.com> http://tezcat.com/~andy/
Chicago Shows List: <shows@ChicagoMusic.com> http://ChicagoMusic.com/
------------------------------
Date: 13 May 1998 15:15:48 GMT
From: mike@stok.co.uk (Mike Stok)
Subject: Re: Regular Expression Question
Message-Id: <6jcdf4$bpl@news-central.tiac.net>
In article <895074702.562091@sparc.tibus.net>,
Aidan Rogers <aidan@crux.blackstar.co.uk> wrote:
>A short question about regular expressions...
>
>is it possible to match from the beginning of the line (the ^) right up
>to a specified character? e.g.
>
>Davy "Biscuit" Beattie (DB)
>
>I want to match everything from the start of the line, up to the (
>and put it into $1.
One way to do it is
if ($line =~ /^(.*?\()/) {
# $1 contains line up to and including the first (
}
which uses non-greedy matching, another is to say
if ($line =~ /^([^(]*\()/) {
...
}
(but note that [^(] will match a \n but . will not (by default) - as you
mentioned lines this probably doesn't matter...)
or (assuming $[ hasn't been messed with) this might be better if you're
looking for a particular character
if (($offset = index ($line, '(')) >= 0) {
$chunk = substr ($line, 0, $offset + 1);
...
}
The alterations to not include the ( are not difficult. All of these
assume that there is a ( in the line you're working on, otherwise the if
fails.
The Benchmark module might be useful if you want to see how well various
methods work on typical data.
Hope this helps,
Mike
--
mike@stok.co.uk | The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/ | PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/ | 65 F3 3F 1D 27 22 B7 41
stok@colltech.com | Collective Technologies (work)
------------------------------
Date: 13 May 1998 14:46:37 GMT
From: arch@abts.net (Shaun Sides)
Subject: Re: Small Perl quick-reference?
Message-Id: <slrn6liq9v.dr5.arch@abts.net>
Original message by: Kevin Reid <kpreid@ibm.net>
Date: Tue, 12 May 1998 13:14:35 -0400
Subject: Small Perl quick-reference?
> "Programming Perl" is a great book - with one problem: it is large and
> heavy.
>
> Does anybody know of a small (paperback-novel size) "quick reference"
> book?
>
> I want something that reminds me of the arguments to read() and lists
> the s/// modifiers, and doesn't bother explaining what the different
> contexts are (except briefly).
How's about 'Perl 5 Desktop Reference'? 44 pages
isbn 1-56592-187-9
by Johan Vromans
published by O'Reilly (of course).
;-)
It's next on my 'to purchase' list.
--
Shaun L. Sides arch@abts.net
Business web site http://www.abts.net/~arch
Showershoe web site http://www.abts.net/~arch/showershoe
Recreational web site http://sara.mmlc.nwu.edu/~arch
------------------------------
Date: Wed, 13 May 1998 13:51:49 GMT
From: dnadler@linex.com
Subject: Tcl and Tk NOT Siamese twins: how do we convince the world?
Message-Id: <6jc8hl$rfq$1@nnrp1.dejanews.com>
It is an entrenched abuse of language to refer to Tk as "Tcl/Tk" or even the
"Tcl/Tk GUI Toolkit". This is done in the survey of GUI toolkits in the
current Linux World. It is a persistent myth that Tk is an advantage that
tcl has over other languages.
I trust that a good percentage of the followers of this group have a canned
spiel when they hear something like "I wanted to rapid-prototype the interface
so I used 'Tcl/Tk'...." It begins something like "The GUI part is Tk, and you
could have used the Tk toolkit with tcl, perl, C, and probably all kinds of
other languages."
For my money, Nick and his collaborators have done a monumental job with
this stuff.
DN
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
------------------------------
Date: 13 May 1998 14:32:32 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Tip: Filehandles
Message-Id: <6jcau0$7re$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc,
Jamie Hoglund <jhoglund@mirage.skypoint.net> writes:
:Tom Christiansen <tchrist@mox.perl.com> wrote:
:Seriously, what is wrong with using globs?
Nothing.
:I've been told not to use them,
:but personally, I find them more intuitive than the $Variable->{name}
Very much so. And at least 10 times faster.
:To me, doing something like *subname = \&module::subname; makes a lot
:more sense, but I almost never see people do that
I do it.
--tom
--
*bp++ = i; /* now go back to screaming loop */
--Larry Wall, from perl/sv.c in the v5.0 perl distribution
------------------------------
Date: 13 May 1998 09:13:45 -0600
From: Nathan Torkington <gnat@frii.com>
Subject: Re: Tip: Filehandles
Message-Id: <5q3eeecifq.fsf@prometheus.frii.com>
Jamie Hoglund <jhoglund@mirage.skypoint.net> writes:
> : and then go and suck in a megamodule just because you're afraid of a star.
>
> Wow, I think those are the stronges words I've ever heard from you. :-)
You need to read more of his posts :-)
Nat
------------------------------
Date: 13 May 1998 09:24:39 -0600
From: Nathan Torkington <gnat@frii.com>
Subject: Re: Tip: Filehandles
Message-Id: <5q1ztychxk.fsf@prometheus.frii.com>
mgjv@comdyn.com.au (Martien Verbruggen) writes:
> That is a very old fashioned way to do it, and hardly anything worth
> drooling about.
The human form hasn't changed in a long time, and I still drool about
it :-) Old fashioned isn't always bad.
> You do realise that there is very likely a very good reason why perlfaq5
> states the following?
>
> # perldoc perlfaq5
> [snip]
> How can I make a filehandle local to a subroutine? How do I
> pass filehandles between subroutines? How do I make an
> array of filehandles?
>
> You may have some success with typeglobs, as we always had
> to use in days of old:
>
> local(*FH);
>
> But while still supported, that isn't the best to go about
> getting local filehandles. Typeglobs have their drawbacks.
> You may well want to use the FileHandle module, which
> creates new filehandles for you (see the FileHandle
> manpage):
> [snip]
Mea culpa. The drawback is that because filehandles aren't a basic
data type, you have to futz with typeglobs to store or pass them.
If you use FileHandle or IO::File from the very start, you have
filehandles from the start and don't have to use a different syntax
for passing filehandles than you do for passing any other type.
IO::* and FileHandle remove the need for thought, but do so at a
cost of load time.
I put typeglob filehandles into a tip because I used to fear the
different. The fact that Perl lets you specify filehandles in so
many different ways (strings, typeglobs, refs to typeglobs) only
served to cloud my mind. When I found a way that always works
and isn't complex, I figured I'd share it.
> PS. I used to use an electric drill to create holes in my wall, but I
> got confused by all the buttons and thingies. I now have seen the
> light! I use a hammer and a nail, and then pull the nail back out.
I use the module approach for this problem. My wife does all the
home repair :-)
Nat
------------------------------
Date: 13 May 1998 09:45:23 -0600
From: Nathan Torkington <gnat@frii.com>
Subject: Tip: Greedy Or
Message-Id: <5qzpgmb2ek.fsf_-_@prometheus.frii.com>
I'd have known it if I'd read Jeff Friedl's "Mastering Regular
Expressions" cover to cover, but I've still not found the time to do
that. Instead, I learned this when Tom and Jeff traded email about
local greediness vs global greediness.
The heart of the nugget is that regexp alternatives separated by | in
Perl are temporally greedy rather than textually greedy. That is,
Perl's RE engine will try alternatives from left to right and take the
first one that matches, rather than take the longest matching one.
An example would be a regular expression to allow dates in the form
"YYMM" and "YYYYMM":
($y, $m) = $string =~ /(\d\d|\d\d\d\d)(\d\d)/;
Try that on the strings "9805" and "199805", and see what you get. It
works fine for the first string, but not for the second. The regular
expression engine tries to match \d\d (two digits), succeeds, and so
never tries to match against the four digit alternative that we really
wanted.
The moral of the story: design your regexps better. Anchoring the
pattern here would have forced the engine to backtrack and match
against the \d\d\d\d that we really wanted. If that's not an option,
expressing your regexp with the longest alternative first may help.
Nat
(maybe the moral is "read Mastering Regular Expressions" :-)
------------------------------
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 2581
**************************************