[22194] in Perl-Users-Digest
Perl-Users Digest, Issue: 4415 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 16 14:10:47 2003
Date: Thu, 16 Jan 2003 11:10:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 16 Jan 2003 Volume: 10 Number: 4415
Today's topics:
regex newbie edwardquick@hotmail.com
Re: regex newbie <bernard.el-hagin@DODGE_THISlido-tech.net>
Re: regex newbie <hazelbigfoot@gmx.net>
Re: regex newbie edwardquick@hotmail.com
Re: regex newbie (Tad McClellan)
Re: regex newbie edwardquick@hotmail.com
Re: regex newbie <nobull@mail.com>
Re: Storable.pm (Ben Morrow)
Re: sysopen problem <neil@alaweb.com>
Re: sysopen problem (Anno Siegel)
Re: sysopen problem <neil@alaweb.com>
Re: sysopen problem (Ben Morrow)
variable interpolation inside a regex (shambolic)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 16 Jan 2003 14:57:45 GMT
From: edwardquick@hotmail.com
Subject: regex newbie
Message-Id: <b06h99$jgk$1@cspc1n11.baplc.com>
This is a multipart message in MIME format.
--=_alternative 0052282180256CB0_=
Content-Type: text/plain; charset="US-ASCII"
Hi,
I just can't get my head round regex expressions! I have a variable
$toRmdr containing the following string:
toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as
4136283ED)
I want to pull out the value 4136283ED into a variable called $queuedAs
but I can't get the regex right. This is my current code but it doesn't
work:
($queuedAs = $toRmdr) =~ s/\([^)]+\)//o;
print "DEBUG: queued as $queuedAs\n";
Can anyone tell me how to fix this please? And also point me to a really
really good regex tutorial?
Many thanks for your help.
Ed.
--=_alternative 0052282180256CB0_=
Content-Type: text/html; charset="US-ASCII"
<br><font size=2 face="sans-serif">Hi,</font>
<br>
<br><font size=2 face="sans-serif">I just can't get my head round regex
expressions! I have a variable $toRmdr containing the following string:</font>
<br>
<br><font size=2 face="sans-serif">toRmdr = (250 2.6.0 Ok, id=41028-09-37,
from MTA: 250 Ok: queued as 4136283ED)</font>
<br>
<br><font size=2 face="sans-serif">I want to pull out the value 4136283ED
into a variable called $queuedAs but I can't get the regex right. This
is my current code but it doesn't work:</font>
<br>
<br><font size=2 face="sans-serif">($queuedAs = $toRmdr) =~ s/\([^)]+\)//o;</font>
<br><font size=2 face="sans-serif">print "DEBUG: queued as $queuedAs\n";</font>
<br>
<br><font size=2 face="sans-serif">Can anyone tell me how to fix this please?
And also point me to a really really good regex tutorial?</font>
<br>
<br><font size=2 face="sans-serif">Many thanks for your help.</font>
<br>
<br><font size=2 face="sans-serif">Ed.</font>
--=_alternative 0052282180256CB0_=--
------------------------------
Date: Thu, 16 Jan 2003 15:05:36 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@DODGE_THISlido-tech.net>
Subject: Re: regex newbie
Message-Id: <b06ho0$p19$3@korweta.task.gda.pl>
[Please don't post MIME to this group]
In article <b06h99$jgk$1@cspc1n11.baplc.com>, edwardquick@hotmail.com wrote:
>
> Hi,
>
> I just can't get my head round regex expressions! I have a variable
> $toRmdr containing the following string:
>
> toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as
> 4136283ED)
>
> I want to pull out the value 4136283ED into a variable called $queuedAs
> but I can't get the regex right. This is my current code but it doesn't
> work:
You're not telling us too much about what you want to match. You say
it's "the value 4136283ED", but that can be matched as easily as:
m/4136283ED/;
Do you mean the last non-whitespace part of the string?
m/\S+$/;
Or do you mean a string of digits ending in ED?
m/\d+ED/;
Or maybe a string of digits followed by any two capital letters?
m/\d+[A-Z][A-Z]/;
Or anything followine "queued as"?
m/queued as .+/;
And so on. You'll have to be more specific.
Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'
------------------------------
Date: Thu, 16 Jan 2003 17:26:22 +0100
From: Jens Hoehne <hazelbigfoot@gmx.net>
Subject: Re: regex newbie
Message-Id: <3E26DD2D.8CBFB56C@gmx.net>
edwardquick@hotmail.com wrote:
>
[...]
> And also point me to a
> really really good regex tutorial?
You could try out your local perldoc information:
perldoc perlretut
and
perldoc perlre
There is also a book - Mastering Regular Expressions - by Jeffrey E. F.
Friedl if you want to go really deep.
Regards,
Jens
------------------------------
Date: 16 Jan 2003 15:37:03 GMT
From: edwardquick@hotmail.com
Subject: Re: regex newbie
Message-Id: <b06jiv$poq$1@cspc1n11.baplc.com>
In article <b06h99$jgk$1@cspc1n11.baplc.com>, edwardquick@hotmail.com
wrote:
>
> Hi,
>
> I just can't get my head round regex expressions! I have a variable
> $toRmdr containing the following string:
>
> toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as
> 4136283ED)
>
> I want to pull out the value 4136283ED into a variable called $queuedAs
> but I can't get the regex right. This is my current code but it doesn't
> work:
You're not telling us too much about what you want to match. You say
it's "the value 4136283ED", but that can be matched as easily as:
m/4136283ED/;
Do you mean the last non-whitespace part of the string?
m/\S+$/;
Or do you mean a string of digits ending in ED?
m/\d+ED/;
Or maybe a string of digits followed by any two capital letters?
m/\d+[A-Z][A-Z]/;
Or anything followine "queued as"?
m/queued as .+/;
And so on. You'll have to be more specific.
Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'
Hi Bernard,
Thanks for your quick and thorough reply! I hadn't thought of all the
possibilities there.
What I want is the number that comes after 'queued as'.
However, I tried:
($queuedAs = $toRmdr) =~ m/queued as .+/;
($queuedAs = $toRmdr) =~ m/\d+ED/;
($queuedAs = $toRmdr) =~ m/\S+$/;
and in each case it just put the whole line into $queuedAs. It didn't chop
any part of it.
Ed.
------------------------------
Date: Thu, 16 Jan 2003 10:04:03 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regex newbie
Message-Id: <slrnb2dlvj.eml.tadmc@magna.augustmail.com>
edwardquick@hotmail.com <edwardquick@hotmail.com> wrote:
> Subject: regex newbie
^^^^^^
More people will see your articles if you leave such things out
of your Subject header.
Have you seen the Posting Guidelines that are posted here weekly?
http://mail.augustmail.com/~tadmc/clpmisc.shtml
> This is a multipart message in MIME format.
Please to not post multipart messages in MIME format.
Have you seen the Posting Guidelines that are posted here weekly?
> I have a variable
> $toRmdr containing the following string:
>
> toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as
> 4136283ED)
You should speak Perl, rather than natural language, whenever possible.
I have:
my $toRmdr = 'toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as 4136283ED)';
Then we would know _exactly_ what you mean.
> I want to pull out the value 4136283ED into a variable called $queuedAs
> but I can't get the regex right. This is my current code but it doesn't
> work:
It does what you told it to do. Tell it to do something else. :-)
> ($queuedAs = $toRmdr) =~ s/\([^)]+\)//o;
That says:
copy the value from $toRmdr into $queuedAs, then
replace: /paren, not paren chars, paren/ with nothing
The regex deletes everything that is between parens, leaving
$toRmdr eq 'toRmdr = '
The s///o option is only useful when there are variables in
your pattern. Your pattern has no variables in it, so the
option is useless.
(and that option is not needed at all in modern perls that
have qr// available.
)
> print "DEBUG: queued as $queuedAs\n";
>
> Can anyone tell me how to fix this please?
my $queuedAs = $1 if $toRmdr =~ /(\w+)\)$/;
or (complain if it does not match):
my $queuedAs;
if ( $toRmdr =~ /(\w+)\)$/ )
{ $queuedAs = $1 }
else
{ die "could not match $toRmdr" }
or (not recommended)
(my $queuedAs = $toRmdr) =~ s/.*?(\w+)\)$/$1/;
> And also point me to a really
> really good regex tutorial?
There are some already installed on your hard disk:
perldoc perlrequick
perldoc perlretut
The ultimate in how to get stuff done with regexes is to pony up
enough cash to buy the "Mastering Regular Expressions" (2e)
O'Reilly book.
> Content-Type: text/html; charset="US-ASCII"
"Do not post binaries, HTML, or MIME"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 16 Jan 2003 16:21:02 GMT
From: edwardquick@hotmail.com
Subject: Re: regex newbie
Message-Id: <b06m5e$poq$2@cspc1n11.baplc.com>
edwardquick@hotmail.com <edwardquick@hotmail.com> wrote:
> Subject: regex newbie
^^^^^^
More people will see your articles if you leave such things out
of your Subject header.
Have you seen the Posting Guidelines that are posted here weekly?
http://mail.augustmail.com/~tadmc/clpmisc.shtml
> This is a multipart message in MIME format.
Please to not post multipart messages in MIME format.
Have you seen the Posting Guidelines that are posted here weekly?
> I have a variable
> $toRmdr containing the following string:
>
> toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250 Ok: queued as
> 4136283ED)
You should speak Perl, rather than natural language, whenever possible.
I have:
my $toRmdr = 'toRmdr = (250 2.6.0 Ok, id=41028-09-37, from MTA: 250
Ok: queued as 4136283ED)';
Then we would know _exactly_ what you mean.
> I want to pull out the value 4136283ED into a variable called $queuedAs
> but I can't get the regex right. This is my current code but it doesn't
> work:
It does what you told it to do. Tell it to do something else. :-)
> ($queuedAs = $toRmdr) =~ s/\([^)]+\)//o;
That says:
copy the value from $toRmdr into $queuedAs, then
replace: /paren, not paren chars, paren/ with nothing
The regex deletes everything that is between parens, leaving
$toRmdr eq 'toRmdr = '
The s///o option is only useful when there are variables in
your pattern. Your pattern has no variables in it, so the
option is useless.
(and that option is not needed at all in modern perls that
have qr// available.
)
> print "DEBUG: queued as $queuedAs\n";
>
> Can anyone tell me how to fix this please?
my $queuedAs = $1 if $toRmdr =~ /(\w+)\)$/;
or (complain if it does not match):
my $queuedAs;
if ( $toRmdr =~ /(\w+)\)$/ )
{ $queuedAs = $1 }
else
{ die "could not match $toRmdr" }
or (not recommended)
(my $queuedAs = $toRmdr) =~ s/.*?(\w+)\)$/$1/;
> And also point me to a really
> really good regex tutorial?
There are some already installed on your hard disk:
perldoc perlrequick
perldoc perlretut
The ultimate in how to get stuff done with regexes is to pony up
enough cash to buy the "Mastering Regular Expressions" (2e)
O'Reilly book.
> Content-Type: text/html; charset="US-ASCII"
"Do not post binaries, HTML, or MIME"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Thanks Tad,
That was a really helpful reply and it's worked well. Sorry about posting
in MIME format (It's Lotus Notes and I can't see any options to switch
this off).
Ed.
------------------------------
Date: 16 Jan 2003 17:45:31 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: regex newbie
Message-Id: <u9d6mxc7k4.fsf@wcl-l.bham.ac.uk>
edwardquick@hotmail.com writes:
> edwardquick@hotmail.com <edwardquick@hotmail.com> wrote:
[ full text of Tad's post, including sig not attributed and not even
indented ]
> Thanks Tad,
>
> That was a really helpful reply and it's worked well. Sorry about posting
> in MIME format (It's Lotus Notes and I can't see any options to switch
> this off).
You really should see the Guidelines _soon_ before you do irreprable
damage to your net credibility.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 16 Jan 2003 17:34:38 +0000 (UTC)
From: mauzo@ux-ma160-16.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: Storable.pm
Message-Id: <b06qfe$gkm$1@wisteria.csv.warwick.ac.uk>
"Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
>Hello World!
>
>I don't understand what $hashref means in the Storable docs.
It means that what Storable returns is not a hash, but a hashref.
> If I want to
>store %vars, and retrieve it later I thought I should do this...
>
>_____________
>use strict;
>use warnings;
>use Storable;
>my %vars;
># Load saved variables, or use default values.
>if (-e "saved.txt") {
You have a race condition here: someone may delete saved.txt between here and
the retrieve. This may not matter to you now, but get into the habit of
avoiding races or you'll get badly bitten someday. See below...
Also, Storable.pm files are binary, not text, so saved.dat would be a better
choice of name under win32.
> print "Saved values found\n\n";
> %vars = retrieve('saved.txt');
retreive returns a hashref (perldoc perlreftut) which is where your error is
coming from. What you want here is:
my $hashref = retrieve('saved.txt');
%vars = %$hashref;
or, shorter but less clear:
%vars = %{ retrieve('saved.txt') };
>} else {
> print "Using default values\n\n";
> %vars = (
> dog_name => ["Please enter your dog's name: ", "Rover"],
> fav_food => ["What is your favourite food? ", "Cheese"],
> testicles => ["How many testicles do you have? ", 3],
> fear => ["What is your biggest fear? ", "Elephants"],
> )
>}
>store \%vars, 'saved.txt';
You see, what you store is \%vars, so what you get back out again isn't going
to be %vars... :)
>print "\nsave complete\n";
> ---------------------------
A rewrite:
#!/usr/bin/perl
use warnings;
use strict;
use Storable;
my (%vars, $ref);
$ref = retrieve 'saved.dat'; # avoid race: just try to retrieve and then see
# if it failed.
if ($ref) {
print "Saved values found.\n";
%vars = %$ref;
}
else {
print "Using defaults.\n";
%vars = ( .... );
store \%vars, 'saved.dat' # we don't need to store what we just loaded...
or die "store failed: $!"; # but we do need to check it worked.
print "Save complete.\n";
}
__END__
You may want to consider working with a hashref rather than a hash throughout:
you will need to use $vars = { ... } instead of %vars = ( ... ) and
$vars->{key} instead of $vars{key}, but then you can pass/accept $vars directly
to/from Storable. You'll want to understand references properly first, though
:).
Ben
------------------------------
Date: Thu, 16 Jan 2003 08:24:29 -0600
From: "Neil Trenholm" <neil@alaweb.com>
Subject: Re: sysopen problem
Message-Id: <v2dg4lae2k6hc8@corp.supernews.com>
----- Original Message -----
From: "Benjamin Goldberg" <goldbb2@earthlink.net>
Newsgroups: comp.lang.perl.misc
Sent: Wednesday, January 15, 2003 10:47 PM
Subject: Re: sysopen problem
> Neil Trenholm wrote:
> [snip]
> > Now for embarrassing part - how would a person fully qualify these
> > constant names, so as to avoid "no strict 'subs" ?
>
> Add parens:
> O_RDWR()|O_CREAT()
>
Thanks Ben !
I had thought that these were constants - but they appear to be subs ....
are they truely ?
Hmmm ... maybe it's time to open up Fnct.pm and look under the hood....
Thanks again for the help Ben !
Neil
------------------------------
Date: 16 Jan 2003 14:36:17 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: sysopen problem
Message-Id: <b06g11$4h4$2@mamenchi.zrz.TU-Berlin.DE>
Neil Trenholm <neil@alaweb.com> wrote in comp.lang.perl.misc:
> ----- Original Message -----
> From: "Benjamin Goldberg" <goldbb2@earthlink.net>
> Newsgroups: comp.lang.perl.misc
> Sent: Wednesday, January 15, 2003 10:47 PM
> Subject: Re: sysopen problem
>
>
> > Neil Trenholm wrote:
> > [snip]
> > > Now for embarrassing part - how would a person fully qualify these
> > > constant names, so as to avoid "no strict 'subs" ?
> >
> > Add parens:
> > O_RDWR()|O_CREAT()
> >
> Thanks Ben !
>
> I had thought that these were constants - but they appear to be subs ....
> are they truely ?
They're both. Perl constants are implemented as subs, prototyped to
take no parameter.
> Hmmm ... maybe it's time to open up Fnct.pm and look under the hood....
Fcntl.pm will most likely just "use constant ...". See "perldoc constant"
and look for the section "TECHNICAL NOTES".
Anno
------------------------------
Date: Thu, 16 Jan 2003 09:53:05 -0600
From: "Neil Trenholm" <neil@alaweb.com>
Subject: Re: sysopen problem
Message-Id: <v2dlapofm8cp2a@corp.supernews.com>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:b06g11$4h4$2@mamenchi.zrz.TU-Berlin.DE...
> Neil Trenholm <neil@alaweb.com> wrote in comp.lang.perl.misc:
> > > Add parens:
> > > O_RDWR()|O_CREAT()
> > >
> > Thanks Ben !
> >
> > I had thought that these were constants - but they appear to be subs
....
> > are they truely ?
>
> They're both. Perl constants are implemented as subs, prototyped to
> take no parameter.
>
> > Hmmm ... maybe it's time to open up Fnct.pm and look under the hood....
>
> Fcntl.pm will most likely just "use constant ...". See "perldoc constant"
> and look for the section "TECHNICAL NOTES".
>
> Anno
Thanks Anno - I will definitely check it out !
I've read many of your insightful posts and find them most helpful ! They
also seem less picky or elitist than _some_ of the regular, knowledgable
posters - whose usual and regular answer is RTFM or 'perdoc XXX'.
No matter how much we RTFM's (1000's of pages - when taking it all into
account) there will still be some scrap of knowledge that we don't know ...
or don't understand. It's nice _not_ to get flamed for a misquote, poor
phrase, dropped semi-colon or other trivial matter. I've been programming
for 20 years, perl for about 7 and still get butterflies posting to cplm -
worried that I may drop a character or not state the problem exactly enough
or overstate the problem etc etc........
Thanks,
Neil
------------------------------
Date: Thu, 16 Jan 2003 17:51:23 +0000 (UTC)
From: mauzo@ux-ma160-16.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: sysopen problem
Message-Id: <b06rer$h2o$1@wisteria.csv.warwick.ac.uk>
"Neil Trenholm" <neil@alaweb.com> wrote:
>I've read many of your insightful posts and find them most helpful ! They
>also seem less picky or elitist than _some_ of the regular, knowledgable
>posters - whose usual and regular answer is RTFM or 'perdoc XXX'.
Now that is more than a little unfair. I would certainly not put myself into
the above category, but those who are very rarely just tell someone to RTFM
without telling them which of the (as you say) very many FMs to read. You
really should not bet upset by a 'perldoc *' reply: noone is being rude, or
implying you're stupid. or anything (well, not necessarily); just assuming
that you want the correct answer to your question. The manuals go over the
answers rather better than someones quickly-composed reply will (as I found not
long ago, when I thought I'd give a 'helpful' answer and missed something
important: a perldoc -f my would have been much better... :).
> No matter how much we RTFM's (1000's of pages - when taking it all into
>account) there will still be some scrap of knowledge that we don't know ...
>or don't understand.
Noone expects you to know all the docs by heart. They expect you to have tried,
and they expect you to read the docs when you're pointed in the right
direction. If the docs are unclear (particularly the tutorial pages), I'm quite
sure people would be happy to explain them to you, and indeed then fix the docs
so they're more clear.
> It's nice _not_ to get flamed for a misquote, poor
>phrase, dropped semi-colon or other trivial matter.
Misquotes are just rude.
I don't think I've seen any spelling/grammar flames on this group (not that
I've been here long).
Dropped semi-colon's are never trivial, and apart from anything else indicate
that you didn't try to run the actual code you posted, which is very important.
Noone can debug your code if it's got typos in.
> I've been programming
>for 20 years, perl for about 7 and still get butterflies posting to cplm -
>worried that I may drop a character or not state the problem exactly enough
>or overstate the problem etc etc........
Copy/paste is your friend when posting code. As for the other things, well, if
some people are a bit short with you just don't take it personally. It
probably wasn't meant that way :)
Just my 2d.
Ben
------------------------------
Date: 16 Jan 2003 11:01:49 -0800
From: johnston.jay@mtvne.com (shambolic)
Subject: variable interpolation inside a regex
Message-Id: <f333df78.0301161101.400e88d2@posting.google.com>
Hi all,
is there a way of turning off variable interpolation within the
left-hand (i.e. search) portion of s///?
i.e.
#!usr/bin/perl -w
use strict;
my $text = <STDIN>; # reading in text with special chars like $ or @ -
say h@tt
$text =~ s/tt/$other_text/;
i'm having a problem where perl thinks it's an array inside $text,
wheras i want to treat it as a normal character.
grateful for any advice or assistance.
jay
------------------------------
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 4415
***************************************