[12109] in Perl-Users-Digest
Perl-Users Digest, Issue: 5709 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 18 04:07:09 1999
Date: Tue, 18 May 99 01:00:21 -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 Tue, 18 May 1999 Volume: 8 Number: 5709
Today's topics:
complex permissions problem with www/cgi script (Michael Friendly)
Re: Error message when reading a file (Larry Rosler)
Re: FAQ 4.40: How can I tell whether a list or array co <ken@forum.swarthmore.edu>
Re: FAQ 4.41: How do I compute the difference of two ar <ken@forum.swarthmore.edu>
Re: FAQ 4.41: How do I compute the difference of two ar (Larry Rosler)
File with TABS <john.wilkinson@soton.sc.philips.com>
Re: help needed (Tad McClellan)
Re: how to catch the return value in perl? <greenej@my-dejanews.com>
How to create passwords in .htpasswd <hkasten@abm-soft.de>
Re: How to create passwords in .htpasswd <pvorishatesspam@earthlink.net>
Re: how to perform other program input in Perl ? <pvorishatesspam@earthlink.net>
Re: OOP and Perl-a good book? <pvorishatesspam@earthlink.net>
Re: Perl "constructors" <rra@stanford.edu>
Perl and cookie (Cplee5570)
Re: question about STDOUT <zzhan@cs.nmsu.edu>
Re: Randon hex #'s, directory contents, and password qu (Tad McClellan)
Re: Reg Exp Q! <bill@fccj.org>
Re: regular expression ? <ken@forum.swarthmore.edu>
Script logging? <portboy@home.com>
Re: Sorting my list... (Larry Rosler)
Re: Sorting problem (Arjun Ray)
Re: Your assistance humbly requested (file processing p (Larry Rosler)
Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 17 May 1999 15:31:28 GMT
From: friendly@hotspur.psych.yorku.ca (Michael Friendly)
Subject: complex permissions problem with www/cgi script
Message-Id: <7hpcog$460$1@sunburst.ccs.yorku.ca>
I recently had a disk crash and had to re-create my OS, web server, etc.
All files were recovered intact. The server runs as user 'www', group
'www' and some perl cgi scripts which used to run, now fail when they call
a secondary, pstogif script, to convert a postscript file to gif.
I've spent many hours trying to figure this out, and am near to hair-tearing
time. One script which fails is
http://hotspur.psych.yorku.ca/Online/mosaics
It has something to do with permissions,
but I can't for the life of me figure out what.
It calls /usr/local/bin/pstogif (from the Latex2html package) to convert
a .ps file to .gif. That in turn uses Ghostscript and the pbm utilities,
with the line:
system("$PNMCROP $in |$REDUCE_COLOR |$PPMTOGIF - > $out");
which fails ('cannot create') under the conditions below. This runs in
a www tmp directory which is owned by user www.
- When I run this as ~friendly manually in /tmp it works
- If I su - www, and run it in the www tmp, it works properly.
- When I run this as ~friendly manually in the www tmp directory, it fails:
htdocs/tmp [189] % pstogif -DEBUG 1 mos2137g.ps
Writing mos2137g.ppm
Renaming mos2137g.ppm to mos2137g.1ppm
Writing mos2137g.2ppm
Writing mos2137g.3ppm
sh: mos2137g1.gif: cannot create
Writing mos2137g1.gif
sh: mos2137g2.gif: cannot create
Writing mos2137g2.gif
sh: mos2137g3.gif: cannot create
Writing mos2137g3.gif
.
I have verified that the files created there by the webserver and the cgi
program are owned by www, in group www, and that the cgi script is actually
being run as user wwww.
Hope someone might have some suggestions about what might be going wrong,
or how I can narrow down the problem further. Please respond directly.
-Michael
--
Michael Friendly Email: friendly@yorku.ca (NeXTmail OK)
Psychology Dept
York University Voice: 416 736-5115 Fax: 416 736-5814
4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
Toronto, ONT M3J 1P3 CANADA
------------------------------
Date: Mon, 17 May 1999 22:25:59 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Error message when reading a file
Message-Id: <MPG.11aa99b625bfac16989a8d@nntp.hpl.hp.com>
In article <sjoph7.9p8.ln@magna.metronet.com> on Mon, 17 May 1999
14:53:48 -0400, Tad McClellan <tadmc@metronet.com> says...
...
> If the original poster wants only files, ignoring all of the other
> types of filesystem objects, then this will work faster, more
> portably, and with very large lists of files:
>
> foreach my $filename ( grep -f, readdir(DIR)) {
True in principle. But just to avoid the next "Why doesn't this work?",
foreach my $filename ( grep -f "$path/$_", readdir(DIR)) {
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 18 May 1999 00:09:54 -0500
From: Ken Williams <ken@forum.swarthmore.edu>
Subject: Re: FAQ 4.40: How can I tell whether a list or array contains a certain element?
Message-Id: <180519990009542339%ken@forum.swarthmore.edu>
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]
In article <MPG.11a8bbc73060bce989a7c@nntp.hpl.hp.com>, Larry Rosler
<lr@hpl.hp.com> wrote:
>[Posted and a courtesy copy sent.]
>
>In article <373f009a@cs.colorado.edu> on 16 May 1999 11:30:02 -0700, Tom
>Christiansen <perlfaq-suggestions@perl.com> says...
>...
>> @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
>> undef %is_blue;
>> for (@blues) { $is_blue{$_} = 1 }
>...
>>
>> @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
>> undef @is_tiny_prime;
>> for (@primes) { $is_tiny_prime[$_] = 1; }
>
>How boring. :-) Why not write this one as:
>
> @istiny_prime[@primes] = (1) x @primes;
>
>Give 'slices' a chance!
Or the following classic (for hashes), which has the advantage that you
can put a "my" in front of it:
%is_blue = map {$_,1} @blues;
I bet part of the idea in the FAQ was to use the most straightforward
syntax for beginners, though. Maybe one of each kind?
------------------------------
Date: Tue, 18 May 1999 01:10:17 -0500
From: Ken Williams <ken@forum.swarthmore.edu>
Subject: Re: FAQ 4.41: How do I compute the difference of two arrays? How do I compute the intersection of two arrays?
Message-Id: <180519990110171020%ken@forum.swarthmore.edu>
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]
In article <373f23c6@cs.colorado.edu>, Tom Christiansen
<perlfaq-suggestions@perl.com> wrote:
> How do I compute the difference of two arrays? How do I compute the
>intersection of two arrays?
>
> Use a hash. Here's code to do both and more. It assumes that each
> element is unique in a given array:
>
> @union = @intersection = @difference = ();
> %count = ();
> foreach $element (@array1, @array2) { $count{$element}++ }
> foreach $element (keys %count) {
> push @union, $element;
> push @{ $count{$element} > 1 ? \@intersection : \@difference }, $element;
> }
This code is correct for union, but for intersection & difference it's
not. It will fail if either @array1 or @array2 has duplicate elements,
because it can't see the difference between two of an element in one
array, or the same element once in each array.
Also, the difference of two sets A & B is commonly defined as "all the
elements in A that aren't in B". What you have seems to be a symmetric
difference, i.e. all the elements in A or B, but not both.
The following code snippets will return output as sets, i.e. with
duplicates removed - but the input arrays may contain duplicates.
{
my %count = map {$_,1} @array1, @array2;
@union = keys %count;
}
{
my %count = map {$_,1} @array1;
@intersection = grep {delete $count{$_}} @array2;
}
{
my %count = map {$_,1} @array1;
@array2_minus_array1 = grep{!delete $count{$_}} @array2;
}
{
my %count1 = map {$_,1} @array1;
my %count2 = map {$_,1} @array2;
my %count3;
foreach $element (keys %count1, keys %count2) { $count3{$element}++ }
@symmetric_difference = grep {$count3{$_} == 1} keys %count3;
}
I bet there's a slicker way to do that last one.
Of course, as you often say, if you're going to do lots of set
operations with lists you probably want to store them as hashes in the
first place.
------------------------------
Date: Tue, 18 May 1999 00:25:59 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: FAQ 4.41: How do I compute the difference of two arrays? How do I compute the intersection of two arrays?
Message-Id: <MPG.11aab5dbc8d86093989a8e@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <180519990110171020%ken@forum.swarthmore.edu> on Tue, 18 May
1999 01:10:17 -0500, Ken Williams <ken@forum.swarthmore.edu> says...
...
> {
> my %count1 = map {$_,1} @array1;
> my %count2 = map {$_,1} @array2;
> my %count3;
> foreach $element (keys %count1, keys %count2) { $count3{$element}++ }
> @symmetric_difference = grep {$count3{$_} == 1} keys %count3;
> }
>
> I bet there's a slicker way to do that last one.
I think you win the bet. :-) Replacing your hash initialization maps
with the much faster hash slices:
{
my (%count1, %count2);
@count1{@array1} = ();
@count2{@array2} = ();
@symmetric_difference =
grep !exists $count1{$_} ^ !exists $count2{$_},
keys %count1, keys %count2;
}
The negations are to quiet the pedants who will argue that TRUE returned
by exists() in numeric context isn't necessarily 1.
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: Tue, 18 May 1999 07:38:58 GMT
From: John Wilkinson <john.wilkinson@soton.sc.philips.com>
Subject: File with TABS
Message-Id: <37411912.6DAA766B@soton.sc.philips.com>
Hi all,
First, thanks for all the help with my first question.
Now I have a little problem I cannot solve on my own, I need help.
I have a text file that contains, amongst other things, filenames that
need to be processed by a perl script. I read in these in my program,
and all is well, unless someone has put a TAB or several spaces after
the end of the filename, so when I come to add the filename extension,
the filename becomes corrupted, with a number of TABS between the
filename and its extension part, i.e.
should be
/bin/foo.tl
with TABS is
/bin/foo .tl
I need to be able to read in the file and strip off the TABS before I
add the extension. I have tried using chomp, to no avail.
Any ideas please.
Regards,
John.
------------------------------
Date: Mon, 17 May 1999 20:48:00 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: help needed
Message-Id: <0cdqh7.g19.ln@magna.metronet.com>
Ronald J Kimball (rjk@linguist.dartmouth.edu) wrote:
: <tex2121@my-dejanews.com> wrote:
: Hey, you followed up that you were able to figure it on your own, so I
: just wanted to comment on this one bit:
And then I wanted to comment on the comment.
: $word =~ s/[\n.,"?><_-=+`~!\@#\$%^&*();:'\/\\]//g;
^
^
: (Note that you need to backslash the @ and the $.)
And the hyphen (or move it to first or last in the char class)
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Tue, 18 May 1999 07:29:36 GMT
From: JAG <greenej@my-dejanews.com>
Subject: Re: how to catch the return value in perl?
Message-Id: <7hr4t2$qjr$1@nnrp1.deja.com>
Look at your "Content-type:" header and make sure that it conforms to
the spec. I think that you are missing a space, and that could be
causing the browser some problems.
--
# James Greene - Informatics Consulting - D-79539 Loerrach, Germany
# Internet: www.gucc.org/greene/consult - greene@gucc.org
# PGP Fingerprint: CA88 9BE2 92B3 3162 DF6B 7080 2F9E A97E F25C 5972
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
------------------------------
Date: Tue, 18 May 1999 08:06:51 +0200
From: "Holger Kasten" <hkasten@abm-soft.de>
Subject: How to create passwords in .htpasswd
Message-Id: <7hr024$b86$1@black.news.nacamar.net>
How can I create passwords in a .htpasswd
file with a perl script???
------------------------------
Date: Mon, 17 May 1999 23:24:20 -0700
From: Phil Voris <pvorishatesspam@earthlink.net>
Subject: Re: How to create passwords in .htpasswd
Message-Id: <37410794.83641CE6@earthlink.net>
Consider using the expect module
------------------------------
Date: Mon, 17 May 1999 23:31:41 -0700
From: Phil Voris <pvorishatesspam@earthlink.net>
Subject: Re: how to perform other program input in Perl ?
Message-Id: <3741094D.EB0B6F85@earthlink.net>
consider the Expect module
------------------------------
Date: Mon, 17 May 1999 23:42:27 -0700
From: Phil Voris <pvorishatesspam@earthlink.net>
Subject: Re: OOP and Perl-a good book?
Message-Id: <37410BD3.5E00F81@earthlink.net>
I've been vainly attempting to learn the practical usage of OO. Being
as I'm most familiar with Perl, I began a project using OO Perl and Perl
Tk. I rapidly realized that understanding OO syntax was not sufficient:
I still had problems implimenting the GUI as a separate class from the
heart of the application. I apparently didn't 'get it.'
Do you intend this book to function as an OO instructional in addition
to a primer in OO Perl. That is, will it address design issues like the
one which I describe?
Thank you (and good luck with the book!),
Phil
------------------------------
Date: 17 May 1999 22:15:54 -0700
From: Russ Allbery <rra@stanford.edu>
Subject: Re: Perl "constructors"
Message-Id: <yliu9r3qlx.fsf@windlord.stanford.edu>
armchair <armchair@my-dejanews.com> writes:
> But since Perl does not have what C++ calls "function overloads" where
> the same name can be used for different functions, who each differ by
> input parameter types
Er... of course it does. That's why Perl puts all of the arguments in @_
and lets you do whatever you want with them, including completely changing
the behavior of the sub if there are three arguments instead of two, or if
the arguments are of different types.
I write Perl code like that all the time.
> I think there is some benefit to be looking at code and instantly see
> that a method is a constructor, or not.
Why? Seriously.
> I am using size of the project to try and amplify a value or benefit.
I don't think that's a very good technique. The things that work well for
small projects don't necessarily work better for big projects, and there
are things one does for big projects that would be a disaster in small
projects. I think small projects are completely different beasts in a
more fundamental way than that.
--
#!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
$^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: 18 May 1999 05:13:41 GMT
From: cplee5570@aol.com (Cplee5570)
Subject: Perl and cookie
Message-Id: <19990518011341.17697.00001986@ng-ba1.aol.com>
Hello Friend
How to use perl script to write cookie and read cookie
please respond thank you
Andrea
------------------------------
Date: Mon, 17 May 1999 23:55:25 -0600
From: "Zhan" <zzhan@cs.nmsu.edu>
Subject: Re: question about STDOUT
Message-Id: <7hqv9v$e8p$1@bubba.NMSU.Edu>
Thanks for your kind reply.
I have forward your message to my friend. I hope this will help him a lot.
Best regards,
zhan
Russ Allbery <rra@stanford.edu> wrote in message
news:ylbtfl4xx3.fsf@windlord.stanford.edu...
> Zhan <zzhan@cs.nmsu.edu> writes:
>
> > Yes! one trouble that I have thought for a long time, but I can't answer
> > it, I have wrote a chatroom CGI with Perl 5.0005 under Linux, but now
> > when I try run it under NT Server with ActivePerl 5.0005 for NT&95, a
> > function (system calls) "STDOUT->flush;" or "autoflush STDOUT 1;" do not
> > work, but it run under Linux very well,
>
> Does $| = 1 work any better?
>
> > And my chat room have another problem, I use IE to visit it is well, but
> > it's Netscape killer now, if I use netscape for win version to visit it,
> > netscape will be crashed, I don't know why?Can u have time to help me?
>
> This sounds like a Netscape bug. Chances are you're generating some sort
> of invalid HTML that it doesn't like.
>
> --
> #!/usr/bin/perl -- Russ Allbery, Just Another Perl Hacker
> $^=q;@!>~|{>krw>yn{u<$$<[~||<Juukn{=,<S~|}<Jwx}qn{<Yn{u<Qjltn{ > 0gFzD gD,
> 00Fz, 0,,( 0hF 0g)F/=, 0> "L$/GEIFewe{,$/ 0C$~> "@=,m,|,(e 0.), 01,pnn,y{
> rw} >;,$0=q,$,,($_=$^)=~y,$/ C-~><@=\n\r,-~$:-u/ #y,d,s,(\$.),$1,gee,print
------------------------------
Date: Mon, 17 May 1999 20:13:02 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Randon hex #'s, directory contents, and password questions...
Message-Id: <eabqh7.iv8.ln@magna.metronet.com>
[ there is no comp.lang.perl newsgroup. It was deleted a few
*years* ago. If it is listed at your news server, then you
suffer under a poorly maintained system.
headers set to only newsgroups that actually exist.
]
Mitch (portboy@home.com) wrote:
: Here's some questions I have:
There have been *tens of thousands* of folks who have learned
Perl before you.
It is rather presumptuous to think that _your_ question has
never occured to any of them...
You should check the FAQ for a newsgroup before posting
to the newsgroup.
: 1.) I need to prompt the user for a password that is stored in some
: configuration file as: password 123456; The prompting and all is fine,
: however, how can I make it so that it doesn't echo any characters?
Perl FAQ, part 8:
"How do I ask the user for a password?"
: Also, how can I parse the config file to see if the inputted password is
: the same as the one they typed in?
Use a regular expression, or split(), or unpack().
If the password is encrypted in the file, then see
Perl FAQ, part 8:
"How do I decode encrypted password files?"
: 2.) How can I generate 32bit random hex #'s? Should I just create a
: 32bit random integer, and then do a printf "%lx", $random;? Is there a
: way to tell rand I want hex #'s?
You seem to think that "hex" is a kind of number. It is not.
It is merely a representation of a number, as is binary and decimal.
Here are several different representations where each refers
to the exact same number:
10 decimal
1010 binary
A hex
So if you have a "32bit random integer", you already have a
hex (and decimal, and binary, and...) number!
Do you want to convert the _representations_ from one number
system to another?
Maybe pack()/unpack() will help, as pointed out in
Perl FAQ, part 4:
"How do I convert bits into ints?"
: 3.) If I have a directory /usr/local/foo:bar/ How can I print the
: contents of all files contained within that directory? All files within
: that directory will be simple ASCII text.
perldoc -f opendir
perldoc -f grep
perldoc -f open
: 4.) I have some entries in a configuration file that read something
: like:
: mapfoo foo foo2 bar; where foo and bar should refer to a directory
: /usr/local/foo:bar/ How can I extract the foo and bar,
That depends on what criteria is used to determine where the
two "good" parts are.
You have not specified that criteria.
The single example you have given leaves a lot to interpretation.
2nd and 4th space-separated fields?
The word following "mapfoo" and the word following "foo2"?
The words on either side of "foo2"?
From the 8-10th characters and the 17-19th characters?
: and then print
: the contents of all files under /usr/local/foo:bar/
I was taught to never do this in programming school, but...
GOTO 3.)
: 5:) Lastly, how can I print the everyline in the configuration file
: that starts with a 32bit integer? So, if the config was like:
: 1234 foo foo2 bar;
: 5678 foo2 foo3 bar2;
: hehe haha;
: whatever fooboy;
: it would print the 1234 and 5678 lines only.
Uhh. Those lines *all* start with a 32 bit integer.
If there are 32 bits in the file, then it starts with a
32 bit integer ...
... or 4 ASCII characters...
... or 2 16-bit integers.
You are very confused about numbers and data, you will not
be able to understand (or find for yourself) _how_ to do
it until you understand _what_ is different.
What matters in what _interpretation_ you wish to impose
on your data.
The config file is text, yes?
Then it should be interpreted as ASCII characters.
You need not be concerned with what bit patterns are used for
what characters if you read the file the <usual> way.
So, your question _should_ have been:
"how can I print the everyline in the configuration file
that starts with four digit characters?"
while (<>) {
print if /^\d{4}/;
}
--
Tad McClellan SGML Consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 17 May 1999 23:27:34 -0400
From: "Bill Jones" <bill@fccj.org>
Subject: Re: Reg Exp Q!
Message-Id: <3740ddf1.0@usenet.fccj.cc.fl.us>
In article <3740579d.0@usenet.fccj.cc.fl.us>, "Bill Jones" <bill@fccj.org>
wrote:
> I started to give up and ask what this matches -
>
> /(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i;
>
> but I am not; I will post what I think the answer is shortly...
>
> Embarrassing, huh?,
> -Sneex- :]
OK, time for my answer (or guess, as the case may be) -
I thought about this a for a little while, and
came up with the following:
$string = "1234567890";
# Match...
print $string =~
/(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i;
print "\n\n";
$string = "1 22 333 4444 55555 666666 7777777 88888888 999999999 0";
# No Match, except on the seven 7's...
print $string =~
/(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i;
print "\n\n";
$string = "1 (904) 632-3089";
# Match...
print $string =~
/(?:(?:(1)[.-]?)?\(?(\d{3})\)?[.-]?)?(\d{3})[.-]?(\d{4})(x\d+)?/i;
print "\n\n";
print "What was matched: $`, $&, $', $+,
$1, $2, $3, $4, $5, $6, $7, $8, $9\n";
print "\nMy Guess: It's an RE to match a phone number...\n\n";
Which prints out:
1234567890
7777777
6323089
What was matched: 1 (904) , 632-3089, , 3089, , , 632, 3089, , , , ,
My Guess: It's an RE to match a phone number...
Thoughts?
-Sneex- :]
_________________________________________________________________________
Bill Jones | Data Security Specialist | http://www.fccj.org/cgi/mail?dss
FCCJ | 501 W State St | Jacksonville, FL 32202 | 1 (904) 632-3089
Trust the computer industry to shorten "Year 2000"
to "Y2k". It was this kind of thinking that caused
the problem in the first place.
-- Tony Poldrugovac
------------------------------
Date: Tue, 18 May 1999 00:33:19 -0500
From: Ken Williams <ken@forum.swarthmore.edu>
Subject: Re: regular expression ?
Message-Id: <180519990033197119%ken@forum.swarthmore.edu>
[[ This message was both posted and mailed: see
the "To," "Cc," and "Newsgroups" headers for details. ]]
In article <GcF%2.733$fx5.627@news.rdc1.sfba.home.com>, Stephen Warren
<swarren@www.wwwdotorg.org> wrote:
><tvn007@my-dejanews.com> wrote in message
>news:7hn4pt$35l$1@nnrp1.deja.com...
>> Here is a problem and my solution:
>>
>> $line[3] =~ s/0/L/g;
>> $line[3] =~ s/1/H/g;
>> $line[3] =~ s/x/X/g;
>>
>> Question:
>>
>> How can I combine the three lines above into one perl statement ?
>
>Well, you should execute:
>
> perldoc perlop
>
>and search for 'tr/'
Yes, that's the right solution for this case. Nice and fast, too. But
if your search or replace strings might have more than one letter, use
something like:
my %replacer = (0 => 'L', 1 => 'H', x => 'X');
$line[3] =~ s/([01x])/$replacer{$1}/g;
Make it less redundant & more general (but harder to read) by doing:
my %replacer = (0 => 'L', 1 => 'H', x => 'X');
my $opts = join '|', map{quotemeta} keys %replacer;
$line[3] =~ s/($opts)/$replacer{$1}/g;
------------------------------
Date: Tue, 18 May 1999 05:24:26 GMT
From: Mitch <portboy@home.com>
Subject: Script logging?
Message-Id: <3740FA00.8647A891@home.com>
In my script I'd like to have the option of setting the setting the a
"log" level of my script. What I mean is that if the user type
"log_level 1" then I'd set the logging to the file to "normal" mode.
"log_level 2" sets it to debugging. etc.
My question is, what is a good and proper way to do this? I'd like the
"log file" to show all commands executed, there return values,
parameters passed to subroutines, values returned, errors caught, etc.
I've searched the perlfaq's and man pages, and have been unable to turn
up anything.
Thanks,
Mitch
------------------------------
Date: Tue, 18 May 1999 00:55:11 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Sorting my list...
Message-Id: <MPG.11aabcb2365c2b94989a8f@nntp.hpl.hp.com>
In article <7hqnaa$h79$1@nnrp1.deja.com> on Tue, 18 May 1999 03:37:46
GMT, tbsmith@deltacom.net <tbsmith@deltacom.net> says...
...
> open I, "file_with_list" or die "bleh: $!\n";
> @i = <I>;
> close I;
> for (@i) { chomp; s/.+\|//; $h{$_}++; }
> print map {
> "$_ has been here $h{$_} time".
> ($h{$_} > 1 ? 's' : '') . ".\n"}
> reverse sort {$h{$a} <=> $h{$b}} grep {$h{$_} =~ /./} keys %h;
There's a lot more going on in that last line than is needed. Here,
with a few other tweaks for compactness and efficiency (such as not
reading all the data -- which may be huge -- into an array):
open I, "file_with_list" or die "bleh: $!\n";
my %h;
/\|(.+)/ and $h{$1}++ while <I>;
print map "$_ has been here $h{$_} time". ($h{$_} > 1 && 's') . ".\n",
sort {$h{$b} <=> $h{$a}} keys %h;
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
Date: 18 May 1999 00:29:06 -0500
From: aray@nmds.com (Arjun Ray)
Subject: Re: Sorting problem
Message-Id: <37d5f89d.2693474261@news1.newscene.com>
In <m1vhdrm4jj.fsf@halfdome.holdit.com>,
merlyn@stonehenge.com (Randal L. Schwartz) wrote:
| Naaah... I'd go with more of:
|
| print
| map "<tr>$_</tr>",
| map { join "", map { "<td>".(/^$/ ? "-" : $_)."</td>" } @$_ }
| sort { $b->[2] <=> $a->[2] }
| grep { $_->[1] =~ m/$FORM{'TNSET'}/i }
| map { chomp; [ split /;/ ] }
| @techlines;
Hey, pretty cool. (Reminds me of APL!)
I suppose the only thing that might be dodgy is the 'join ""' part.
Wouldn't a strategic 'print @array' take care of this automatically?
(Also, looking back at Thomas' problem, there's also the cases from
split dropping trailing delimiters, whereas the sorry state of current
browsers generally behooves us to get the number of <td>s right.) So,
how about this
print
map { ('<tr>', @$_, '</tr>', "\n") }
map { [ map { '<td>'.(/^$/ ? "-" : $_).'</td>' } @$_ ] }
sort { $b->[2] <=> $a->[2] }
grep { $_->[1] =~ m/$FORM{'TNSET'}/i }
map { chomp; [ split /;/, $_, 8 ] }
@techlines;
| But no, that's not the canonical Schwartzian Transform either, not
| as coined by Mr. Christiansen.
Well, it starts with a map, has a sort somewhere, and ends with a map.
Good enough for me...
:ar
------------------------------
Date: Mon, 17 May 1999 22:06:55 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Your assistance humbly requested (file processing problem)
Message-Id: <MPG.11aa954b659c9705989a8c@nntp.hpl.hp.com>
[Posted and a courtesy copy sent.]
In article <3740B9C6.4401@bitwell.net> on Tue, 18 May 1999 00:48:45 GMT,
Josh Pointer <josh@bitwell.net> says...
> My problem is as follows. I need to read in an existing file, throw away
> certain predefined lines and write the remaining lines to a new file
> which replaces the existing file. Being overly blessed with hubris, I
> thought I had it, but I apparently don't. The following code produces a
> blank file.
...
I separated your data-handling code from the file manipulation, and
rewrote it in Perl instead of C, and added some protection against
premature end-of-file. And it works just fine.
Your file manipulations look OK too, so I'm not sure what is wrong.
#!/usr/local/bin/perl -w
use strict;
my @initialize = ( -2, 2, -1, 3, -3, 3 );
for my $length (@initialize) {
if ($length < 0) {
<DATA> || last while $length++;
} else {
print <DATA> || last while $length--;
}
}
__END__
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
prints:
3
4
6
7
8
12
13
14
--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com
------------------------------
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 5709
**************************************