[22970] in Perl-Users-Digest
Perl-Users Digest, Issue: 5190 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 7 14:10:34 2003
Date: Mon, 7 Jul 2003 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)
Perl-Users Digest Mon, 7 Jul 2003 Volume: 10 Number: 5190
Today's topics:
regex and parsing (Justin Rodino)
Re: regex and parsing <abigail@abigail.nl>
Re: regex and parsing <g4rry_short@zw4llet.com>
Re: Regex help... (Tad McClellan)
Re: Regex help... <peb@bms.umist.ac.uk>
Re: Regex help... (jgamble@noreply.screenpages.com)
Re: Regexp constructed from command line arguments. <nobull@mail.com>
Re: script for unrestricted permutation (Chris Charley)
Re: STDIO problem (Jan)
Re: Using semicolon delimited autosplit "perl -aF\;" (C Marshall)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 7 Jul 2003 07:24:23 -0700
From: justin_rodino@yahoo.co.uk (Justin Rodino)
Subject: regex and parsing
Message-Id: <8f89781b.0307070624.1affdbe1@posting.google.com>
trying to utilise as few modules as possible, does anyone know how to
parse a line to get/set variable information? For example, here is my
line:
User="1233" Location="somewhere" Phone="1111"
and what i want to do is make variables $user=1233,
$location=somewhere, $phone=1111. There might be more than 3, there
might be less than 3 items...
Any help greatly appreciated. Thanks!
------------------------------
Date: 07 Jul 2003 15:06:41 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: regex and parsing
Message-Id: <slrnbgj341.tcb.abigail@alexandra.abigail.nl>
Justin Rodino (justin_rodino@yahoo.co.uk) wrote on MMMDXCVII September
MCMXCIII in <URL:news:8f89781b.0307070624.1affdbe1@posting.google.com>:
<> trying to utilise as few modules as possible, does anyone know how to
<> parse a line to get/set variable information? For example, here is my
<> line:
<>
<> User="1233" Location="somewhere" Phone="1111"
<>
<> and what i want to do is make variables $user=1233,
<> $location=somewhere, $phone=1111. There might be more than 3, there
<> might be less than 3 items...
This problem is poorly defined, so after giving a solution, your second
line probably won't parse. But that happens if you define your problem
by giving a single example.
I'd solve it this way:
my %data;
$_ = 'User="1233" Location="somewhere" Phone="1111"';
$data {lc $1} = $2 while /\G(\w+)="([^"]*)"\s*/g;
Abigail
--
use lib sub {($\) = split /\./ => pop; print $"};
eval "use Just" || eval "use another" || eval "use Perl" || eval "use Hacker";
------------------------------
Date: Mon, 07 Jul 2003 16:05:51 +0000
From: Garry Short <g4rry_short@zw4llet.com>
Subject: Re: regex and parsing
Message-Id: <bec2di$im$1$8300dec7@news.demon.co.uk>
Justin Rodino wrote:
> trying to utilise as few modules as possible, does anyone know how to
> parse a line to get/set variable information? For example, here is my
> line:
>
> User="1233" Location="somewhere" Phone="1111"
>
> and what i want to do is make variables $user=1233,
> $location=somewhere, $phone=1111. There might be more than 3, there
> might be less than 3 items...
>
> Any help greatly appreciated. Thanks!
Purely to print the info out, this'll work:
1 my $a = "User=\"1233\" Location=\"somewhere\" Phone=\"1111\"";
2
3 my @fields = split / /, $a;
4 foreach $f (@fields) {
5 my ($var, $val) = split /=/, $f;
6 $val =~ s/"//g;
7 print "Var = $var, Val = $val\n";
8 }
Running it gives:
me@home:~> perl test.pl
Var = User, Val = 1233
Var = Location, Val = somewhere
Var = Phone, Val = 1111
Personally, I'd leave the quotes out. If you want to be able to use spaces
in the values, use a different record delimiter (newlines would do nicely).
HTH,
Garry
------------------------------
Date: Mon, 7 Jul 2003 08:20:35 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Regex help...
Message-Id: <slrnbgist3.1o2.tadmc@magna.augustmail.com>
Math55 <magelord@t-online.de> wrote:
> hi, why does "log" find lines like that:
>
> 20030707062514.ksyms
Because it can.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 07 Jul 2003 15:28:01 +0100
From: Paul Boardman <peb@bms.umist.ac.uk>
Subject: Re: Regex help...
Message-Id: <3F098371.1090702@bms.umist.ac.uk>
Math55 wrote:
> hi, why does "log" find lines like that:
>
>
> 20030707062514.ksyms
>
> anyone any idea????
>
> THANK YOU:)
>
as you might have gathered from the other, unhelpful, comments you've
received, we need more to go on before we can help to workout what the
problem is.
Why not post some code? Stripped down to the barest bits that show the
problem?
HTH
Paul
------------------------------
Date: 7 Jul 2003 09:22:05 -0700
From: jgamble@screenpages.com (jgamble@noreply.screenpages.com)
Subject: Re: Regex help...
Message-Id: <ffac87bd.0307070822.469687a8@posting.google.com>
magelord@t-online.de (Math55) wrote in message news:<a2b8188a.0307070212.61b95436@posting.google.com>...
> hi, why does "log" find lines like that:
>
>
> 20030707062514.ksyms
>
> anyone any idea????
>
> THANK YOU:)
According to the Camel book log will return the logarithm (base e) of
a passed in expression. If the expression is omitted it will attempt
a logarithm of $_
Does that help?
John
------------------------------
Date: 07 Jul 2003 18:01:48 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Regexp constructed from command line arguments.
Message-Id: <u9d6gm46f7.fsf@wcl-l.bham.ac.uk>
tadmc@augustmail.com (Tad McClellan) writes:
> The solution to your problem can be found in the Perl FAQ:
>
> How can I expand variables in text strings?
See also numerous previous threads discussing this FAQ (do a google
groups search on the exact phrase) since the answer given in the FAQ
is not really very good.
A patch to replace it with, IMNSHO, a much better answer can be found:
http://www.wcl.bham.ac.uk/pub/bam/patches/perl/perlfaq4-scalar-interpolate-take-3.diff
The question/answer reads:
How can I expand/interpolate variables in text strings?
You can process a string through Perl's interpolation
engine like this:
$text = 'this has a $foo in it...\n ...and a $bar';
# Assume $text does not contain "\nEND\n"
chop ( $text = eval "<<END\n$text\nEND\n" );
die if $@;
This will not work, and for good reason, if $text is com-
ing form an tainted source. For explanation of how $text
could execute arbitrary Perl code see ``How do I expand
function calls in a string?'' in this section of the FAQ.
If $text is coming from a source external to the Perl
script (typically a file) and you would be content to
trust executable code from that source then you simply
make data from that source untainted. This is no more or
less dangerous than using "do()". For an explaination of
tainting see the perlsec manpage.
If you do not trust the source that much then you can
limit and launder the parts of the string that are passed
to eval() something like this:
$text =~ s/(\$\w+)/$1/eeg; # needed /ee, not /e
This still gives unrestricted access to your scalar vari-
ables. It is often better to use a hash:
%user_defs = (
foo => 23,
bar => 19,
);
$text =~ s/\$(\w+)/$user_defs{$1}/g;
For other variations on the theme of text templates see
the sprintf() function and numerous modules on CPAN.
However the OP's question was actually about s///. Whilst the FAQ
that Tad mentions is somewhat applicable I think the OP's exact
question appears often enough that is should qualify as a FAQ in it's
own right. Like most FAQs this one seems to come in bursts - it goes
unasked for months then is asked be several people in the course of a
couple of weeks (as it has been in the last couple of weeks).
A good previous thread in this FAQ please see the thread following message
<a3m4ff$5ra$1@news.surfnet.nl>.
http://groups.google.com/groups?selm=a3m4ff%245ra%241%40news.surfnet.nl
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 7 Jul 2003 08:49:34 -0700
From: charley@pulsenet.com (Chris Charley)
Subject: Re: script for unrestricted permutation
Message-Id: <4f7ed6d.0307070749.7f0bb197@posting.google.com>
"John W. Krahn" <krahnj@acm.org> wrote in message news:<3F07BD9F.BAA5B427@acm.org>...
> "John W. Krahn" wrote:
> >
> > This looks a bit simpler. :-)
> >
> > my $group = 4;
> > my @elements = qw/ red green blue /;
> >
> > for ( 0 .. ( @elements ** $group ) - 1 ) {
> > my @odometer;
> > do { unshift @odometer, $_ % @elements } while $_ = int( $_ / @elements );
> > @odometer = ( (0) x $group, @odometer )[ map -$_, reverse 1 .. $group ];
> > print "@{[ map $elements[$_], @odometer ]}\n";
> > }
>
>
> Or a bit simpler. :-)
>
> my $group = 4;
> my @elements = qw/ red green blue /;
>
> for ( 0 .. ( @elements ** $group ) - 1 ) {
> my @odometer;
> do { unshift @odometer, $_ % @elements } while $_ = int( $_ / @elements );
> print "@{[ map $elements[$_], (0) x ($group - @odometer), @odometer ]}\n";
> }
>
>
>
> John
Well, I didn't mean to offend many of the fine Perl programmers.
Should have kept in mind all the folks that would be viewing my post -
guess I carried the simplified explanation a bit too far? :-)
John, I will look over your code to understand its brevity.
------------------------------
Date: 7 Jul 2003 10:20:22 -0700
From: jan_buys@hotmail.com (Jan)
Subject: Re: STDIO problem
Message-Id: <11971c2c.0307070920.46ce3e56@posting.google.com>
"Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote in message news:<Xns93AE933BDAB7Asdn.comcast@206.127.4.25>...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
> jan_buys@hotmail.com (Jan) wrote in
> news:11971c2c.0307040546.700d307b@posting.google.com:
>
> > Hi,
> >
> > I think my perl knowledge must have shrunk to the size of a neutron...
> > *sigh*.
> > I'm trying to do some STD I/O on a cmd console from within a script
> > called by another script. I narrowed down the problem to the smallest
> > full scripts I could. test.pl is the main script, calling test2.pl in
> > which the problems with the IO exist.
> >
> > Code of the 2 scripts :
> >
> > #!c:\perl\bin\perl.exe # Activeperl 5.6.1 - win32 build 633
> > # script : test.pl
> >
> > print `cqperl -w test2.pl`;
> >
> > #!c:\program files\Rational\clearquest\cqperl.exe
> > # Cqperl, derived from Activeperl 5.6.0, win32
> > # script : test2.pl
> >
> > $| = 1 ;
> > print "Hello there\n" ;
> > chomp($answer = <STDIN>);
> > print "$answer to you too...\n";
> >
> >
> > The problem is the STDOUT msgs only are flushed to screen once the
> > input is entered...
>
> Because the second script is executed within the first script's
> backticks, its output does NOT go to STDOUT. Well, it does, but its
> STDOUT is tied to a buffer in the first script, not to the console.
>
> Setting $|=1 makes perl flush the buffer after each print, but that just
> means that the output gets to the first script's buffer sooner. Nothing
> gets printed until the first script's print statement, which happens
> after the second script has finished executing and the user has already
> entered an answer.
>
> You might want to send the user prompts to STDERR, or find some other way
> of communicating between scripts.
Thanks for the info. For the moment I will send the outputs to
STDERR, but I don't really like that (call it a religious thing or
something...) and try another way later on... the show must go on.
Once again, thx for explanation
Jan
>
> - --
> Eric
> $_ = reverse sort qw p ekca lre Js reh ts
> p, $/.r, map $_.$", qw e p h tona e; print
>
> -----BEGIN xxx SIGNATURE-----
> Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
>
> iQA/AwUBPwXHRmPeouIeTNHoEQIGNACg7NmQdkTy2lvZHCbLbvoR9EepglIAn3Y5
> n/YnAYER/pSvSN9h8Hy6KWfH
> =Z7G8
> -----END PGP SIGNATURE-----
------------------------------
Date: 7 Jul 2003 09:03:41 -0700
From: c_j_marshall@hotmail.com (C Marshall)
Subject: Re: Using semicolon delimited autosplit "perl -aF\;"
Message-Id: <cb9c7b76.0307070803.5055cc40@posting.google.com>
c_j_marshall@hotmail.com (C Marshall) wrote in message news:<cb9c7b76.0307070226.3bdbe9d4@posting.google.com>...
> I haven't had any luck managing to autosplit on the semi colon
> character under AIX when putting my commandline options into a script.
>
I've found that I can get away with a simple -F\; as long as this is
not the last flag I use - and that the flag after this will then be
ignored.
Therefore
#!/usr/bin/perl -awlnF\; --
seems to work perfectly - but I can't say I understand why.
------------------------------
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 5190
***************************************