[30666] in Perl-Users-Digest
Perl-Users Digest, Issue: 1911 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 9 16:09:44 2008
Date: Thu, 9 Oct 2008 13:09: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 Thu, 9 Oct 2008 Volume: 11 Number: 1911
Today's topics:
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <kramer@ufl.edu>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <jurgenex@hotmail.com>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <jwkenne@attglobal.net>
Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ? <joost@zeekat.nl>
Re: Date Problem <whynot@pozharski.name>
Re: efficient way to write multiple loops code <hirenshah.05@gmail.com>
Re: How to make all available CPU resource to Perl/MySQ xhoster@gmail.com
Inplace editing the elegant way <jrpfinch@gmail.com>
Re: Inplace editing the elegant way <jurgenex@hotmail.com>
Re: Parsing CSV and " " sln@netherlands.com
Re: Parsing CSV and " " sln@netherlands.com
RegEx: certain number of occurrences <mislam@nospam.ciuc.edu>
Re: RegEx: certain number of occurrences (Steven M. O'Neill)
Re: RegEx: certain number of occurrences <jurgenex@hotmail.com>
Re: RegEx: certain number of occurrences <someone@example.com>
Re: RegEx: certain number of occurrences (Steven M. O'Neill)
Re: RegEx: certain number of occurrences sln@netherlands.com
Re: RegEx: certain number of occurrences sln@netherlands.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 09 Oct 2008 11:29:15 -0400
From: kramer <kramer@ufl.edu>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <gcl801$j00k$1@usenet.osg.ufl.edu>
Thanks Ben. This is the kind of answer I was looking for. After 40
years of programming on other languages and now picking up perl I am
constantly surprised at how often perl somehow correctly "interprets" my
"english" intents and gives me what I want. But not so in this case so
I asked.
Even though it looks like it would break many other things are
"junctions" one of the per6 items I can somehow use as a feature in
5.010 now?
Your example triggered smart matches so I'll have to check to see if
that can be bent to get what I want. All just to save a bunch of
repetitive typing!
Jack
Ben Morrow wrote:
> Quoth Jim Gibson <jimsgibson@gmail.com>:
>> In article <gcjig4$fg3a$1@usenet.osg.ufl.edu>, kramer <kramer@ufl.edu>
>> wrote:
>>
>>> I expected ($x == ($y or $z)) to return true if $x equals either $y or
>>> $z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
>>>
>>> Why?
>> Because Perl is not English. I can't think of any computer language
>> that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.
>
> use Quantum::Superpositions;
>
> my ($x, $y, $z) = (1, 2, 1);
>
> if ($x == any($y, $z)) {
> print "YAY!";
> }
>
> This will be built in to Perl 6, where (IIRC) you will be able to write
>
> ($x == $y|$z)
>
> The concept is called 'junctions'.
>
> Ben
>
------------------------------
Date: Thu, 09 Oct 2008 11:37:33 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <kljse4hnrm4h970tir6j33ig3vpemo6fb1@4ax.com>
Willem <willem@stack.nl> wrote:
>Jim Gibson wrote:
>) Because Perl is not English. I can't think of any computer language
>) that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.
>
>Perl 6 perhaps ? :-)
I sure hope not because it would be mathematically wrong. As I mentioned
in a previous answer '==' and 'or' are _NOT_ distributive.
Maybe using a different notation, I have no problem with that. But for
heavens sake not for '==' and 'or'. Their semantics have been fixed a
long, long time ago.
jue
------------------------------
Date: Thu, 09 Oct 2008 15:52:30 -0400
From: John W Kennedy <jwkenne@attglobal.net>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <48ee60fd$0$4965$607ed4bc@cv.net>
Jim Gibson wrote:
> In article <gcjig4$fg3a$1@usenet.osg.ufl.edu>, kramer <kramer@ufl.edu>
> wrote:
>
>> I expected ($x == ($y or $z)) to return true if $x equals either $y or
>> $z. iow to give the same as (($x==$y) or ($x==$z)). But it doesn't.
>>
>> Why?
>
> Because Perl is not English. I can't think of any computer language
> that will parse '($x == ($y or $z))' as '(($x==$y) or ($x==$z));'.
COBOL
--
John W. Kennedy
A proud member of the reality-based community.
------------------------------
Date: Thu, 09 Oct 2008 21:59:12 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: ($x == ($y or $z)) same as (($x==$y) or ($x==$z)) ???
Message-Id: <87od1tleun.fsf@zeekat.nl>
kramer <kramer@ufl.edu> writes:
> Even though it looks like it would break many other things are
> "junctions" one of the per6 items I can somehow use as a feature in
> 5.010 now?
You can use effectively the same operations by installing
Quantum::Superpositions from CPAN, and it shouldn't break anything. See
http://search.cpan.org/~lembark/Quantum-Superpositions-2.02/lib/Quantum/Superpositions.pm
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
------------------------------
Date: Thu, 09 Oct 2008 13:16:43 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Date Problem
Message-Id: <b921s5x3bf.ln2@carpet.zombinet>
Graham Stow <graham.stow@gmail.com> wrote:
> My logic is that, when the value of <$todays_date> changes, it will because
> it will have become tomorrow's date, and will not equal <$date> anymore. So
> then assign the value of <$todays_date> to <$date> and do something.
> "Eric Pozharski" <whynot@pozharski.name> wrote in message
> news:unnvr5x9km.ln2@carpet.zombinet...
>> Graham Stow <graham.stow@gmail.com> wrote:
>> *SKIP*
>>> if ($date ne $todays_date) {
>>> $date= $todays_date;
>>> do something.....
>>> }
>>
>> That "does something" because I<$date> is not equal to I<$todays_date>.
>>
>> *CUT*
>>
>> Surely you're interested why condition succedes?
[ Don't top-post -- people don't like to feel stupid ]
Intentions and code are two different beasts.
Do what you was said to do (twice already) -- use fscking cron.
--
Torvalds' goal for Linux is very simple: World Domination
------------------------------
Date: Thu, 9 Oct 2008 11:53:19 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: efficient way to write multiple loops code
Message-Id: <da3eee6f-711f-48ff-824a-4639d94a2566@v39g2000pro.googlegroups.com>
On Oct 8, 7:27=A0pm, Tad J McClellan <ta...@seesig.invalid> wrote:
> Ilya Zakharevich <nospam-ab...@ilyaz.org> wrote:
> > [A complimentary Cc of this posting was sent to
> > Tad J McClellan
> ><ta...@seesig.invalid>], who wrote in article <slrngepo67.von.ta...@tadm=
c30.sbcglobal.net>:
>
> >> > =A0 =A0 =A0 =A0 =A0@file1 =3D split('\|',$file1_line);
>
> >> A pattern match should *look like* a pattern match:
> >> =A0 =A0@file1 =3D split(/\|/,$file1_line);
>
> > In general, I do not agree. =A0A split on a constant string WITHOUT
> > METACHARS should better be written as a split on string. =A0
>
> I like that idea enough that I may actually change my preference...
>
> > However, in
> > this particular case, it is better to use something looking as a REx.
>
> > However, do you really find /\|/ very esthetic? =A0;-) =A0
>
> No. In this case, the nature of the beast precludes anything esthetic. :-=
(
>
> > Can it be
> > written better than m'\|'?
>
> That is not too objectionable, though I kinda like /\Q|/
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Thanks to all.
I tried to read file in array and use @file1_lines =3D <INFO_P>;
But I my results are getting changed.
Below is my FULL Code which I am using.
Please suggest something to make it run more fast.
#!/usr/local/bin/perl
$p_file =3D "out_p.txt";
$s_file =3D "out_s.txt";
$r_file =3D "out_r.txt";
open(INFO_P,$p_file);
open(INFO_S,$s_file);
open(INFO_R,$r_file);
@p_lines =3D <INFO_P>;
@s_lines =3D <INFO_S>;
@r_lines =3D <INFO_R>;
$fail_flag =3D 1;
$p_slow =3D 0;
$p_fail =3D 0;
$r_robin =3D 0;
$s_as_p =3D 0;
foreach $s_line (@s_lines)
{
@sec =3D split('\|',$s_line);
$s_cli_ip =3D $sec[4];
$s_ser_ip =3D $sec[5];
$s_id =3D $sec[7];
$r_robin_flag =3D 1;
$flag =3D 0;
foreach $p_line (@p_lines)
{
@pri =3D split('\|',$p_line);
$p_cli_ip =3D $pri[4];
$p_ser_ip =3D $pri[5];
$p_id =3D $pri[7];
if($s_cli_ip eq $p_cli_ip)
{
$flag =3D 1;
if($s_id eq $p_id)
{
$r_robin_flag =3D 0;
$s_res_first =3D 0;
$p_res_first =3D 0;
foreach $r_line (@r_lines)
{
@res =3D split('\|',$r_line);
$r_cli_ip =3D $res[4];
$r_ser_ip =3D $res[5];
$r_id =3D $res[7];
if(($s_cli_ip eq $r_cli_ip) && ($s_id eq $r_id))
{
if($r_ser_ip eq $s_ser_ip)
{
#chk if pri_res_first
if($p_res_first eq '0'){
$slow =3D 1;
$s_res_first =3D 1;
}
}elsif($r_ser_ip eq $p_ser_ip){
$fail_flag =3D 0;
if($s_res_first){
#$slow =3D 1;
}else{
#$s_res_first =3D 0;
$p_res_first =3D 1;
}
}
}
if($p_res_first){
$primary++ ;
last;
}
}
if($fail_flag){
$primary_fail++;
}elsif($slow){
$slow =3D 0;
$fail_flag =3D 1;
$primary_slow++;
}
last;
}
}
}
if($flag =3D=3D 0){
$s_as_p++;
}elsif($r_robin_flag){
$r_robin++;
}
}
close(INFO_P);
close(INFO_S);
close(INFO_R);
------------------------------
Date: 09 Oct 2008 15:19:58 GMT
From: xhoster@gmail.com
Subject: Re: How to make all available CPU resource to Perl/MySQL in Win XP?
Message-Id: <20081009111959.961$Y2@newsreader.com>
"evillen@gmail.com" <evillen@gmail.com> wrote:
> >
> > Interesting - I wonder if there is a LOT of overhead
> > in the communication BETWEEN your two processes?
> >
>
> Hi Bugbear - do you have any suggestions about how I could determine/
> measure the overhead required between Perl & MySQL?
$dbh->ping() foreach 1..1e5;
I can do about 5000 pings per second.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
------------------------------
Date: Thu, 9 Oct 2008 07:16:54 -0700 (PDT)
From: jrpfinch <jrpfinch@gmail.com>
Subject: Inplace editing the elegant way
Message-Id: <d9f9b06e-b6f8-4cee-a4b8-066104ee290f@g17g2000prg.googlegroups.com>
I would like to develop a script that can inplace edit a large file
(potentially 1Gb), omitting some lines according to a regex and
replacing some lines according to another regex. Here is an example
of a script that does this with a couple of hardcoded patterns:
{
local ($^I, @ARGV) = ('', ($full_path_to_config_file));
while (<>)
{
s/this/that/;
s/foo/bar/;
print unless ((/delete me/) or (/Delete Me Too/));
}
}
What is the best way to scale this for n patterns? When I say best, I
mean a good balance of speed and readability. Can I put all the
omission patterns in an array and all the search/replace patterns in
another array and somehow wedge them into the example above?
Any help appreciated.
Jon
------------------------------
Date: Thu, 09 Oct 2008 08:16:00 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Inplace editing the elegant way
Message-Id: <jp7se4584elfu5ap6ieghd48dv2sutf142@4ax.com>
jrpfinch <jrpfinch@gmail.com> wrote:
>I would like to develop a script that can inplace edit a large file
>(potentially 1Gb), omitting some lines according to a regex and
>replacing some lines according to another regex. Here is an example
>of a script that does this with a couple of hardcoded patterns:
>
>{
> local ($^I, @ARGV) = ('', ($full_path_to_config_file));
> while (<>)
> {
> s/this/that/;
> s/foo/bar/;
> print unless ((/delete me/) or (/Delete Me Too/));
> }
>}
>
>What is the best way to scale this for n patterns? When I say best, I
>mean a good balance of speed and readability. Can I put all the
>omission patterns in an array and all the search/replace patterns in
>another array and somehow wedge them into the example above?
I'd probably store the REs pattern and replacement text as key/value
pairs in a hash(*) and just loop through them.
And a separate hash or array for the "delete" patterns.
*: that should work, because REs are just strings, too.
jue
------------------------------
Date: Thu, 09 Oct 2008 18:52:50 GMT
From: sln@netherlands.com
Subject: Re: Parsing CSV and " "
Message-Id: <ldkse4hsts55a35ihq37efa9sk95limigl@4ax.com>
On Wed, 8 Oct 2008 16:58:03 -0700 (PDT), hotkitty <stpra123@gmail.com> wrote:
>I'm trying to parse the following csv file in a linux environment:
>
>"this is row1 column0","this is row1 column1","this is row1
>column2","this is row1 column3","this
>is row1 column4"
>"this is row2 column0","this is row2 column1","this is row2
>column2","this is row2 column3","this is
>row2 column4"
>
>Pretty standard CSV but with the last column running onto the next
>line it gets screwed up somehow as my script doesn't recognize when a
>new row starts. I tried substituting the carriage return but still no
>luck. When I open up the file on my windows box w/ notepad I get the
>following (notice the " " that is added to the end of the
>line):
>"this is row1 column0","this is row1 column1","this is row1
>column2","this is row1 column3","this
>is row1 column4"" "
>"this is row2 column0","this is row2 column1","this is row2
>column2","this is row2 column3","this is
>row2 column4"" "
>
>Maybe I'm just overlooking some simple solution but how do I deal w/
>the " " as Linux doesn't recognize it?
>
>Thanks in advance. My code is as follows:
>
>my $csv= Text::CSV->new(); ####### I've also tried setting binary to 1
>but still didn't work
>open (CSV, "<", $thisfile) or die $!;
> while (<CSV>) {
> if ($csv->parse($_)) {
>my @csvcolumns= $csv->fields();
>my $newstuff = "$csvcolumns[1]";
>open(OUT, ">>$thatfile");
>print OUT "$newstuff\n";
>close(OUT);
>}
This is one way. I used the criteria buffer a row until /"$/, eol is found,
and just process remaining buffer if EOF. Otherwise, there is no delineation of rows..
sln
#############
# Csv1 Regex
#############
use strict;
use warnings;
my $fname = 'c:\temp\junk.csv';
open CSV, $fname or die "can't open $fname...";
my ($row, $tmp) = ('','');
my ($parsing, $count) = (1,1);
while ($parsing)
{
if (!($_ = <CSV>))
{
$parsing = 0;
} else {
$tmp = $_;
$tmp =~ s/\s+$//s;
$row .= " $tmp" if (length($tmp));
## buffer until '"$' or eof
next if ($tmp !~ /"$/);
}
print " (".$count++.") ----------\n";
while ($row =~ /\s*"+\s*([^"]*?)\s*"+\s*|\s*([^,\n]+)\s*/g)
{
my $val = defined $1 ? $1 : $2;
print "val = $val\n";
# ... push @ary, $val;
}
$row = $tmp = '';
}
close CSV;
__END__
output:
(1) ----------
val = this is row1 column0
val = this is row1 column1
val = this is row1 column2
val = this is row1 column3
val = this is row1 column4
(2) ----------
val = this is row2 column0
val = this is row2 column1
val = this is row2 column2
val = this is row2 column3
val = this is row2 column4
(3) ----------
val = this is row3 column0
val = this is row3 column1
junk.csv
----------
"this is row1 column0","this is row1 column1","this is row1
column2","this is row1 column3","this
is row1 column4"
"this is row2 column0","this is row2 column1","this is row2
column2","this is row2 column3","this is
row2 column4"
"this is row3 column0","this is row3 column1",
------------------------------
Date: Thu, 09 Oct 2008 19:11:23 GMT
From: sln@netherlands.com
Subject: Re: Parsing CSV and " "
Message-Id: <ldlse4tj15a4ah1b7ofq95b40jggp1v1lj@4ax.com>
On Thu, 09 Oct 2008 18:52:50 GMT, sln@netherlands.com wrote:
>On Wed, 8 Oct 2008 16:58:03 -0700 (PDT), hotkitty <stpra123@gmail.com> wrote:
>
>>I'm trying to parse the following csv file in a linux environment:
>>
>>"this is row1 column0","this is row1 column1","this is row1
>>column2","this is row1 column3","this
>>is row1 column4"
>>"this is row2 column0","this is row2 column1","this is row2
>>column2","this is row2 column3","this is
>>row2 column4"
>>
>>Pretty standard CSV but with the last column running onto the next
>>line it gets screwed up somehow as my script doesn't recognize when a
>>new row starts. I tried substituting the carriage return but still no
>>luck. When I open up the file on my windows box w/ notepad I get the
>>following (notice the " " that is added to the end of the
>>line):
>>"this is row1 column0","this is row1 column1","this is row1
>>column2","this is row1 column3","this
>>is row1 column4"" "
>>"this is row2 column0","this is row2 column1","this is row2
>>column2","this is row2 column3","this is
>>row2 column4"" "
>>
>>Maybe I'm just overlooking some simple solution but how do I deal w/
>>the " " as Linux doesn't recognize it?
>>
>>Thanks in advance. My code is as follows:
>>
>>my $csv= Text::CSV->new(); ####### I've also tried setting binary to 1
>>but still didn't work
>>open (CSV, "<", $thisfile) or die $!;
>> while (<CSV>) {
>> if ($csv->parse($_)) {
>>my @csvcolumns= $csv->fields();
>>my $newstuff = "$csvcolumns[1]";
>>open(OUT, ">>$thatfile");
>>print OUT "$newstuff\n";
>>close(OUT);
>>}
>
>This is one way. I used the criteria buffer a row until /"$/, eol is found,
>and just process remaining buffer if EOF. Otherwise, there is no delineation of rows..
>
>sln
>
>#############
># Csv1 Regex
>#############
>
>use strict;
>use warnings;
>
>my $fname = 'c:\temp\junk.csv';
>open CSV, $fname or die "can't open $fname...";
>
>my ($row, $tmp) = ('','');
>my ($parsing, $count) = (1,1);
>
>while ($parsing)
>{
> if (!($_ = <CSV>))
> {
> $parsing = 0;
> } else {
> $tmp = $_;
> $tmp =~ s/\s+$//s;
> $row .= " $tmp" if (length($tmp));
> ## buffer until '"$' or eof
> next if ($tmp !~ /"$/);
> }
> print " (".$count++.") ----------\n";
> while ($row =~ /\s*"+\s*([^"]*?)\s*"+\s*|\s*([^,\n]+)\s*/g)
> {
> my $val = defined $1 ? $1 : $2;
> print "val = $val\n";
> # ... push @ary, $val;
> }
> $row = $tmp = '';
>}
>
>close CSV;
>
>__END__
>
>output:
>
> (1) ----------
>val = this is row1 column0
>val = this is row1 column1
>val = this is row1 column2
>val = this is row1 column3
>val = this is row1 column4
> (2) ----------
>val = this is row2 column0
>val = this is row2 column1
>val = this is row2 column2
>val = this is row2 column3
>val = this is row2 column4
> (3) ----------
>val = this is row3 column0
>val = this is row3 column1
>
>
>junk.csv
>----------
>"this is row1 column0","this is row1 column1","this is row1
>column2","this is row1 column3","this
>is row1 column4"
>"this is row2 column0","this is row2 column1","this is row2
>column2","this is row2 column3","this is
>row2 column4"
>
>"this is row3 column0","this is row3 column1",
>
>
Btw, Excel will not do this correctly. Either you have to generate a
proper csv file (with EOR definition), or do this kind of a fix-up using your own criteria.
------------------------------
Date: Thu, 09 Oct 2008 10:15:35 -0500
From: Sharif <mislam@nospam.ciuc.edu>
Subject: RegEx: certain number of occurrences
Message-Id: <gcl76n$5vl$1@news.acm.uiuc.edu>
Hi,
I have a string where I need to extract the id surrounded by '%22'. The
id string can be 3 or 4 characters long. I can accomplish this in two
steps. How I can achieve this with one?
==============
use strict ;
my $string1 = "jid=%22TL3%22&scope=site";
$string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
print "$1\n";
my $string2 = "jid=%22TL3N%22&scope=site";
$string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
print "$1\n";
my $string3 = "jid=%22TL3%22&scope=site";
$string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
print "$1\n";
========================
# perl t.pl
TL3
TL3N
TL3%
I don't want the last one where '%' is not part of the id.
--s
------------------------------
Date: Thu, 9 Oct 2008 15:29:47 +0000 (UTC)
From: steveo@panix.com (Steven M. O'Neill)
Subject: Re: RegEx: certain number of occurrences
Message-Id: <gcl81b$opa$1@reader1.panix.com>
Sharif <mislam@nospam.ciuc.edu> wrote:
>Hi,
>
>I have a string where I need to extract the id surrounded by '%22'. The
>id string can be 3 or 4 characters long. I can accomplish this in two
>steps. How I can achieve this with one?
>
>
>==============
>use strict ;
>
>my $string1 = "jid=%22TL3%22&scope=site";
>$string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
>print "$1\n";
>
>
>my $string2 = "jid=%22TL3N%22&scope=site";
>$string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>
>
>my $string3 = "jid=%22TL3%22&scope=site";
>$string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>========================
>
>
>
>
># perl t.pl
>TL3
>TL3N
>TL3%
>
>I don't want the last one where '%' is not part of the id.
foreach $string ("jid=%22TL3%22&scope=site", "jid=%22TL3N%22&scope=site") {
$string =~ /jid=%22(.{3,4})%22/;
print $1 . "\n";
}
TL3
TL3N
--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo
------------------------------
Date: Thu, 09 Oct 2008 08:57:14 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: RegEx: certain number of occurrences
Message-Id: <j3ase4pm81ajbgou2a0ikmj8eql9l5uk5a@4ax.com>
Sharif <mislam@nospam.ciuc.edu> wrote:
>Hi,
>
>I have a string where I need to extract the id surrounded by '%22'. The
>id string can be 3 or 4 characters long. I can accomplish this in two
>steps. How I can achieve this with one?
>
>
>==============
>use strict ;
>
>my $string1 = "jid=%22TL3%22&scope=site";
>$string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
>print "$1\n";
>
>
>my $string2 = "jid=%22TL3N%22&scope=site";
>$string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>
>
>my $string3 = "jid=%22TL3%22&scope=site";
>$string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>========================
>
>
>
>
># perl t.pl
>TL3
>TL3N
>TL3%
>
>I don't want the last one where '%' is not part of the id.
"If you know what to capture, use m//, if you know what separates it use
split()":
print +(split('\%22', $string3))[1];
Or
$string3 =~ /jid=\%22([^\%]*)\%22/;
jue
------------------------------
Date: Thu, 09 Oct 2008 09:00:18 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: RegEx: certain number of occurrences
Message-Id: <mUpHk.60712$Nu3.9461@newsfe14.iad>
Steven M. O'Neill wrote:
> Sharif <mislam@nospam.ciuc.edu> wrote:
>>
>> I have a string where I need to extract the id surrounded by '%22'. The
>> id string can be 3 or 4 characters long. I can accomplish this in two
>> steps. How I can achieve this with one?
>>
>>
>> ==============
>> use strict ;
>>
>> my $string1 = "jid=%22TL3%22&scope=site";
>> $string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
>> print "$1\n";
>>
>> my $string2 = "jid=%22TL3N%22&scope=site";
>> $string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>> print "$1\n";
>>
>> my $string3 = "jid=%22TL3%22&scope=site";
>> $string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>> print "$1\n";
>> ========================
>>
>>
>> # perl t.pl
>> TL3
>> TL3N
>> TL3%
>>
>> I don't want the last one where '%' is not part of the id.
>
> foreach $string ("jid=%22TL3%22&scope=site", "jid=%22TL3N%22&scope=site") {
> $string =~ /jid=%22(.{3,4})%22/;
> print $1 . "\n";
> }
>
> TL3
> TL3N
$ perl -le'
foreach $string ("jid=%22TL3%22&scope=site", "some junk",
"jid=%22TL3N%22&scope=site") {
$string =~ /jid=%22(.{3,4})%22/;
print $1;
}
'
TL3
TL3
TL3N
You shouldn't use the numerical variables unless the match succeeded.
$ perl -le'
foreach $string ("jid=%22TL3%22&scope=site", "some junk",
"jid=%22TL3N%22&scope=site") {
if ( $string =~ /jid=%22(.{3,4})%22/ ) {
print $1;
}
}
'
TL3
TL3N
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Thu, 9 Oct 2008 16:08:51 +0000 (UTC)
From: steveo@panix.com (Steven M. O'Neill)
Subject: Re: RegEx: certain number of occurrences
Message-Id: <gclaaj$5l3$1@reader1.panix.com>
John W. Krahn <jwkrahn@shaw.ca> wrote:
>You shouldn't use the numerical variables unless the match succeeded.
Nice catch, thanks.
--
Steven O'Neill steveo@panix.com
Brooklyn, NY http://www.panix.com/~steveo
------------------------------
Date: Thu, 09 Oct 2008 17:04:06 GMT
From: sln@netherlands.com
Subject: Re: RegEx: certain number of occurrences
Message-Id: <78ese4hub6u5l1mu0aoqln58n2ume2tdmi@4ax.com>
On Thu, 09 Oct 2008 10:15:35 -0500, Sharif <mislam@nospam.ciuc.edu> wrote:
>Hi,
>
>I have a string where I need to extract the id surrounded by '%22'. The
>id string can be 3 or 4 characters long. I can accomplish this in two
>steps. How I can achieve this with one?
>
>
>==============
>use strict ;
>
>my $string1 = "jid=%22TL3%22&scope=site";
>$string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
>print "$1\n";
>
>
>my $string2 = "jid=%22TL3N%22&scope=site";
>$string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>
>
>my $string3 = "jid=%22TL3%22&scope=site";
>$string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>print "$1\n";
>========================
>
>
>
>
># perl t.pl
>TL3
>TL3N
>TL3%
>
>I don't want the last one where '%' is not part of the id.
>
>
>--s
>
>
Just another variation of the rest.
Its clear you are looking for other qualifiers other
than %22. Check that.
sln
use strict;
use warnings;
my @stra = (
'jid=%22TL3%22&scope=site',
'jid=%22TL3N%22&scope=site',
'jid=%22TL3%22&scope=site'
);
foreach my $sval (@stra)
{
$sval =~ /(?:jn|jid)\s*=\s*%22(\w{3,4})%22/;
print "$1\n" if (defined $1);
}
__END__
TL3
TL3N
TL3
------------------------------
Date: Thu, 09 Oct 2008 17:13:02 GMT
From: sln@netherlands.com
Subject: Re: RegEx: certain number of occurrences
Message-Id: <1sese4p0a4v2h0v1q740aka20b9esjk2t8@4ax.com>
On Thu, 09 Oct 2008 17:04:06 GMT, sln@netherlands.com wrote:
>On Thu, 09 Oct 2008 10:15:35 -0500, Sharif <mislam@nospam.ciuc.edu> wrote:
>
>>Hi,
>>
>>I have a string where I need to extract the id surrounded by '%22'. The
>>id string can be 3 or 4 characters long. I can accomplish this in two
>>steps. How I can achieve this with one?
>>
>>
>>==============
>>use strict ;
>>
>>my $string1 = "jid=%22TL3%22&scope=site";
>>$string1 =~ /(?:jn|jid)=(?:%22)?(.{3})/ ;
>>print "$1\n";
>>
>>
>>my $string2 = "jid=%22TL3N%22&scope=site";
>>$string2 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>>print "$1\n";
>>
>>
>>my $string3 = "jid=%22TL3%22&scope=site";
>>$string3 =~ /(?:jn|jid)=(?:%22)?(.{3,4})/ ;
>>print "$1\n";
>>========================
>>
>>
>>
>>
>># perl t.pl
>>TL3
>>TL3N
>>TL3%
>>
>>I don't want the last one where '%' is not part of the id.
>>
>>
>>--s
>>
>>
>
>Just another variation of the rest.
>Its clear you are looking for other qualifiers other
>than %22. Check that.
>
>sln
>
>
>use strict;
>use warnings;
>
>my @stra = (
>'jid=%22TL3%22&scope=site',
>'jid=%22TL3N%22&scope=site',
>'jid=%22TL3%22&scope=site'
>);
>
>foreach my $sval (@stra)
>{
if ($sval =~ /(?:jn|jid)\s*=\s*%22(\w{3,4})%22/)
{
print "$1\n";
}
>}
>
>__END__
>
>TL3
>TL3N
>TL3
------------------------------
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.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
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 V11 Issue 1911
***************************************