[7660] in Perl-Users-Digest
Perl-Users Digest, Issue: 1286 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 7 13:14:09 1997
Date: Fri, 7 Nov 97 10:00:22 -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 Fri, 7 Nov 1997 Volume: 8 Number: 1286
Today's topics:
Re: $perl vaiables to javascript vaiables (Ronald L. Parker)
Re: An excellent example of perl madness (or "split stu (Bart Lateur)
Re: An excellent example of perl madness (or "split stu <ghowland@hotlava.com>
Re: An excellent example of perl madness (or "split stu <Steve_Kilbane@cegelecproj.co.uk>
Re: Associative Array (HASH) problem <jamesr@aethos.co.uk>
Re: Associative Array (HASH) problem (Bart Lateur)
Chomp vs Chop <mpezzuto@link.com>
Re: Command line options lusol@turkey.cc.Lehigh.EDU
Re: crontab -l reformating (Reinvent the wheel?) (bodhi1@nonamesleft.com)
entering data (Sylvain Juneau)
Re: file not found error? <brad.bradley@bridge.bellsouth.com>
Re: Help with ugly regex (Ronald L. Parker)
Re: interrupt system("$cmd") if timeout (Brendan O'Dea)
Re: make ftp on perl script with windows 95/NT <vcuya@mindspring.com>
Re: Perl variable filehandles (brian d foy)
Perl5 for IntraNetware and PATH_INFO <rlluhman@netins.net>
Please Help Perl 32 under Nt4 "Path" <gibby@tcol.net>
Re: Puzzle: palindromep (Ronald L. Parker)
reading from end of file <d-weaver@nwu.edu>
Re: Regular Expressions <dturley@ravine.binary.net>
Re: Regular Expressions (Bart Lateur)
Re: Sorting integers in Perl as an array (Andrew M. Langmead)
tcl subst in perl? <kistler@inf.ethz.ch>
Traversing a directory tree in Perl <skumar@concentric.net>
Traversing a directory tree in Perl skumar@umi.com
Re: Traversing a directory tree in Perl (Andrew M. Langmead)
Re: Undefining an associative array element (Brandon S. Allbery KF8NH; to reply, change "void" to "kf8nh")
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 07 Nov 1997 16:27:58 GMT
From: ron@farmworks.com (Ronald L. Parker)
Subject: Re: $perl vaiables to javascript vaiables
Message-Id: <34633da0.158848@news.supernews.com>
On Thu, 6 Nov 1997 20:27:04 -0800, "Myles Lawrence"
<myleslawrence@email.msn.com> wrote:
>sub print_prompt {
Did you know that you never call this sub?
>var productType = new Array();
>var deviceType = new Array();
I'm assuming that you want to set these arrays to have the values that
are in the Perl arrays you fill in later. Not knowing any Javascript
at all, I would think something like this (modulo Javascript syntax,
which is an oxymoron, or at least an undefined term) would work:
$i=0;
foreach $pt ( @prodType ) {
print qq(productType[$i] = "$pt";);
$j=0;
foreach $dt ( @$devType[$i] ) {
print qq(deviceType[$i,$j] = "$dt";);
$j++;
}
$i++;
}
Though, of course, you'll have to do this part after you have filled
in the Perl arrays. I don't know how this requirement gets along with
your document structure. You might have to save off some of the stuff
you're printing into your forms as you read it for printing later.
There may be an easier way to initialize an array in Javascript; if
so, modify the above syntax as necessary.
>EOF
> ;
Why do you keep putting these bare semicolons after your here
documents?
> $devType[$i,$j] = $db2->Data('printer_name');
This doesn't do what you think it does in Perl. Perhaps you meant
$devType[$i][$j] = $db2->Data('printer_name');
(Hint: that first syntax creates a 1-dimensional array, and the value
of $i is completely ignored.)
------------------------------
Date: Fri, 07 Nov 1997 16:31:19 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: An excellent example of perl madness (or "split stupidity")
Message-Id: <346838e4.5587520@news.tornado.be>
tadmc@flash.net (Tad McClellan) wrote:
>: If you look a bit further down in the same documentation, you can see:
>: : The pattern PATTERN may be replaced with an expression to specify
>
>But it doesn't say that /PATTERN/ may be replaced with an expression...
>
>: : patterns that vary at runtime. (To do runtime compilation only once,
>: : use /$variable/o.)
> ^ ^
>
>See? The slashes are still there ;-)
>So, where in the docs _does_ it say that you can provide a string there?
Just there. In facte there are two sentences, which should remain
different entities. The first sentence talks about an expression,
without any example.
: The pattern PATTERN may be replaced with an expression to specify
: patterns that vary at runtime.
Here an example of mine:
$pattern = "[,;]";
@ary = split $pattern, $_;
All very legal according to that first sentence in the docs. $pattern is
an expression. It replaces PATTERN.
And now for the second:
: To do runtime compilation only once, use /$variable/o.
The reason why the slashes are necessary here, is because there is no
other way to attach the "o" regexp option to the pattern in any other
way.
Bart Lateur
bart.mediamind@tornado.be
------------------------------
Date: Fri, 07 Nov 1997 17:20:13 +0100
From: Gary Howland <ghowland@hotlava.com>
To: Steve Kilbane <Steve_Kilbane@cegelecproj.co.uk>
Subject: Re: An excellent example of perl madness (or "split stupidity")
Message-Id: <34633FBD.299C@hotlava.com>
Steve Kilbane wrote:
>
> In article <34631765.7570@hotlava.com>, Gary Howland <ghowland@hotlava.com> writes:
> > It looks like we need two warnings. A warning of the form
> > "possible regular expression in double quoted string" or
> > even "redundant backslash in double quoted string" for cases
> > like "\d" might make sense, and would certainly help catch a
> > range of silly split() errors, but what are the disadvtanges?
>
> It couldn't be a generic error, because (apart from anything else)
> it would make filenames in the DOS world unusable. It's possible
> that the warning could only be enabled where a regexp was
> expected, but there you're getting into the internals of perl,
> about which I know nothing. I don't know whether the parser
> could "know" at that point that the string was originally a
> double-quoted literal.
But aren't DOS filenames specified like "\\d:...", not "\d:.."?
The warning I'm talking about here is for *redundant* slashes
that get ignored.
> > Or is there a "legitimate" need to specify a regex as a
> > string?
>
> That's a question that could do with being clarified.
> I'm not sure whether you mean as a string literal,
> or as a generic scalar EXPR.
I mean as a string literal.
Gary
--
pub 1024/C001D00D 1996/01/22 Gary Howland <gary@hotlava.com>
Key fingerprint = 0C FB 60 61 4D 3B 24 7D 1C 89 1D BE 1F EE 09 06
------------------------------
Date: Fri, 07 Nov 1997 17:39:44 GMT
From: Steve Kilbane <Steve_Kilbane@cegelecproj.co.uk>
Subject: Re: An excellent example of perl madness (or "split stupidity")
Message-Id: <b77cd$11272c.227@news.cegelecproj.co.uk>
In article <34633FBD.299C@hotlava.com>, Gary Howland <ghowland@hotlava.com> writes:
> But aren't DOS filenames specified like "\\d:...", not "\d:.."?
> The warning I'm talking about here is for *redundant* slashes
> that get ignored.
Indeed. I stand corrected. My apologies. Time to plug my brain back
in, I think...
--
<Steve_Kilbane@cegelecproj.co.uk> - All opinions are mine alone.
Kilbane's law of integration: standardise on protocols and file
formats, and the applications take care of themselves.
------------------------------
Date: 7 Nov 1997 15:36:19 GMT
From: "James Richardson" <jamesr@aethos.co.uk>
Subject: Re: Associative Array (HASH) problem
Message-Id: <01bceb92$fc2ea840$26c0a4c1@kitkat.aethos.co.uk>
Ryoko <faerielands@geocities.com> wrote in article
<34625739.1689@geocities.com>...
> I have a flat file that looks something like:
>
> Paul::Jones::Houston::Texas::150
> Jack::Jones::Houston::Texas::150
> Paul::Jackson::Houston::Texas::150
> Kelly::Maxwell::Detroit::Michigan::20
> Matt::Howard::Pasadena::California::125
> Matt::Howard::Pasadena::Arkansas::125
> Oliver::Twist::Yonkers::New Jersey::5
> Fred::Flinstone::Hollywood::Californiay::70
>
> The numbers at the end are scores that each player received after
> playing a game. I wish to set up an associative array with the scores as
> the KEYs of this assoc. array. Can the score '150', for example, be
> associated with 'Paul::Jones::Houston::Texas' in one instance, and
> 'Jack::Jones::Houston::Texas' in another? I would like the assoc. array
> to look like this:
[...]
>
> My ultimate goal is to be able to SORT all the records according to
> their score (in descending order -- I have that taken care of). For
> players who have identical scores, they would appear next to each other
> in the sorting process.
>
Unfortunately you just cant store entries with the same key in a hash..
(Its just the way they work!!!)
In the normal manner of perl, theres a couple of ways to do what you
want...
Heres one:
Why not store the people that got the same scores as a list...this is vv
easy to do, and also means that the sorting/printing at the end of the
program is easy to do as well....
So, changing your original program a little bit...
while (<DATA>) {
chop;
$item = $_;
#I changed the split here..
@item_line = split(/::/, $item);
$the_score = $item_line[4];
$the_rest = join("::", $item_line[0], $item_line[1], $item_line[2],
$item_line[3]);
#And modded the following line too...
push(@{$SCORE{$the_score}},$the_rest);
}
print;
__DATA__
Paul::Jones::Houston::Texas::150
Jack::Jones::Houston::Texas::150
Paul::Jackson::Houston::Texas::150
Kelly::Maxwell::Detroit::Michigan::20
Matt::Howard::Pasadena::California::125
Matt::Howard::Pasadena::Arkansas::125
Oliver::Twist::Yonkers::New Jersey::5
Fred::Flinstone::Hollywood::Californiay::70
__END__
This is what the program outputs....
%SCORE = (
125 => ARRAY(0x40131570)
0 'Matt::Howard::Pasadena::California'
1 'Matt::Howard::Pasadena::Arkansas'
150 => ARRAY(0x400e3b20)
0 'Paul::Jones::Houston::Texas'
1 'Jack::Jones::Houston::Texas'
2 'Paul::Jackson::Houston::Texas'
20 => ARRAY(0x4012bba4)
0 'Kelly::Maxwell::Detroit::Michigan'
5 => ARRAY(0x4011e744)
0 'Oliver::Twist::Yonkers::New Jersey'
70 => ARRAY(0x4011e624)
0 'Fred::Flinstone::Hollywood::Californiay'
)
Hope that helps a little bit....
James
------------------------------
Date: Fri, 07 Nov 1997 16:31:26 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Associative Array (HASH) problem
Message-Id: <346b3b3f.6191310@news.tornado.be>
Ryoko <faerielands@geocities.com> wrote:
>I would like the assoc. array to look like this:
>
>{ 150, 'Paul::Jones::Houston::Texas',
> 150, 'Jack::Jones::Houston::Texas',
> 5, 'Oliver::Twist::Yonkers::New Jersey',
>
>etc... }
>So, the first time around, the score '150' is associated with
>'Paul::Jones::Houston::Texas'. The second time around, the score '150'
>is associated with 'Jack::Jones::Houston::Texas', and the first
>association is REPLACED.
Which is what I would expect. One key, one value.
>My ultimate goal is to be able to SORT all the records according to
>their score (in descending order -- I have that taken care of). For
>players who have identical scores, they would appear next to each other
>in the sorting process.
Aha! But you don't need to do it that way. Just tweak sort so that it
sorts the way you want.
%score = ( 'Paul::Jones::Houston::Texas' => 150,
'Jack::Jones::Houston::Texas' => 150,
'Oliver::Twist::Yonkers::New Jersey' => 5 };
#Oi! Ordinary brackets! You don't want a ref to a hash!
($\,$,) = ("\n",":\t");
foreach $name (sort { $score{$b} <=> $score{$a} || $a cmp $b }
keys %score) {
print $name, $score{$name};
}
This doesn't do exactly what you want, but it prints the scores in
descending order, and for identical scores, the names in alphabetical
order. A good starting point, I presume.
Bart Lateur
bart.mediamind@tornado.be
------------------------------
Date: Fri, 07 Nov 1997 12:40:41 -0500
From: Matt Pezzuto <mpezzuto@link.com>
Subject: Chomp vs Chop
Message-Id: <34635299.41C6@link.com>
My one friend and I got in an
argument about chomp vs chop.
My one friend is an perl guru
and I am a newbie and I'm
reading books and stuff. Well,
I few weeks ago I had my friend
write a perl script for me and
he used chop to get ride of an
eol. While I was learning perl
I learned it was safer to use
chomp if your only purpose is
to get ride of an eol. I asked
my friend why he didn't use
chomp. He didn't know about it
and asked what it did and I told
him. But after thinking for a
second he replied by saying,
"When would ever not know what
your chopping off?" I failed to
come up with an example and he
claimed that using chomp was half-hazard
way of programming because the
programmer should know whether or
not an eol is expected to not and
chop is quicker than chomp because
it doesn't have to check to make
sure the last character is an eol.
I replied with then why did they
make chomp? I do not totally agree
with him but I have to admit that
he has a point. Who is right, the
newbie or the guru?
------------------------------
Date: 7 Nov 1997 14:47:31 GMT
From: lusol@turkey.cc.Lehigh.EDU
Subject: Re: Command line options
Message-Id: <63v9m3$195e@fidoii.cc.Lehigh.EDU>
>: ($arg0) = $ARGV[0] =~ s/-t//;
>: ($arg1) = $ARGV[1] =~ s/-t//;
>
>: should do it.
>
>No, this still returns 1.
Ops, thinking whilst not reading:
($arg0) = $ARGV[0] =~ /-t(.*)/;
($arg1) = $ARGV[1] =~ /-t(.*)/;
That's what I had in mind. In any case, s/// is defined to return the count,
which can be > 1 if using g.
------------------------------
Date: Fri, 07 Nov 1997 15:24:37 GMT
From: bodhi1@nonamesleft.com (bodhi1@nonamesleft.com)
Subject: Re: crontab -l reformating (Reinvent the wheel?)
Message-Id: <63vc0f$rqm@argentina.earthlink.net>
Good call. That's exactly why I put Normal human beings.
I need to publish cron job schedules for my end users. (Who range from Unix
gurus to What's a computer type). I want to put it out on our intranet, and do
it in an automated manner on a daily basis.
Something that organizes it by time, what jobs are going to execute that day
on which machine. We have about 150 machines, so if there is a good way to
capture both User and Machine, that's bonus.
In article <34605294.66FF@dagoba.org>, Jedi Master Yoda <yoda@dagoba.org>
wrote:
>Martien Verbruggen wrote:
>>
>> In article <63nbgu$jdv@suriname.earthlink.net>,
>> bodhi1@nonamesleft.com (bodhi1@nonamesleft.com) writes:
>> > I am looking for any Perl scripts that will allow me to take the output of
>> > crontab -l and format it into something that is readable by normal human
>> > beings. It would be ideal if it would put it into a .html format.
>>
>> In what way is the output of 'crontab -l' not human readable? I am perfectly
>> capable of reading it, and I am not an alien...
>
>He said 'normal' human beings...
>
>What would be nice would be a script that takes crontab -l and outputs:
>
>On the 1st of every month, at 9.15am: /home/admin/foo.sh
>At 2 minutes past every hour, between 9am and 6pm: /home/admin/bar.sh
>Every Thursday morning at 6am: /home/admin/baz.sh
>&c.
>
>There's something to while away the long winter evenings with...
------------------------------
Date: Fri, 07 Nov 1997 15:27:44 GMT
From: sjuneau@microtec.net (Sylvain Juneau)
Subject: entering data
Message-Id: <346332d6.88048343@news.cmc.ec.gc.ca>
I have this problem: when I enter $answer and press return it just sit
there and does not execute the rest of the sub unless i ctrl-d the
thing. any idea why?
sub send {
print "Enter the name of the bulletin to resend\n";
chop($bulletin=<STDIN>);
$bulletin =~ tr/a-z/A-Z/; # put everything in uppercase
@filelisting= `ls /usr/local/cis/OUT/alpha/`;
$numberoffiles=$#filelisting + 1;
for($i=0;$i<$numberoffiles;$i++){
open(FILE, "/usr/local/cis/OUT/alpha/$filelisting[$i]") || die"can't
open: $!\
n";
$/ ="\001";
$* = 0;
while (<FILE>){
if ($_=~/$bulletin/){
@a[$i]=$_}}
close FILE;
open(OUTPUT,">header");
foreach $I (@a) {
print OUTPUT $I;
}
close (OUTPUT);
}
@b= `grep '$bulletin' header`;
print @b;
print "\nIs this the bulletin(s) that you want to resend?";
$answer=<STDIN>;
etc...................
Sylvain
e-mail sjuneau@microtec.net
------------------------------
Date: Fri, 07 Nov 1997 09:50:42 -0600
From: Brad Bradley <brad.bradley@bridge.bellsouth.com>
Subject: Re: file not found error?
Message-Id: <346338D2.A6D7EB36@bridge.bellsouth.com>
> I have a snippet of code:
>
> chdir("/export/0/webman/checkin_server/data/") || print "1\n\n";
> @sched = `/bin/cat $username.sched`; print @sched;
Michael,
It looks like the single quotes are what is throwing you off. Perl won't
interpret any variable stored inside the single quotes, thuis your username
never gets trenslated during runtime. Try "" to set off the command and you
should have no problem.
Brad Bradley
------------------------------
Date: Fri, 07 Nov 1997 15:13:19 GMT
From: ron@farmworks.com (Ronald L. Parker)
Subject: Re: Help with ugly regex
Message-Id: <34632ef0.7052661@news.supernews.com>
On Fri, 7 Nov 1997 01:31:08 GMT, ebohlman@netcom.com (Eric Bohlman)
wrote:
>schadsey@coralsys.com wrote:
[snip]
>: 6) word(number,score number)
>: 7) word(number, number, number..., score number)
[snip]
>$elem="\\d+";
>$list="$elem(\\s*,\\s*$elem)*(\\s*score\\s*$elem)?";
This matches "123 score 456" which is not valid, and it
doesn't match "123,score 456" which is. How about this:
$list="$elem(\\s*,\\s*$elem)*(\\s*,\\s*score\\s*$elem)?";
>$term="\\w+\\s*\\(\\s*$list\\)?";
>$exp="^$term(\\s*;\\s*$term)*\$";
>print $exp,"\n";
------------------------------
Date: 7 Nov 1997 16:52:24 GMT
From: bod@compusol.com.au (Brendan O'Dea)
Subject: Re: interrupt system("$cmd") if timeout
Message-Id: <63vh08$1s0$1@diablo.compusol.com.au>
In article <34630D84.1B4BCB7B@torus.eng.yale.edu>,
Helina Chan <helina@torus.eng.yale.edu> wrote:
>Cameron Simpson wrote:
>
>> Helina Chan <helina@torus.eng.yale.edu> writes:
>> | I could not find information about interrupting
>> | a process started by system("$cmd") if a specified timeout
>> | occured.
[snip]
>> Use fork() instead of system. In the child, set an alarm for 75 seconds.
>> Then wait() in the parent, more or less as normal.
[snip]
> Thanks for your help. However, it does not seem to work.
>The problem is that, at least tested on my linux machine,
>sh spawns another process. So after the sh process timed
>out, its child continues. Here is an example code:
Do you need to spawn a shell? Cameron's suggestion works for a plain
rsh:
#!/usr/bin/perl
($timeout, $host, @cmd) = @ARGV;
unless (defined ($pid = fork)) { die "can't fork $!"; }
unless ($pid) { alarm $timeout; exec 'rsh', $host, @cmd; };
wait;
if ($? & 255) { print "terminated by signal ", $? & 255, "\n"; }
else { print "exited with status ", $? >> 8, "\n"; }
although it won't if you change the exec to something like:
exec 'sh', '-c', "rsh $host @cmd";
since shells tend to reset alarms and such like. If you have to call
a shell, you will have to timeout the wait in the parent instead, then
kill off the child if the timer expires:
# parent
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm $timeout;
wait;
alarm 0;
};
if ($@)
{
die if $@ ne "alarm\n";
kill 15, $pid;
wait;
}
Regards,
--
Brendan O'Dea bod@compusol.com.au
Compusol Pty. Limited (NSW, Australia) +61 2 9809 0133
------------------------------
Date: Fri, 7 Nov 1997 10:29:28 -0500
From: "Victor A. Cuya" <vcuya@mindspring.com>
Subject: Re: make ftp on perl script with windows 95/NT
Message-Id: <63vc99$71@camel20.mindspring.com>
Tasuka Amano Hsu wrote in message
<01bceb40$cff05640$0301a8c0@w95.tailyn.com.tw>...
>I want make a program to do ftp to the other site on network,
>with perl script and MS-windows 95/NT how to do that ?
>
>PS:I use Perl 5.001 on MS-Windows 95
>
>Tasuka Amano
Use the Win32::Internet extension. It is a very clean process, and you can
even do subdirectories as well....
Victor A. Cuya
vcuya@mindpsring.com
victor_cuya@ccmail.prusec.com
------------------------------
Date: Fri, 07 Nov 1997 11:26:20 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: Perl variable filehandles
Message-Id: <comdog-ya02408000R0711971126200001@news.panix.com>
In article <346329BF.332E@cs.purdue.edu>, corcordt@cs.purdue.edu wrote:
>I would like to be able to select that handle to print to
>such as a print {$users[$i++]} "Welcome to port xxx.\n";
how about
$users[$i++] = new FileHandle;
? good luck :)
--
brian d foy <comdog@computerdog.com>
NY.pm - New York Perl M((o|u)ngers|aniacs)* <URL:http://ny.pm.org/>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
------------------------------
Date: Fri, 7 Nov 1997 09:14:30 -0600
From: "Rick" <rlluhman@netins.net>
Subject: Perl5 for IntraNetware and PATH_INFO
Message-Id: <63vb9u$3sn$1@news.iastate.edu>
I am wondering why the following script (named testenv.cgi) does not appear
to work with Novell's Webserver 3.1 (LCGI).
print "Content-type: text/html\n\n";
print "path info = <b>$ENV{'PATH_INFO'}</b>\n";
When I invoke this script (http://domain/perl/testenv.cgi) without any
additional path information I get:
path info = /
When I add information to the URL that runs this script
(http://domain/perl/testenv.cgi/addedinfo) I get a dialog box that indicates
"document contains no data"rather than
path info = /addedinfo
------------------------------
Date: Fri, 7 Nov 1997 10:44:59 -0500
From: "Mark Gibbs" <gibby@tcol.net>
Subject: Please Help Perl 32 under Nt4 "Path"
Message-Id: <63vcsh$8jv$1@news2.cais.com>
can someone help me I have perl 5 installed under NT 4.0 with microsoft iis
ver 3.
We have several websites on our server and for some reason If i put a perl
file under a cgi-bin dir on any website it says can not find
"c:/www-r/worldwidespecialties/cgi-bin/test.pl"
now I put the file under www.aloaide.com/cgi-bin/test.pl
also if I view another web site on our server and go to the cgi-bin dir
it is not showing what is in that cgi-bin dir but what is in
worldwidespecialties dir
this is crazy..... can anyone please help me out
thankyou
Mark Gibbs
Treasure Coast Online
561-781-0866
Fax 561-781-0867
------------------------------
Date: Fri, 07 Nov 1997 15:30:42 GMT
From: ron@farmworks.com (Ronald L. Parker)
Subject: Re: Puzzle: palindromep
Message-Id: <346430ae.7498482@news.supernews.com>
On Fri, 7 Nov 1997 00:10:38 GMT, ebohlman@netcom.com (Eric Bohlman)
wrote:
>Still, it can be done without an explicit stack:
>
>sub is_palindrome($) {
> my $s=shift;
> my $i,$j;
> for ($i=0,$j=length($s)-1; $i<$j; $i++,$j--) {
> return 0 if substr($s,$i,1) ne substr($s,$j,1);
> }
> return 1;
>}
>
>or, since TMTOWTDI:
>
>sub is_palindrome($) {
> my $l=int(length($_[0])/2);
> substr($_[0],0,$l) eq reverse substr($_[0],-1,$l);
>}
How about just
sub is_palindrome($) {
$_[0] eq reverse($_[0]);
}
or even
sub is_palindrome($) {
$x = uc(shift);
$x =~ s/\W//g;
$x eq reverse($x);
}
which comes closer to what's desired by correctly identifying
palindromes with random capitalization and with inserted spaces and
punctuation, such as "A man, a plan, a canal: Panama!"
But none of these do what the original poster was looking for, which
is a way to extract a palindromic sequence from a string, while
ignoring whitespace (and presumably punctuation.) See my earlier
posting for a way to do this using a regex and a while loop, though it
doesn't find the palindromic sequence of maximal length, preferring
the one the terminates sooner (well, actually the one whose center
occurs earliest, though it can be shown that these are equivalent.)
Whether this is the desired result or not wasn't clear from the
problem specification; therefore it is the desired result.
------------------------------
Date: Fri, 07 Nov 1997 11:36:42 -0600
From: dan weaver <d-weaver@nwu.edu>
Subject: reading from end of file
Message-Id: <346351AA.658C@nwu.edu>
I would like to read a file from the end since it is a very large text
file. Is there any way to open a file for reading lines of text
backwards, starting fromthe last line? I checked the FAQ.
If you could, email me at d-weaver@nwu.edu
Dan
------------------------------
Date: 7 Nov 1997 15:52:25 GMT
From: David Turley <dturley@ravine.binary.net>
Subject: Re: Regular Expressions
Message-Id: <63vdfr$cc3$1@oksennus.binary.net>
> In article <34622109.48D6@southernvirginia.edu>, gnew@southernvirginia.edu wrote:
> >Trying to send an individuals name through a regular expression that
> >will make sure the first letter in the First Middle and Last name are
> >all upper case no matter how they were given.
Be sure you really want to do this. Some people's names begin with lower
case letters. They might not appreciate having the spelling of their name
corrected.
David Turley
dturley@pobox.com
http://www.pobox.com/~dturley
"You don't want to be the only person sitting on a crocodile's back."
-- heard on a PBS nature show
______________________________________________________________________
------------------------------
Date: Fri, 07 Nov 1997 16:31:23 GMT
From: bart.mediamind@tornado.be (Bart Lateur)
Subject: Re: Regular Expressions
Message-Id: <346637fd.5357054@news.tornado.be>
>In article <comdog-ya02408000R0611971707560001@news.panix.com>,
comdog@computerdog.com (brian d foy) writes:
> i hate this sort of data manipulation - it seems that a lot of systems
> do this - and i should know :)
"Brian D Foy"?
mgjv@comdyn.com.au (Martien Verbruggen) wrote:
>Yeah,
>
>what if your last name is McBeth, O'Donnell, or Lloyd Webber ?
Lettin' [ s/(/w+)/\u\L$1/g ] loose on these, I get:
Mcbeth (oops)
O'Donnell (ok)
Lloyd Webber (ok)
Bart.
------------------------------
Date: Fri, 7 Nov 1997 15:57:42 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Sorting integers in Perl as an array
Message-Id: <EJA9o7.HK7@world.std.com>
syarbrou@ais.net (Steve) writes:
[sorting by number the list]
>102|/etc/pub/host.htm
>25|/pub/home/john.htm
Since perl implicitly converts scalars between numbers and strings,
and since it stops converting at the firsts non-numeric characters (in
this case the pipe character) sorting is as simple as comparing the
elements numerically with a comparison routine (as documented in the
entry for sort in the perlfunc man page.)
@sorted = sort { $a <=> $b } @list;
But since the "-w" thought police have gotten to the later versions of
perl, you get some spurious warnings with the warning switch enabled.
Constructs that will run without warnings, and will allow you to sort
on any (not just the first) column can be found in the FAQ entry "How
do I sort an array by (anything)?"
<URL:http://www.perl.com/CPAN/doc/manual/html/pod/perlfaq4/
How_do_I_sort_an_aray_by_anyth.html>
--
Andrew Langmead
------------------------------
Date: Fri, 07 Nov 1997 18:36:30 +0100
From: Per Kistler <kistler@inf.ethz.ch>
Subject: tcl subst in perl?
Message-Id: <3463519E.2EAF473E@inf.ethz.ch>
Hi All
What is the tcl "subst" in perl? If I have a string with
"$var" in it, and want to eval the $var, but NOT to
execute it, then, in tcl I can use: set a [subst $string].
What is this in perl?
Thanks, Per.
--
Per Kistler, Unix Systems Administrator, kistler@inf.ethz.ch
http://www.cs.inf.ethz.ch/~kistler/
Swiss Federal Institute of Technology, Zuerich, Switzerland
---------------------------------------------------------------------
------------------------------
Date: Fri, 07 Nov 1997 10:50:47 -0500
From: Swaroop Kumar <skumar@concentric.net>
Subject: Traversing a directory tree in Perl
Message-Id: <346338D6.9CA9D046@umi.com>
Hi:
I'm relatively a newbie in the world of Perl. I would like to know
how to traverse a directory tree using Perl. I've written a script that
uses opendir and readdir to supposedly do the job. In this script,
there's a recursive function (subroutine) called ProcessDir that calls
itself. In this function, I open the directory that's been passed as an
argument, and proceed to loop thru' each directory entry. If its a file,
I just print its name and size. If its a directory, I call the function
(ProcessDir) recursively. As an aside, let me mention that I pass the
absolute pathname of the directory to ProcessDir. I do not process "."
and ".." entries in ProcessDir. Most variables are local (using the
"local" declaration). But, here's what happens. If I start off at the
current directory, it first prints the files it finds in the directory,
and for the first subdirectory, it prints its contents and exits,
although there are some more sub-directories in the current directory.
Its like the old directory descriptor (before the first recursive call)
gets overwritten or something. The code I'm using now is shown below.
Could someone help me out here please?
Thanx in advance,
Swaroop (skumar@umi.com)
#!/usr/bin/perl
sub ProcessDir
{
local $CurDir = shift (@_);
local $CURDIRDESC, $NextDir, $CurFile;
if (! -d $CurDir)
{
return;
}
printf "Came into ProcessDir with %s...\n", $CurDir;
opendir (CURDIRDESC, $CurDir);
READLOOP: while (1)
{
$CurFile = readdir (CURDIRDESC) || last READLOOP;
if ($CurFile eq "." || $CurFile eq "..")
{
printf "Skipping to the next iteration...\n";
next READLOOP;
}
if (-d $CurFile)
{
printf "$CurFile is a directory\n";
$NextDir = $CurDir . '/' . $CurFile;
ProcessDir ($NextDir);
printf "Out of recursive call...\n";
next READLOOP;
}
else
{
printf "$CurFile is a file\n";
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($CurFile);
if ($size > $ARGV[1])
{
printf "%s: %d\n", $CurFile, $size;
}
}
}
printf "Closing directory %s..\n", $CurDir;
closedir (CURDIRDESC);
}
if ($#ARGV > 1 || $#ARGV < 0)
{
die ('Usage: szg [start_dir] <lower_limit_on_file_size>');
}
if ($#ARGV == 1)
{
ProcessDir ($ARGV[0]);
}
------------------------------
Date: Fri, 07 Nov 1997 11:12:41 -0600
From: skumar@umi.com
Subject: Traversing a directory tree in Perl
Message-Id: <878922310.2758@dejanews.com>
Hi:
I'm relatively a newbie in the world of Perl. I would like to know
how to traverse a directory tree using Perl. I've written a script that
uses opendir and readdir to supposedly do the job. In this script,
there's a recursive function (subroutine) called ProcessDir that calls
itself. In this function, I open the directory that's been passed as an
argument, and proceed to loop thru' each directory entry. If its a file,
I just print its name and size. If its a directory, I call the function
(ProcessDir) recursively. As an aside, let me mention that I pass the
absolute pathname of the directory to ProcessDir. I do not process "."
and ".." entries in ProcessDir. Most variables are local (using the
"local" declaration). But, here's what happens. If I start off at the
current directory, it first prints the files it finds in the directory,
and for the first subdirectory, it prints its contents and exits,
although there are some more sub-directories in the current directory.
Its like the old directory descriptor (before the first recursive call)
gets overwritten or something. The code I'm using now is shown below.
Could someone help me out here please?
Thanx in advance,
Swaroop (skumar@umi.com)
#!/usr/bin/perl
sub ProcessDir
{
local $CurDir = shift (@_);
local $CURDIRDESC, $NextDir, $CurFile;
if (! -d $CurDir)
{
return;
}
printf "Came into ProcessDir with %s...\n", $CurDir;
opendir (CURDIRDESC, $CurDir);
READLOOP: while (1)
{
$CurFile = readdir (CURDIRDESC) || last READLOOP;
if ($CurFile eq "." || $CurFile eq "..")
{
printf "Skipping to the next iteration...\n";
next READLOOP;
}
if (-d $CurFile)
{
printf "$CurFile is a directory\n";
$NextDir = $CurDir . '/' . $CurFile;
ProcessDir ($NextDir);
printf "Out of recursive call...\n";
next READLOOP;
}
else
{
printf "$CurFile is a file\n";
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($CurFile);
if ($size > $ARGV[1])
{
printf "%s: %d\n", $CurFile, $size;
}
}
}
printf "Closing directory %s..\n", $CurDir;
closedir (CURDIRDESC);
}
if ($#ARGV > 1 || $#ARGV < 0)
{
die ('Usage: szg [start_dir] <lower_limit_on_file_size>');
}
if ($#ARGV == 1)
{
ProcessDir ($ARGV[0]);
}
-------------------==== Posted via Deja News ====-----------------------
http://www.dejanews.com/ Search, Read, Post to Usenet
------------------------------
Date: Fri, 7 Nov 1997 17:15:25 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Traversing a directory tree in Perl
Message-Id: <EJAD9p.InI@world.std.com>
Swaroop Kumar <skumar@concentric.net> writes:
> If I start off at the
>current directory, it first prints the files it finds in the directory,
>and for the first subdirectory, it prints its contents and exits,
>although there are some more sub-directories in the current directory.
>Its like the old directory descriptor (before the first recursive call)
>gets overwritten or something. The code I'm using now is shown below.
You aren't localizing the directory handle, CURDIRDESC. When you open
the handle again in your recursive call, you are closing the previous
one in its caller.
One way of doing what you want is to add:
local(*CURDIRDESC);
To you're subroutine.
Another way would be to use the DirHandle module to create directory
handles held in a scalar.
use DirHandle;
my $curdirdesc = new DirHandle, $CurDir;
$CurFile = $curdirdesc->read();
Another would be to use the File::Find module to do all the recursion
for you.
use File::Find;
my $startdir = shift;
my $size_limit = shift;
die 'Usage: szg [start_dir] <lower_limit_on_file_size>'
unless defined $startdir and defined $size_limit and $#ARGV < 0;
find(\&print_big_files, $startdir);
sub print_big_files {
return unless -f $_;
my $size = -s _;
print "$File::Find::name: $size\n" if $size > $size_limit;
}
--
Andrew Langmead
------------------------------
Date: Fri, 07 Nov 97 12:08:33 -0500
From: bsa@void.apk.net (Brandon S. Allbery KF8NH; to reply, change "void" to "kf8nh")
Subject: Re: Undefining an associative array element
Message-Id: <34634b31$4$ofn$mr2ice@speaker>
In <63rmmr$39r@gap.cco.caltech.edu>, on 11/06/97 at 06:05 AM,
wart@ugcs.caltech.edu (Wart) said:
+-----
| undef $info{$key_to_delete};
+--->8
You want "delete $info{$key_to_delete}".
--
brandon s. allbery [Team OS/2][Linux] bsa@void.apk.net
cleveland, ohio mr/2 ice's "rfc guru" :-) KF8NH
------------------------------
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 1286
**************************************