[18614] in Perl-Users-Digest
Perl-Users Digest, Issue: 782 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Apr 27 14:10:40 2001
Date: Fri, 27 Apr 2001 11:10:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <988395010-v10-i782@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 27 Apr 2001 Volume: 10 Number: 782
Today's topics:
Re: Question on variable substitution (Anno Siegel)
Re: Question on variable substitution nobull@mail.com
regex for quoted lists <cybertoast@mindless.com>
Re: Regexp Question <abe@ztreet.demon.nl>
Re: Sockets, Time-outs, and Alarms nobull@mail.com
Solved: What the HECK is tr/// doing? <jcook@strobedata.com>
Re: Tcl is faster then perl. <coopvs14@americasm01.nt.com>
What the HECK is tr/// doing? <jcook@strobedata.com>
Re: What the HECK is tr/// doing? (Anno Siegel)
Re: What the HECK is tr/// doing? <uri@sysarch.com>
Re: What the HECK is tr/// doing? <jcook@strobedata.com>
Re: What the HECK is tr/// doing? nobull@mail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 27 Apr 2001 15:41:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Question on variable substitution
Message-Id: <9cc3vr$87l$1@mamenchi.zrz.TU-Berlin.DE>
According to Dieter Faulbaum <faulbaum@alder.bessy.de>:
>
> this is an excerpt from a perl file, which doesn't do what I want.
> Can anyone tell me how to do it in the right way?
Do what? You should say what you are expecting. Since your program
does *not* do it, there is no way for us to know.
> sub update(@_);
You are declaring a prototype for update, but I don't think that
is what you want to do. By a double coincidence, this even compiles.
The first coincidence is that the character "_" is accepted in
a prototype even though it doesn't have a meaning.
> my $From = "(^#\s*)(rstatd)";
> my To = "$2";
This doesn't compile. You are not showing us the code you are
actually using. Cut-and-paste, don't retype.
> update($From, $To);
Here is the other coincidence. It just so happens that the prototype
(@) allows this form of call.
I'm leaving you at this point. Invest a little more effort in
asking your question.
Anno
------------------------------
Date: 27 Apr 2001 17:32:56 +0100
From: nobull@mail.com
Subject: Re: Question on variable substitution
Message-Id: <u9pudygv5z.fsf@wcl-l.bham.ac.uk>
Dieter Faulbaum <faulbaum@alder.bessy.de> writes:
> Subject: Question on variable substitution
It seems to me that this question is often asked and answered.
However with a quick Google search on "variable substitution" I was
unable to find it in the last couple of weeks. Also looking back over
the last 8 weeks of my posts I was unable to find an instance of me
answering this question. I don't like Google as much as the old Deja.
> this is an excerpt from a perl file,
Do not post excerpts. Produce a small but complete script to
illustrate your question. Otherwise we cannot be sure if we are
dealing with your original problem or an artifact of your
transcription errors.
> which doesn't do what I want.
>
> sub update(@_);
Invalid character in prototype. Anyone know why it doesn't give a
syntax error? Redundant anyhow as (@) is the default prototype.
> my $From = "(^#\s*)(rstatd)";
You should get into the habit of using qr// to quote regular
expressions. Apart from the fact that it is more efficient it also
avoids mistakes like the forgetting to double the backslash above.
(Remeber \s is special in regular expressions, not in double-quoted
strings).
> my To = "$2";
I assume the missing $ in $To is a transcription errror.
Double quotes create an interpolative context. The above (less the
typo) therefore copies the _current_ value of $2 into $To.
You want to put the expression itself not the result of evaluating the
expression into the variable. Please see recent (<1 week ago) thread
that was selfishly entitled "expressions".
> while (<>) {
> ($_ = $_) =~ s/$From/$To/;
> }
Would it be safe to assume that in your real program that you actually
so something with the altered value in $_ ? Or are you really reading
a line from the file into $_, altering the value in $_ and then
discarding it?
BTW: The bit where you say ( $_ = $_ ) =~ is pure obfuscation - remove
it.
> Can anyone tell me how to do it in the right way?
You do not give us nearly enough information about what you are trying
to do for us to determine if your basic approach is sound and your
implementation is bad or if your basic approach is wrong.
Here are some alternatives:
my $From = '(^#\s*)(rstatd)';
my $To = '$2';
while (<>) {
s/$From/$To/ee;
print;
}
my $From = qr/(^#\s*)(rstatd)/;
my $To = sub { $2 };
while (<>) {
s/$From/&$To/e;
print;
}
my $Subst = sub { s/(^#\s*)(rstatd)/$2/ };
while (<>) {
&$Subst;
print;
}
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 27 Apr 2001 12:18:36 -0500
From: sundar <cybertoast@mindless.com>
Subject: regex for quoted lists
Message-Id: <3AE9A9EC.EF6CA127@mindless.com>
I'm a little stumped on a seemingly simple regex.
I've got a string, for instance:
$string = "and one, 'very, font, face, verdana', much, 'something, else'";
I need to break the string out into a list, based on the delimiter - in this
case comma. The problem is that the string may contain "quoted lists", as in
the stuff between the 's above.
I need the split to return the comma separated data, but ignoring the stuff
between quotes, i.e.,
@list = ("and one", "very, font, face, verdana", "much", "something, else");
I thought that a non-greedy match would work:
@list = split ( /,|'([^\']+)'|/, $string); # split on comma or lazy quoted
data
but this returns the comma and the spaces as elements in the array! I tried
adding a word boundary match:
@list = split ( /,\w,|'([^\']+)'|/, $string);
but this keeps the commas!
I know that it's probably something that's very simple that I'm missing, but 2
hours of messing with this has got me nowhere.
Thanks for any help.
------------------------------
Date: Fri, 27 Apr 2001 17:43:40 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: Regexp Question
Message-Id: <4j4jetom18g6bhnl1t12nqtjt8af2am9sb@4ax.com>
On Thu, 26 Apr 2001 16:50:10 -0700, Peter White
<hpya78@postoffice.pacbell.net> wrote:
> Here is my script
>
> File:
> ------------------------
> Ouput should be:
> aa 45 23 22
> bb 2 12 67
> dd 99 98 100
> mm 56 87 1
>
#!/usr/bin/perl -w
use strict;
use Quantum::Superpositions;
while ( <DATA> ) {
my( $word, @nums ) = split;
next if any( @nums ) < 0 or any( @nums ) > 100;
print;
}
__DATA__
aa 45 23 22
bb 2 12 67
dd 99 98 100
ff 101 99 100
mm 56 87 1
nn 10000 999 1
--
Good luck, Abe
Amsterdam Perl Mongers http://amsterdam.pm.org
perl -wle '$_=q@Just\@another\@Perl\@hacker@;print qq@\@{[split/\@/]}@'
------------------------------
Date: 27 Apr 2001 17:17:02 +0100
From: nobull@mail.com
Subject: Re: Sockets, Time-outs, and Alarms
Message-Id: <u9r8yegvwh.fsf@wcl-l.bham.ac.uk>
eins@durchnull.de (Rudolf Polzer) writes:
> nobull@mail.com <nobull@mail.com> wrote:
> > eins@durchnull.de (Rudolf Polzer) writes:
> >
> > > nobull@mail.com <nobull@mail.com> wrote:
> > > > Anyhow I can't recall for sure but I think ignored signals don't
> > > > interrupt system calls.
> > >
> > > $SIG{USR1} = "IGNORE";
> >
> > This tells the OS not to pass SIGUSR1 into userspace but to ignore
> > it.
>
> So the OS and not the program supports ignoring. Is there any
> relevant difference about this (perhaps about file handles etc.)
I refer you back to the top of this post where I said "I can't recall
for sure but I think ignored signals don't interrupt system calls".
Having thought more about it I am now more sure that ignored signals
do _not_ cause system calls (e.g. sysread() in Perl) to return with an
EINTR condition ( i.e. $! == Errno::EINTR ).
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 27 Apr 2001 09:09:38 -0700
From: Jim Cook <jcook@strobedata.com>
Subject: Solved: What the HECK is tr/// doing?
Message-Id: <3AE999C2.A740F5A6@strobedata.com>
Oh, God. Now, I DID read the manual before posting this, it just didn't
click that I was giving a range operator in the bad case. When I typed
"+-<>" I was thinking "plus, minus, less, greater" and never put it
together.
So, I escaped the "-" and things work.
I really do feel like an idiot -- I'm supposed to be better than that.
Sorry for the disturbance. I just hope somebody else can empathize with
how I got so wrapped around the axle and give me a little slack.
Jim Cook wrote:
>
> Wow, I can't imagine what I'm doing wrong here. Can anybody explain why
> I'm getting the answer shown? Following is a screen capture with all the
> interesting information, I hope. Why does my scaler get translated into
> "+,-." instead of "+-<>"?
>
> D:\>type test.pl
> #! perl -w
> use strict;
>
> my $bad;
> my $good;
>
> $bad = "daft";
> $good = "daft";
>
> $bad =~ tr/daft/+-<>/;
> $good =~ tr/daft/-+<>/;
>
> print "\$bad = '$bad'\n";
> print "\$good = '$good'\n";
>
> D:\>perl -v
>
> This is perl, v5.6.0 built for MSWin32-x86
>
> Copyright 1987-2000, Larry Wall
>
> Perl may be copied only under the terms of either the Artistic License
> or the
> GNU General Public License, which may be found in the Perl 5.0 source
> kit.
>
> Complete documentation for Perl, including FAQ lists, should be found on
> this system using `man perl' or `perldoc perl'. If you have access to
> the
> Internet, point your browser at http://www.perl.com/, the Perl Home
> Page.
>
> D:\>perl test.pl
> $bad = '+,-.'
> $good = '-+<>'
>
> --
> jcook@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
> 2001 Wed: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
> Strobe Data Inc. home page http://www.strobedata.com
> My home page O- http://jcook.net
--
jcook@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
2001 Wed: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
Strobe Data Inc. home page http://www.strobedata.com
My home page O- http://jcook.net
------------------------------
Date: Fri, 27 Apr 2001 11:51:55 -0400
From: "Wenzel, Joel [CAR:VS11:EXCH]" <coopvs14@americasm01.nt.com>
Subject: Re: Tcl is faster then perl.
Message-Id: <3AE9959B.6050D981@americasm01.nt.com>
Tim Hammerquist wrote:
> Wenzel, Joel [CAR:VS11:EXCH] <coopvs14@americasm01.nt.com> wrote:
> > Everyone was ignoring my question when I said perl was faster then Tcl
> > so I switched it around.
>
> * plonk *
>
> --
> -Tim Hammerquist <timmy@cpan.org>
> Your question doesn't make any sense. You might as well ask whether
> it is possible to grow vegetables from a painting, without becoming
> Wednesday first.
> -- Abigail, c.l.p.misc
hehe
------------------------------
Date: Fri, 27 Apr 2001 08:51:10 -0700
From: Jim Cook <jcook@strobedata.com>
Subject: What the HECK is tr/// doing?
Message-Id: <3AE9956E.ADF418A6@strobedata.com>
Wow, I can't imagine what I'm doing wrong here. Can anybody explain why
I'm getting the answer shown? Following is a screen capture with all the
interesting information, I hope. Why does my scaler get translated into
"+,-." instead of "+-<>"?
D:\>type test.pl
#! perl -w
use strict;
my $bad;
my $good;
$bad = "daft";
$good = "daft";
$bad =~ tr/daft/+-<>/;
$good =~ tr/daft/-+<>/;
print "\$bad = '$bad'\n";
print "\$good = '$good'\n";
D:\>perl -v
This is perl, v5.6.0 built for MSWin32-x86
Copyright 1987-2000, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5.0 source
kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to
the
Internet, point your browser at http://www.perl.com/, the Perl Home
Page.
D:\>perl test.pl
$bad = '+,-.'
$good = '-+<>'
--
jcook@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
2001 Wed: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
Strobe Data Inc. home page http://www.strobedata.com
My home page O- http://jcook.net
------------------------------
Date: 27 Apr 2001 16:27:00 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: What the HECK is tr/// doing?
Message-Id: <9cc6kk$a7n$1@mamenchi.zrz.TU-Berlin.DE>
According to Jim Cook <jcook@strobedata.com>:
> Wow, I can't imagine what I'm doing wrong here. Can anybody explain why
> I'm getting the answer shown? Following is a screen capture with all the
> interesting information, I hope. Why does my scaler get translated into
> "+,-." instead of "+-<>"?
[snip]
Read the tr/// documentation again. "-" has a special meaning unless
it appears as the first character of a search- or replacement string.
(Probably also last, you'll find that.) It defines a *range* of
characters. The result it what you observed.
Anno
------------------------------
Date: Fri, 27 Apr 2001 16:46:06 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: What the HECK is tr/// doing?
Message-Id: <x7itjqgujo.fsf@home.sysarch.com>
>>>>> "JC" == Jim Cook <jcook@strobedata.com> writes:
JC> Wow, I can't imagine what I'm doing wrong here. Can anybody explain why
JC> I'm getting the answer shown? Following is a screen capture with all the
JC> interesting information, I hope. Why does my scaler get translated into
JC> "+,-." instead of "+-<>"?
JC> $bad = "daft";
JC> $good = "daft";
JC> $bad =~ tr/daft/+-<>/;
^^^
that is a range of chars and not the three chars you think they
are. read perlop to see what that means.
JC> $good =~ tr/daft/-+<>/;
that works because - is at the beginning of the char list (it also works
at the end).
you can escape the - with \ to make it do what you want.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info: http://www.sysarch.com/perl/OOP_class.html
------------------------------
Date: Fri, 27 Apr 2001 10:05:53 -0700
From: Jim Cook <jcook@strobedata.com>
Subject: Re: What the HECK is tr/// doing?
Message-Id: <3AE9A6F1.8E6D581@strobedata.com>
> JC> $bad =~ tr/daft/+-<>/;
> ^^^
>
> that is a range of chars and not the three chars you think they
> are. read perlop to see what that means.
Thank you for the reply. As it turns out, that very fact did dawn on me
a few minutes after I got to the point of posting the question. I just
wasn't thinking about range operators and had looked at perlop and the
manual already.
You'll see my reply in the newsgroup also.
Thank you for the time. I sure with my epiphany had come BEFORE I posted
the message.
--
jcook@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
2001 Wed: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
Strobe Data Inc. home page http://www.strobedata.com
My home page O- http://jcook.net
------------------------------
Date: 27 Apr 2001 18:31:28 +0100
From: nobull@mail.com
Subject: Re: What the HECK is tr/// doing?
Message-Id: <u9itjqgsgf.fsf@wcl-l.bham.ac.uk>
Jim Cook <jcook@strobedata.com> writes:
> Wow, I can't imagine what I'm doing wrong here. Can anybody explain why
> I'm getting the answer shown?
The character - is special in tr/// for details "perldoc perlop".
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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.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.
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 V10 Issue 782
**************************************