[11584] in Perl-Users-Digest
Perl-Users Digest, Issue: 5184 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Mar 20 02:07:25 1999
Date: Fri, 19 Mar 99 23: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, 19 Mar 1999 Volume: 8 Number: 5184
Today's topics:
Re: 2d-array sort (Larry Rosler)
Re: [Q] Big-endian IEEE floats I/O with perl (Larry Rosler)
Re: Algorithm to split words into "encyclopedia labels" (Andrew M. Langmead)
Re: Better Sort, Please (Ronald J Kimball)
convert a file chongchanlee@hotmail.com
Re: Days in Month array (Larry Rosler)
Re: Days in Month array (Larry Rosler)
Re: dir command in perl? <cassell@mail.cor.epa.gov>
Re: Filehandle Q... (Larry Rosler)
Re: getting keys of hashes of hashes <jglascoe@giss.nasa.gov>
Re: Help: Conditional use of packages in Perl (Ronald J Kimball)
Re: How do I split an list of text into separate lists (Larry Rosler)
Re: How to sort a LoL? (Larry Rosler)
Re: How to sort a LoL? (Larry Rosler)
Re: Input for PERL Course request (David H. Adler)
Re: join lines from <textarea> (brian d foy)
Re: Mac Newbie: droplet question (Ronald J Kimball)
Re: MSIE Does Not Undestand "IMGSIZE" (Larry Rosler)
Re: Need Faster Approach (Ronald J Kimball)
Re: new on subroutines (Larry Rosler)
Newbie Regexp Replace Question dingus69@my-dejanews.com
Re: Newbie Regexp Replace Question (Tad McClellan)
Perl Compiler. <jcokos@ccs.net>
Re: Simple perl question <cassell@mail.cor.epa.gov>
Re: Simple perl question (Tad McClellan)
Re: Simple perl question (Tad McClellan)
Re: ternary operator (Larry Rosler)
Re: ternary operator (Larry Rosler)
Re: Variable splitting (Larry Rosler)
Web Server Automation <jcokos@ccs.net>
Re: Which OS am I in (Larry Rosler)
XML::Parser - 'Objects' style <zzhao@execulink.com>
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 17 Mar 1999 05:39:15 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: 2d-array sort
Message-Id: <MPG.11595059c3bd24d698976d@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36EF913D.C8D68A9A@gredos.cnb.uam.es> on Wed, 17 Mar 1999
12:25:50 +0100, Federico Abascal <fabascal@gredos.cnb.uam.es >says...
> >
> > @sorted = sort { $a->[0] <=> $b->[0] } @Data1;
>
> I have tried this way of sorting an array of arrays but it seems it doesn't work
>
> @arrayOfarrays = (["Hello", 23], ["Bye", 4]);
> @ordenado = sort({ $a->[1] <=> $b->[1] } @arrayOfarrays);
> foreach $tt (@ordenado) {
> print "\n @$tts \n";
> }
>
> Is any error in my code?
Indeed there is. Your loop iterator is named $tt, but your array
dereference is @$tts ! The '-w' flag would have pointed out the use of
variables only once and the use of uninitialized values; the 'use
strict;' pragma would force you to declare all variables so this
couldn't happen.
Perl gives you simple programming-support tools. Use them!
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 08:34:08 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: [Q] Big-endian IEEE floats I/O with perl
Message-Id: <MPG.1159795de7015a4f989776@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36EFCBEB.5AD74BD@ltt.ntua.gr> on Wed, 17 Mar 1999 17:36:11
+0200, Costis Angelis <loulou@ltt.ntua.gr >says...
> I handle very often large files containing float numbers, single
> precision in binary format. Since I work both on Linux and IRIX, I would
> like to know if there is any easy way to deal with portability problems.
>
> Though I guess those who could help me out understand what I mean, let
> me explain a little better what I am looking for. I am no Perl expert,
> but I managed to find in the man pages that you can write 'network'
> format integers consistently, so that they are portable across various
> CPU architectures. However, the man page says that this is not
> respectively implemented for floats, as there is no similar standard
> convention.
But there is a similar standard convention, and it predates Perl!
There is an IEEE standard representation for both single- and double-
precision floating-point numbers, which is by now universal among
hardware systems.
You can use pack and unpack with 'f' and 'd' specifiers quite reliably
among systems with the same "endian-ness". Between systems of opposite
"endian-ness" you have to reverse the byte order. A simple run-time
experiment can determine what the situation is. (I have posted it
before -- compare the results of pack/unpack 'N' and 'L', for example.)
Hmmm. I just took another look at the Subject of your post, and see
that you are aware of endian and IEEE issues. Maybe you are just overly
concerned about the man pages that you quote without citation. Which
are they? Perhaps they should be fixed.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 20 Mar 1999 06:10:23 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Algorithm to split words into "encyclopedia labels"
Message-Id: <F8vqHC.89C@world.std.com>
"Paul J. Lucas" <pjl@be-NOSPAM-st.com> writes:
> OK, how about s simpler problem: what's an easy way in Perl to
> tell how much of two strings match?
> $a = 'Hello';
> $b = 'Helicopter';
$diffs = $a ^ $b; # find the bits that are different between the strings
($nulls) = ($diffs =~ /^(\0*)/); # extract the initial matching bits.
$length = length($nulls); # determine the length of the initial match
print "$length characters match\n";
--
Andrew Langmead
------------------------------
Date: Sat, 20 Mar 1999 01:43:16 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Better Sort, Please
Message-Id: <1doxv6f.hgdcew1qbr9vpN@p216.tc2.state.ma.tiac.com>
Larry Rosler <lr@hpl.hp.com> wrote:
> Last month, Andrew Langmead showed how to avoid the overhead of a
> subroutine call on each comparison, using a typeglob. Paraphrasing:
>
> {
> local *sub_sort = build_sort_sub();
> @sorted = sort sort_sub @a;
> }
Still has that same darn typo, though...
{
local *sort_sub = build_sort_sub();
@sorted = sort sort_sub @a;
}
--
chipmunk (Ronald J Kimball) <rjk@linguist.dartmouth.edu>
perl -e 'print map chop, sort split shift, reverse shift
' 'j_' 'e._jP;_jr/_je=_jk{_jn*_j &_j :_j @_jr}_ja)_js$_j
~_jh]_jt,_jo+_jJ"_jr>_ju#_jt%_jl?_ja^_jc`_jh-_je|' -rjk-
------------------------------
Date: Sat, 20 Mar 1999 06:38:47 GMT
From: chongchanlee@hotmail.com
Subject: convert a file
Message-Id: <7cvfpm$ne5$1@nnrp1.dejanews.com>
I have a data file written in 16-bit binart little endian format of 43Mb.
I can't read this file. If anyone knows how to read or convert (if necessary)
this file, please help me.
I have Pentium II with 450mh with 256MB RAM in Window NT 4.0.
Thank you in advance.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Wed, 17 Mar 1999 10:39:24 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Days in Month array
Message-Id: <MPG.115996b4653db545989778@nntp.hpl.hp.com>
In article <J2SH2.857$Vl4.25100@newsfeed.slurp.net> on Wed, 17 Mar 1999
17:58:01 GMT, Craig Berry <cberry@cinenet.net> says...
> Jay Glascoe (jglascoe@giss.nasa.gov) wrote:
> : > I'm trying to build a timeline of days and dates when given a
> : > year. Leap years are messin with my code.
> : >
> : > How does one calculate if a given year is a leap year?
> :
> : isn't it simply
> :
> : print "leap year\n" if $year % 4 == 0;
>
> Nope.
Yep, for 1900 < $year && $year < 2100 (also, BTW, for 0 < $year && $year
< $n where $n is described below).
> my $isLeap = ($year % 4 == 0 &&
> $year % 100 != 0) ||
> $year % 400 == 0;
Yep, for $year > $n where $n is location-dependent (> ~ 1572 for Roman
Catholic countries, > ~ 1752 for Protestant countries, > ~ 1917 for
Eastern Orthodox countries, ...).
> That is, a year is a leap year if:
>
> * It is divisible by four, unless
> * It is a century year, unless
> * It is a 400-year multiple.
>
> Putting it yet another way, every four years is a leap year, except
> non-400-multiple century years. So 1996 and 2000 are leap years, but 1900
> and 2100 are not.
Every function has domains for its arguments over which the value of the
function is valid. These domains are part of the specification of the
function. Assertions without the domain specification are invalid or
(at best) incomplete.
--
Larry Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 12:30:19 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Days in Month array
Message-Id: <MPG.1159b0b3897bae5498977a@nntp.hpl.hp.com>
In article <36F008D5.8162E20B@giss.nasa.gov> on Wed, 17 Mar 1999
14:56:05 -0500, Jay Glascoe <jglascoe@giss.nasa.gov> says...
...
> > print "biz!\n" unless $year % 4 or not $year % 100 and $year % 400;
>
> however, my favorite by far was:
>
> On Wed, 10 Mar 1999, Abigail wrote:
> >
> > By checking whether the day following Feb 28 is in February.
> >
> > sub is_leap {
> > require Time::Local;
> > my $year = shift;
> > my $time = Time::Local::timelocal (0, 0, 12, 28, 1, $year);
> > my $date = localtime ($time + 24 * 60 * 60);
> >
> > substr ($date, 4, 3) eq 'Feb';
> > }
That is orders of magnitude better^Wslower than the other dull
expressions, and orders of magnitude funnier, too.
--
Larry Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 19 Mar 1999 17:13:45 -0800
From: "David L. Cassell" <cassell@mail.cor.epa.gov>
Subject: Re: dir command in perl?
Message-Id: <36F2F649.FD9D2F16@mail.cor.epa.gov>
Abigail wrote:
>
> josri (josri@earthlink.net) wrote on MMXXVI September MCMXCIII in
> <URL:news:36F2CDB2.A024A42@earthlink.net>:
> ** hi
> ** how can i run "dir" command with perl in windows?
> **
> ** I tried
> ** $dir='dir';
>
> Look up system(), open(), exec() and the `` operator.
I missed the originating post, but the problem seems simpler than
this. Josri, you used the single quote ' when you needed to use
the backtick ` . In ActivePerl build 509, this works just
like you would want it to.
But Abigail is right [as always]. If you want to know how
to handle a variety of conditions, make sure you have more
than one tool in your bag. Knowing the difference between
system(), exec(), and `` will save you a lot of headaches
further down the road.
HTH,
David
--
David L. Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Wed, 17 Mar 1999 08:15:25 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Filehandle Q...
Message-Id: <MPG.115974fbc2a44200989774@nntp.hpl.hp.com>
In article <4mnnc7.149.ln@magna.metronet.com> on Wed, 17 Mar 1999
03:04:20 -0500, Tad McClellan <tadmc@metronet.com >says...
>
> system("whois $domain >$out_file) && die "system(whois) failed $!";
You mean "$?" (not "$!"). Force of habit, I guess.
perldoc -f system
"You can check all the failure possibilities by inspecting $? like this:
..."
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 19 Mar 1999 20:00:16 -0500
From: Jay Glascoe <jglascoe@giss.nasa.gov>
To: dwatanab@uci.edu
Subject: Re: getting keys of hashes of hashes
Message-Id: <36F2F320.239A8B87@giss.nasa.gov>
dana watanabe wrote:
>
> I'm trying to be incredibly lazy and avoid declaring (or 'my'ing) variables
> for foreach statements by giving up on foreach altogether
> and just using for (which gives you $_ which is a lot less work to type anyway)
As Alan already stated, "foreach" and "for" are synonymous
keywords in Perl: they both fool around with "$_".
> But i've created a hash of a hash of a hash and was wondering if i could
> 'for' through it.
>
> Three questions:
> Is this possible?
mmm. Yeah, I guess.
$a{a}{b}{c}=1;
$a{a}{b}{d}=2;
$a{a}{b}{e}=3;
$a{c}{b}{e}=4;
$a{c}{d}{e}=5;
my @foo = ();
for (keys %a) {
push @foo, $_;
for (keys %{ $a{$foo[0]} }) {
push @foo, $_;
for (keys %{ $a{$foo[0]}{$foo[1]} }) {
push @foo, $_;
print "@foo\n";
pop @foo;
}
pop @foo;
}
pop @foo;
}
> Is this possible, but bad to do?
sub flatten {
my @res = ();
my @stack = ();
sub flatten_helper {
my ($href, $depth, $aref, $res) = @_;
unless (--$depth) {
push @$res, [@$aref];
return;
}
while (my ($k, $v) = each %$href) {
push @$aref, $k;
flatten_helper($v, $depth, $aref, $res);
pop @$aref;
}
}
flatten_helper(@_, \@stack, \@res);
return \@res;
}
use Data::Dumper;
print Dumper flatten(\%a, 4);
> Is using for instead of foreach bad?
yes! uh, no! I mean, ummm, okay.
Jay Glascoe
--
Fiery Phoenix
Oh seven-record splendor
We will correct you
------------------------------
Date: Sat, 20 Mar 1999 01:43:18 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Help: Conditional use of packages in Perl
Message-Id: <1doxvls.s3jk3h1u3l17cN@p216.tc2.state.ma.tiac.com>
Larry Rosler <lr@hpl.hp.com> wrote:
> BEGIN {
> if (condition is true)
> use package1;
> else
> use package2;
> }
*bonk*
A) The BEGIN block has to be compiled, and 'use' is still a compile-time
directive.
B) Test this solution some time. You'll see.
--
_ / ' _ / - aka - rjk@linguist.dartmouth.edu
( /)//)//)(//)/( Ronald J Kimball chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
"It's funny 'cause it's true ... and vice versa."
------------------------------
Date: Wed, 17 Mar 1999 06:08:32 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How do I split an list of text into separate lists of paragraphs
Message-Id: <MPG.115957355dec5e7e989770@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36ef8688.8786948@news> on Wed, 17 Mar 1999 11:03:36 GMT, Jim
Britain <jbritain@home.com >says...
> There's got to be a better way to break text to paragraphs..
<SNIP of use of Text::Format>
> However, I would still like to know how to separate paragraphs to an
> individual (indexed) list of paragraphs.. (without going more nuts).
If you are reading the text from a file, you might find the special
variable $/ (a.k.a. $INPUT_RECORD_SEPARATOR) useful. Look in perlvar.
If your text is in a string, use 'split /\n\n/' or 'split /\n\n+/'.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 06:13:08 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How to sort a LoL?
Message-Id: <MPG.1159584741ef2578989771@nntp.hpl.hp.com>
In article <36EF8CFE.2EEF5522@gredos.cnb.uam.es> on Wed, 17 Mar 1999
12:07:43 +0100, Federico Abascal <fabascal@gredos.cnb.uam.es >says...
> I have an array of two element arrays, and I want to sort it by the
> number contained in the second element of each of the arrays of the
> array. (In C/C++: sort by array[i][1]). Do you know how to do it?
You answered yourself in the following post a few minutes later, which
got here first:
@arrayOfarrays = (["Hello", 23], ["Bye", 4]);
@ordenado = sort({ $a->[1] <=> $b->[1] } @arrayOfarrays);
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 08:19:53 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How to sort a LoL?
Message-Id: <MPG.115976063db7c772989775@nntp.hpl.hp.com>
In article <uopnc7.149.ln@magna.metronet.com> on Wed, 17 Mar 1999
03:39:58 -0500, Tad McClellan <tadmc@metronet.com >says...
> Federico Abascal (fabascal@gredos.cnb.uam.es) wrote:
>
> : I have an array of two element arrays,
...
> : and I want to sort it by the
> : number contained in the second element of each of the arrays of the
> : array. (In C/C++: sort by array[i][1]). Do you know how to do it?>
...
> my @ra = ( ['Bubba', 250],
> ['Joe', 200],
> ['Mary', 225]
> );
>
> # Schwartzian Transform
> my @sorted = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] }
> map { [ $_, $$_[1] ] } @ra;
Oopsie! Second time this has come up in a week. Both Randal and I
noticed it.
Maybe we need a new algorithm called the Schwartzian Overkill.
my @sorted = sort { $a->[1] <=> $b->[1] } @ra;
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 20 Mar 1999 00:27:51 -0500
From: dha@panix.com (David H. Adler)
Subject: Re: Input for PERL Course request
Message-Id: <slrn7f6cem.8rm.dha@panix.com>
On Fri, 19 Mar 1999 00:30:49 -0500, Ronald J Kimball
<rjk@linguist.dartmouth.edu> wrote:
>aspect of Tim Christiansen's Camel reviews is reading the ones where he
^^^
Hmm... Is he new? :-)
dha
--
David H. Adler - <dha@panix.com> - http://www.panix.com/~dha/
Perl can certainly be used as a first computer language, but it was
really designed to be a *last* computer language. - Larry Wall
------------------------------
Date: Fri, 19 Mar 1999 21:38:03 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: join lines from <textarea>
Message-Id: <comdog-ya02408000R1903992138030001@news.panix.com>
In article <7culv4$17q$1@bison.rosnet.ru>, "Michael Yevdokimov" <flanker@sonnet.ru> posted:
> Ok, but how to restore the text structure after using join() function??
split().
--
brian d foy
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
------------------------------
Date: Sat, 20 Mar 1999 01:43:19 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Mac Newbie: droplet question
Message-Id: <1doxw0j.akg1pl11sxd0dN@p216.tc2.state.ma.tiac.com>
Rory Campbell Lange <'x'campbell-lange@easynet.co.uk> wrote:
> Thanks for the notes, Ronald
> I got the following useful advice on the applescript newsgroup:
>
> So this is what you really wanted:
>
> #!perl -w
> use strict;
> use Fcntl; # for sysopen
> my $file = shift @ARGV;
>
> if (! $file) {
This will not work properly if you have a file named '0' (zero); it will
ask you to supply another filename below.
if (not(defined $file and length $file)) {
> $file = MacPerl::DoAppleScript
> 'choose file with prompt "Please select coordinate file"';
> $file =~ s/^alias "//;
> $file =~ s/"$//;
> }
>
> sysopen(IN, $file, O_RDONLY, 0) or die "Can't open '$file': $!";
>
> Note that if sysopen() is confusing you and you know that you won't have
> leading or trailing whitespace, you can do open(IN "< $file") ... . I
> just gave you the safest version. open() is magic and sysopen() is not,
> so sysopen() is safer when $file is unknown.
Ironic that Chris was concerned enough about leading or trailing
whitespace to use sysopen() instead of open(), but he forgot about files
named '0'. :)
> Hope this helps,
>
> --
> Chris Nandor mailto:pudge@pobox.com
--
#!/usr/bin/sh -- chipmunk (aka Ronald J Kimball)
perl -e'for(sort keys%main::){print if $$_ eq 1}
' -s -- -' Just' -' another ' -'Perl ' -'hacker
' http://www.tiac.net/users/chipmunk/ [rjk@linguist.dartmouth.edu]
------------------------------
Date: Wed, 17 Mar 1999 05:44:00 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: MSIE Does Not Undestand "IMGSIZE"
Message-Id: <MPG.1159517954d4c5ed98976e@nntp.hpl.hp.com>
In article <7co3ds$a15@eccws1.dearborn.ford.com> on Wed, 17 Mar 1999
11:25:14 +0000, Richard H <"rhrh@hotmail.com,or,rhardicr"@ford.com>
<Richard H <"rhrh@hotmail.com,or,rhardicr"@ford.com> >says...
> quick q,
> where does the imgsize method come from,
> is it in any distributed module??
quick a,
Image::Size
:-)
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 20 Mar 1999 01:43:21 -0500
From: rjk@linguist.dartmouth.edu (Ronald J Kimball)
Subject: Re: Need Faster Approach
Message-Id: <1doxwbu.1scyirpj9hx3lN@p216.tc2.state.ma.tiac.com>
Larry Rosler <lr@hpl.hp.com> wrote:
> In article <7ckafs$es1@news.service.uci.edu> on 16 Mar 1999 01:00:44
> GMT, Earl Hood <ehood@medusa.acs.uci.edu> says...
> > In article <7ck0hd$krn$1@nnrp1.dejanews.com>,
> > <frogsmock@my-dejanews.com> wrote:
> >
> > >However,
> > >what I'm hoping for is a faster way (impossible?) to get to the
> > >75,000th item instead of splitting off each preceding item.
> >
> > Have looked into using index()/rindex()?
>
> How would one use either of those functions effectively to find the
> 75,000th item in a comma-delimited string, as requested? Looping
> through 75000 index() calls to find the 75,000th comma sounds like a
> *very* costly procedure.
How would you do it _without_ using index()?
--
_ / ' _ / - aka - rjk@linguist.dartmouth.edu
( /)//)//)(//)/( Ronald J Kimball chipmunk@m-net.arbornet.org
/ http://www.ziplink.net/~rjk/
perl -e'$_="\012534`!./4(%2`\cp%2,`(!#+%2j";s/./"\"\\c$&\""/gees;print'
------------------------------
Date: Wed, 17 Mar 1999 11:53:16 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: new on subroutines
Message-Id: <MPG.1159a806585092f9989779@nntp.hpl.hp.com>
In article <36efe47b.0@news.hawaii.rr.com> on Wed, 17 Mar 1999 09:17:04
-1000, Steve Bowen <bowens001nospam@hawaii.rr.com> says...
...
> #!/usr/bin/perl
> # hellowww.pl
> require "htmlend.pl";
This is relative to the current directory. When running a CGI program,
you *never* know what the current directory is, unless you set it. Of
course, an absolute path should work (until you move the program
somewhere else :-).
> print "Content-type: text/html", "\n\n";
> print "<HTML>", "\n";
> print "<TITLE>Perl meets the World Wide Web</TITLE>", "\n";
> print "</HEAD>", "\n";
> print "BODY>", "\n";
Missing an opening for the tag, though that is not your immediate
problem. Also, you should look into using '<<HERE' documents, for
readability and maintainability.
...
> Also one other question. If I say print COUNT 1;
> does this store the value one in a file called COUNT and if so where is the
> file located?
Yes, provided COUNT is a filehandle opened for a file with write
permission.
The file is located wherever you specified when you opened it.
--
Larry Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 20 Mar 1999 02:29:39 GMT
From: dingus69@my-dejanews.com
Subject: Newbie Regexp Replace Question
Message-Id: <7cv169$c1i$1@nnrp1.dejanews.com>
I'm sure this is a stupid question but i've read the FAQ and I've rtm and I
can't figure it out.
How can I use a s/ / / expression to replace a set of numbers?
e.g I have a set of strings (filenames) of the form Image51.gif through
Image78.gif which I wish to change to Fred01.gif to Fred28.gif. I can find
the bit to change by doing a /Image[5-7]/ but s/Image[5-7]/Fred[0-2]/ doesn't
change them.
Any help welcome
Dingus
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
------------------------------
Date: Fri, 19 Mar 1999 18:50:49 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Newbie Regexp Replace Question
Message-Id: <psnuc7.mo1.ln@magna.metronet.com>
dingus69@my-dejanews.com wrote:
: I'm sure this is a stupid question but i've read the FAQ and I've rtm and I
: can't figure it out.
: How can I use a s/ / / expression to replace a set of numbers?
: e.g I have a set of strings (filenames) of the form Image51.gif through
: Image78.gif which I wish to change to Fred01.gif to Fred28.gif. I can find
: the bit to change by doing a /Image[5-7]/ but s/Image[5-7]/Fred[0-2]/ doesn't
: change them.
Looks like it changes them to me...
-----------------
#!/usr/bin/perl -w
use strict;
$_ = 'Image51.gif';
s/Image[5-7]/Fred[0-2]/;
print "$_\n";
-----------------
... though likely not the change that you wanted to get.
The "replacement part" of s/// is a _string_, not a regex.
Makes sense, since a regex may match more than one string,
which one should perl choose as the replacement value?
Regexen deal with strings, to deal with numbers you need
non-regex Perl code.
s///e lets you use arbitrary Perl code.
-----------------
#!/usr/bin/perl -w
use strict;
my $num = 1;
foreach ( qw(Image50.gif Image51.gif Image78.gif Image79.gif) ) {
s/(Image(\d\d))/
if ($2 >= 51 && $2 <= 78) # change it
{ sprintf "Fred%02d", $num++ }
else # no change
{ $1 }
/e;
print "$_\n";
}
-----------------
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 20 Mar 1999 01:20:48 -0500
From: "John Cokos" <jcokos@ccs.net>
Subject: Perl Compiler.
Message-Id: <7cveqi$j6a@dfw-ixnews11.ix.netcom.com>
How does the perl compiler deal with "use" and "require" ???
I've used the perl2exe program for Windows, and what it does is compile
anything "use"ed into the executable, but will dynamically require
any "require"d files.
Is the unix version the same?
John
------------------------------
Date: Fri, 19 Mar 1999 17:01:06 -0800
From: "David L. Cassell" <cassell@mail.cor.epa.gov>
Subject: Re: Simple perl question
Message-Id: <36F2F352.D0394999@mail.cor.epa.gov>
MicroChip wrote:
> In perl at least, chop() and other function act as both procedures (no
> return value) or funtction(value returned).
Well, you might want to think of it a little differently than that.
chop() lets you chop anything that is an lvalue.. which in Perl
includes an assignment. Hence:
> In the case of your example.....
>
> chop($number = <STDIN>); # chop applies the function to $number and
> the return value is discarded.
Umm, close, but the return value is *not* discarded.
> is the same as
>
> $number = chop(<STDIN>); # chop applies the function to what you type
> and returns the input minus the newline.
Afraid not. chop() returns the character chopped. $number
is now a string holding a newline.
> Someone correct me if im wrong, I didnt experiment with it.
Okay, since you insist. :-)
BTW, you really should play with these things before you post.
Doing so has saved me several truckloads of embarrassment.
> Kelsang Wangchuk wrote:
> > Dear Perl peeps,
I like the term 'Perlite' myself, although no one else has
expressed any interest in it. Maybe I garden too much. :-)
> > I started learning Perl about ten minutes ago using the Wall, Christiansen
> > and Randal text, and already have a question.
That's understandable. And Randal's last name is 'Schwartz'.
As in 'Schwartzian transform'. If the going seems too tough,
grab a copy of 'Learning Perl' and work through it first.
> > I understand that the line...
> >
> > chop($number = <STDIN>);
> >
> > ...removes the carraige return from the input and places the number in
> > $number. To me, however, this does not seem intuitive: if this were a C
> > program, then the result of this line would certainly be CR-free, but since
> > the chop surrounds the assignment, the $number itself would still have a CR
> > attached to it. What's going on?
Perl lets you work with *everything*. You get the exact results back
from STDIN. C is going to save an inputted string with a null at the end,
which limits what you can do with it. For example, try entering some
strings with nulls as separators. Oops! Perl lets you work with binary
inputs in this way (and in many other ingenious ways too).
And BTW, I am going to assume you have a modern version of Perl and the
turquoise Camel book. If so, think about using chomp() instead of chop().
It's safer, as the docs explain.
If you are on a unix system, learn about the documentation program 'perldoc'.
If you are on a WinTel system, make sure to read the ActivePerl docs in the
handy html format.
HTH,
David
--
David L. Cassell, OAO cassell@mail.cor.epa.gov
Senior computing specialist
mathematical statistician
------------------------------
Date: Fri, 19 Mar 1999 14:53:12 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Simple perl question
Message-Id: <8v9uc7.ck1.ln@magna.metronet.com>
Kelsang Wangchuk (wangchuk@bodhisattva.freeserve.co.uk) wrote:
: Dear Perl peeps,
: I started learning Perl about ten minutes ago using the Wall, Christiansen
: and Randal text, and already have a question.
: I understand that the line...
: chop($number = <STDIN>);
: ....removes the carraige return from the input and places the number in
: $number.
It does those things, but not in that order.
(but it removes the last character regardless of what
character it is)
1) <STDIN> returns a line
2) $number = <STDIN> assigns the returned line to $number
3) chop() removes the last char from its argument
It is a bit of Functional Programming, best read "from the
inside out".
: To me, however, this does not seem intuitive: if this were a C
: program, then the result of this line would certainly be CR-free, but since
: the chop surrounds the assignment, the $number itself would still have a CR
: attached to it. What's going on?
chop() changes its argument (pass by reference), and returns
the character that was removed.
chomp() is preferred for removing line endings with modern perls.
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 19 Mar 1999 14:56:33 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Simple perl question
Message-Id: <h5auc7.ck1.ln@magna.metronet.com>
MicroChip (nospam@hotmail.com) wrote:
: In perl at least, chop() and other function act as both procedures (no
: return value) or funtction(value returned).
I don't think so.
You are free to ignore the returned value though...
: In the case of your example.....
: chop($number = <STDIN>); # chop applies the function to $number and
: the return value is discarded.
: is the same as
: $number = chop(<STDIN>); # chop applies the function to what you type
: and returns the input minus the newline.
: Someone correct me if im wrong, I didnt experiment with it.
You are wrong.
Please do not post guesses.
The usual aim of followups here is to *reduce* confusion.
Posting code that won't compile doesn't do that...
: Kelsang Wangchuk wrote:
: >
: > Dear Perl peeps,
: >
: > I started learning Perl about ten minutes ago using the Wall, Christiansen
: > and Randal text, and already have a question.
: >
: > I understand that the line...
: >
: > chop($number = <STDIN>);
: >
: > ...removes the carraige return from the input and places the number in
: > $number. To me, however, this does not seem intuitive: if this were a C
: > program, then the result of this line would certainly be CR-free, but since
: > the chop surrounds the assignment, the $number itself would still have a CR
: > attached to it. What's going on?
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 17 Mar 1999 06:28:31 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: ternary operator
Message-Id: <MPG.11595be6413ff0e2989772@nntp.hpl.hp.com>
In article <36EF967B.6F1A@geocities.com> on Wed, 17 Mar 1999 11:48:16
+0000, 23_skidoo <23_skidoo@geocities.com >says...
...
> $secondVar = ($firstVar == 0) ? 0 : $array[0];
>
> i just want to check that i understand this correctly, would it not have
> been easier to write the above example like this:
>
> $secondVar = $firstVar ? $array[0] : 0;
>
> is there any reason to do it the long winded way? i realise this is
> probably just a case of tmtowtdi but examples in tutorial books tend to
> use the simplest possible illustration so i was a bit wary.
There is a subtle difference. The first example tests whether the
numerical value of $firstVar be zero or non-zero. The second example
tests whether that the value of $firstVar be TRUE or FALSE. The string
value '00', for example, would give different results.
> also i've
> been unable to find a reference for ternary in the perlfaq. does it go
> by any other names or can someone point me to a reference?
In perlop, it is called 'Conditional Operator'. I have seen it referred
to with phrases involving calling the question mark 'hook', but they are
too ugly to repeat here.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 08:54:44 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: ternary operator
Message-Id: <MPG.11597e2f84b19452989777@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36EFD1FA.DB41ABBD@giss.nasa.gov> on Wed, 17 Mar 1999
11:02:02 -0500, Jay Glascoe <jglascoe@giss.nasa.gov >says...
> [courtesy copy of post sent to cited author]
>
> 23_skidoo wrote:
> >
>
> <snip>
>
> > $secondVar = ($firstVar == 0) ? 0 : $array[0];
>
> [versus]
>
> > $secondVar = $firstVar ? $array[0] : 0;
>
> The top one works in cases where "$firstVar" is
> either null, "", or undefined. So, if you're sure
> "$firstVar" contains a number, then go ahead and
> use the second line.
I think you have 'the top one' and 'the second line' reversed.
> Or, do something like
>
> $secondVar = $firstVar || $array[0]; # probably best style
I *know* you have these reversed! The idiom is:
$secondVar = $firstVar && $array[0]; # nifty but underused
> $secondVar = $firstVar or $secondVar = $array[0]; # a bit silly
and
> $secondVar = $array[0] unless $secondVar = $firstVar; # sillier still
if
> or, silliest of all ;^)
>
> if ($firstVar) { $secondVar = $array[0] } else { $secondVar = $firstVar }
Maybe silliest, but correct! :-)
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Wed, 17 Mar 1999 05:53:33 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Variable splitting
Message-Id: <MPG.115953b9d2c7691d98976f@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36ef893c.10566640@news.NL.net> on Wed, 17 Mar 1999 10:55:11
GMT, Ruud Limbeck <ruud.limbeck@tip.nl >says...
> I need to split up a variable but is don't work .....
> The problemm $test contains : thisisatestandiwant
>
> All I want to have is test (actually 7 characters from
> the beginning and 4 caracters long .and drop it in a
> variable called $new .
perldoc -f substr
It could also be done with a regex or with 'unpack' (and even with
'split' which you imply doesn't work), but 'substr' is the most natural.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Sat, 20 Mar 1999 01:19:06 -0500
From: "John Cokos" <jcokos@ccs.net>
Subject: Web Server Automation
Message-Id: <7cveqh$j6a@dfw-ixnews11.ix.netcom.com>
Is there a perl script out there that will automate virtual domain
creation and email/pop account creation for virtual domain hosting?
Server is running RedHat 5.2, Apache, and Sendmail
I know perl very well, apache pretty well, but almost nothing about sendmail
and pop email.
Thanks in advance.
John
------------------------------
Date: Wed, 17 Mar 1999 06:33:02 -0800
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Which OS am I in
Message-Id: <MPG.11595cf965b63181989773@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <36EFB00F.84057F26@math.missouri.edu> on Wed, 17 Mar 1999
07:37:19 -0600, Stephen Montgomery-Smith <stephen@math.missouri.edu
>says...
> How can a perl program tell whether it is running under DOS
> or UNIX?
>
> I looked for a uname like function for perl, but I didn't find it.
It isn't a function. It is a 'special variable'. Look in perlvar for
$OSNAME or $^O.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personl/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Fri, 19 Mar 1999 22:06:02 -0500
From: "Jerry Zhao" <zzhao@execulink.com>
Subject: XML::Parser - 'Objects' style
Message-Id: <7cv3i2$294$2@goblin.uunet.ca>
I am having a hard time figuring out the 'Objects' style of the XML::Parser.
The manual says that "a hash object is created for each element", but how
does one make use of (refrence) the objects? A simple illustration of the
usage would be greatly appreciated!
------------------------------
Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>
Administrivia:
Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing.
]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body. Majordomo will then send you instructions on how to confirm your
]subscription. This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.
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 5184
**************************************