Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Fails To List All The Multiple Matches In The Same Line?

Reply
Thread Tools

Perl Fails To List All The Multiple Matches In The Same Line?

 
 
Cibalo
Guest
Posts: n/a
 
      07-08-2009
Hello,

I would like to list all the 5-digit zip codes in my database, of
which a line may contain more than one zip codes. Then, I create a
test database, testdb, for testing as follows.

# echo -e 'zip1 10036; zip2 48226; zip3 94128\nzip4 V8Y 1L1; zip5
400069\nzip6 \nzip7 12345' > testdb
# cat testdb
zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
# perl -wnl -e '/\b[0-9]{5}\b/g and print "$.: $&";' testdb
1: 10036
4: 12345
# grep -now -e '[0-9]\{5\}' testdb
1:10036
48226
94128
4:12345
#

Even with the global modifier, the above perl script lists only the
first pattern match with multiple matches in the same line. But I can
make it worked with grep as listed above.

What's wrong with my perl script? What am I missing?

# perl --version; grep --version
This is perl, v5.10.0 built for i386-linux-thread-multi
Copyright 1987-2007, 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 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.org/, the Perl Home
Page.

grep (GNU grep) 2.5.1
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
#

Thank you very much for your assistance.

Best Regards,

cibalo
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      07-08-2009
Cibalo wrote:
>
> I would like to list all the 5-digit zip codes in my database, of
> which a line may contain more than one zip codes. Then, I create a
> test database, testdb, for testing as follows.
>
> # echo -e 'zip1 10036; zip2 48226; zip3 94128\nzip4 V8Y 1L1; zip5
> 400069\nzip6 \nzip7 12345' > testdb
> # cat testdb
> zip1 10036; zip2 48226; zip3 94128
> zip4 V8Y 1L1; zip5 400069
> zip6
> zip7 12345
> # perl -wnl -e '/\b[0-9]{5}\b/g and print "$.: $&";' testdb
> 1: 10036
> 4: 12345
> # grep -now -e '[0-9]\{5\}' testdb
> 1:10036
> 48226
> 94128
> 4:12345
> #
>
> Even with the global modifier, the above perl script lists only the
> first pattern match with multiple matches in the same line. But I can
> make it worked with grep as listed above.


The problem is that even with the global option the pattern is evaluated
in scalar context and so will only match once. You need to either match
in list context:

$ echo "zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
" | perl -lne'print "$.: $_" for /\b[0-9]{5}\b/g'
1: 10036
1: 48226
1: 94128
4: 12345


Or match all patterns in scalar context:

$ echo "zip1 10036; zip2 48226; zip3 94128
zip4 V8Y 1L1; zip5 400069
zip6
zip7 12345
" | perl -lne'print "$.: $1" while /\b([0-9]{5})\b/g'
1: 10036
1: 48226
1: 94128
4: 12345



John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-09-2009
On Wed, 08 Jul 2009 01:27:10 -0700, "John W. Krahn" <(E-Mail Removed)> wrote:

>Cibalo wrote:
>>

<snip>
>> Even with the global modifier, the above perl script lists only the
>> first pattern match with multiple matches in the same line. But I can
>> make it worked with grep as listed above.

>
>The problem is that even with the global option the pattern is evaluated
>in scalar context and so will only match once. You need to either match
>in list context:
>
>$ echo "zip1 10036; zip2 48226; zip3 94128
>zip4 V8Y 1L1; zip5 400069
>zip6
>zip7 12345
>" | perl -lne'print "$.: $_" for /\b[0-9]{5}\b/g'

^^^^^^^^^^^^^^^^^^^
Carefull, someone might accuse you of obfuscation.

while (<DATA>)
{
print;
@_ = $_ =~ /\b[0-9]{5}\b/g;
for (@_)
{
print "$.: $_\n";
}
}

-sln

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-09-2009
On Wed, 08 Jul 2009 01:27:10 -0700, "John W. Krahn" <(E-Mail Removed)> wrote:

>Cibalo wrote:
>>
>> I would like to list all the 5-digit zip codes in my database, of
>> which a line may contain more than one zip codes. Then, I create a
>> test database, testdb, for testing as follows.
>>
>> # echo -e 'zip1 10036; zip2 48226; zip3 94128\nzip4 V8Y 1L1; zip5
>> 400069\nzip6 \nzip7 12345' > testdb
>> # cat testdb
>> zip1 10036; zip2 48226; zip3 94128
>> zip4 V8Y 1L1; zip5 400069
>> zip6
>> zip7 12345
>> # perl -wnl -e '/\b[0-9]{5}\b/g and print "$.: $&";' testdb
>> 1: 10036
>> 4: 12345
>> # grep -now -e '[0-9]\{5\}' testdb
>> 1:10036
>> 48226
>> 94128
>> 4:12345
>> #
>>
>> Even with the global modifier, the above perl script lists only the
>> first pattern match with multiple matches in the same line. But I can
>> make it worked with grep as listed above.

>
>The problem is that even with the global option the pattern is evaluated
>in scalar context and so will only match once. You need to either match
>in list context:
>
>$ echo "zip1 10036; zip2 48226; zip3 94128
>zip4 V8Y 1L1; zip5 400069
>zip6
>zip7 12345
>" | perl -lne'print "$.: $_" for /\b[0-9]{5}\b/g'
>1: 10036
>1: 48226
>1: 94128
>4: 12345
>
>
>Or match all patterns in scalar context:
>
>$ echo "zip1 10036; zip2 48226; zip3 94128
>zip4 V8Y 1L1; zip5 400069
>zip6
>zip7 12345
>" | perl -lne'print "$.: $1" while /\b([0-9]{5})\b/g'

^^^^^^^^^^^^^^^^^^^^^^^
>1: 10036
>1: 48226
>1: 94128
>4: 12345
>
>
>
>John


I always enjoy (and marvel at) seeing Unix 1 liner shell
compositions here. Seems so at ease and natural. I just got Windyo'z.
When I cut and paste these 1 liners (even though my shell does 'echo')
each line is treated as a new command, even when I batch it.
Unfortunately, the {'"} syntax is also different under Windows (and I
have XP, the great).

Why can't windows do unix?

Oh well, I have to settle for the 'jist' and test using a pl file.
This last works as expected, the first (list context) is slightly obfuscated,
or would be to the OP, who never got past the /g switch meaning.

Btw, nice explanation John.

while (<DATA>)
{
while (/\b([0-9]{5})\b/g)
{
print "$.: $1\n";
}
}

-sln

 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-09-2009
On Thu, 09 Jul 2009 13:51:48 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>On Wed, 08 Jul 2009 01:27:10 -0700, "John W. Krahn" <(E-Mail Removed)> wrote:
>
>>Cibalo wrote:
>>>

><snip>
>>> Even with the global modifier, the above perl script lists only the
>>> first pattern match with multiple matches in the same line. But I can
>>> make it worked with grep as listed above.

>>
>>The problem is that even with the global option the pattern is evaluated
>>in scalar context and so will only match once. You need to either match
>>in list context:
>>
>>$ echo "zip1 10036; zip2 48226; zip3 94128
>>zip4 V8Y 1L1; zip5 400069
>>zip6
>>zip7 12345
>>" | perl -lne'print "$.: $_" for /\b[0-9]{5}\b/g'

> ^^^^^^^^^^^^^^^^^^^
>Carefull, someone might accuse you of obfuscation.
>
>while (<DATA>)
>{
> print;
> @_ = $_ =~ /\b[0-9]{5}\b/g;
> for (@_)
> {
> print "$.: $_\n";
> }
>}
>
>-sln


Funny how
for /\b[0-9]{5}\b/g
works, but this
@_ = $_ =~ /\b[0-9]{5}\b/g;
for ()
doesen't.

As though the shortcut's got shorted.

-sln

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      07-10-2009
On Thu, 09 Jul 2009 14:09:13 -0700, sln wrote:

> Why can't windows do unix?


In this case, it can. Install cygwin.

HTH,
M4
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-10-2009
Martijn Lievaart <(E-Mail Removed)> wrote:
>On Thu, 09 Jul 2009 14:09:13 -0700, sln wrote:
>
>> Why can't windows do unix?


"Why can't a Ford do a Chevy?"

>In this case, it can. Install cygwin.


That doesn't "do Unix" (whatever that is supposed to mean).
It merely provides the typical Unix utilities in the Windows
environment.

jue
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-13-2009
On Fri, 10 Jul 2009 14:34:52 -0700, Jürgen Exner <(E-Mail Removed)> wrote:

>Martijn Lievaart <(E-Mail Removed)> wrote:
>>On Thu, 09 Jul 2009 14:09:13 -0700, sln wrote:
>>
>>> Why can't windows do unix?

>
>"Why can't a Ford do a Chevy?"


In this case, the only thing a 'Chevy' can do is Camaro.
Ford can do anything. Buy a horse (Mustang).

>
>>In this case, it can. Install cygwin.

>
>That doesn't "do Unix" (whatever that is supposed to mean).


Why does Unix do /dir/dir/dir/not_dir (whatever that means), and why forward slashes?
Is /dir/dir/dir/not_\dir available?

>It merely provides the typical Unix utilities in the Windows
>environment.


This means a compiler right?

>
>jue


-sln
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      07-17-2009
On Mon, 13 Jul 2009 13:36:32 -0700, sln wrote:

>>It merely provides the typical Unix utilities in the Windows
>>environment.

>
> This means a compiler right?


No it means binaries (and in typical unix tradition, also a compiler,
it's one of the binaries).

M4
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      07-17-2009
On Fri, 17 Jul 2009 22:24:19 +0200, Martijn Lievaart <(E-Mail Removed)> wrote:

>On Mon, 13 Jul 2009 13:36:32 -0700, sln wrote:
>
>>>It merely provides the typical Unix utilities in the Windows
>>>environment.

>>
>> This means a compiler right?

>
>No it means binaries (and in typical unix tradition, also a compiler,
>it's one of the binaries).
>
>M4


Since I have to learn everything on my own (because class is too slow),
they (an employer) would have to pay me (unix deliverables) while I am
forced to learn. To shift to different OSs' all the time takes a lot
out of me. I can deliver unix code with a compiler that keeps me in line.
I'm so lazy I make the compiler do my work. Make it tell me my errors,
take me to my errors, take me to the docs, make it fix it for me.
IDE's are my slave's, they get out of line ... I pop em in the mout'

-sln
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking a string against multiple matches Aaron Scott Python 7 12-02-2008 03:10 PM
How to access multiple group matches? Christoph Krammer Python 1 04-06-2007 03:10 PM
Constant.t fails 240 of 272 tests and recurs.t fails 1 of 25 tests on HPUX using perl 5.8.7 dayo Perl Misc 11 12-16-2005 09:09 PM
Speeding up multiple regex matches Talin Python 3 11-19-2005 08:31 PM
java.util.regex and multiple matches argabalala@yahoo.fr Java 4 09-28-2005 09:35 AM



Advertisments