Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pattern match

Reply
Thread Tools

pattern match

 
 
Geezer From The Freezer
Guest
Posts: n/a
 
      07-26-2005
@a contains:-

abcdefg1234
1234 this is a test
test
test 1234

I want to print everything with 1234 so:-



$list = @a =~ /1234/ ;
chomp($list);
print ($list);

Why doesn't this work?

Be grateful if someone can point me in the right direction.
 
Reply With Quote
 
 
 
 
Niall Macpherson
Guest
Posts: n/a
 
      07-26-2005

Geezer From The Freezer wrote:
>
> $list = @a =~ /1234/ ;
> chomp($list);
> print ($list);
>
> Why doesn't this work?
>
> Be grateful if someone can point me in the right direction.


It's not clear from your post what you expect this to produce
I'll assume you're expecting the altered array to be printed.

However you are assigning an array (@a) to a scalar ($list)

Doing this results in the $list variable being assigned the number of
elements in the array , not the contents of the array - i.e. in this
case 4.

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-26-2005
Geezer From The Freezer wrote:
> @a contains:-
>
> abcdefg1234
> 1234 this is a test
> test
> test 1234
>
> I want to print everything with 1234 so:-
>
>
>
> $list = @a =~ /1234/ ;
> chomp($list);
> print ($list);
>
> Why doesn't this work?


"it doesn't work" is about the worst possible description of a problem.
What _does_ it do? Print something you didn't expect? Fail with an error
message? Hang in a loop? ...?

Well, at least you sort of told us what you did expect.

If you would have "use warnings;" then perl would have told you already what
is probably wrong (see output of warnings enabled).
Another hint can be found in the man page for m// (perldoc perlop, section
Regexp Quote-Like Operators):

If the "/g" option is not used, "m//" in list context returns a
list consisting of the subexpressions matched by the parentheses
in the pattern, i.e., ("$1", "$2", "$3"...). [...]

I don't see any capturing parathesis in your code.

When there are no parentheses in the pattern, the
return value is the list "(1)" for success.

Well, guess that's not what you are looking for, either. Seems like m// is
the wrong tool for the job.

> Be grateful if someone can point me in the right direction.


From your description it seems you are looking for a plain old grep()?

jue


 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      07-26-2005
yeah, for list-filtering, grep is best.

my @test = qw(
test1234
2134
1234
blah
test
);

print "@test\n";
my @nums = grep /1234/,@test;
print "@nums\n";


Geezer From The Freezer wrote:
> @a contains:-
>
> abcdefg1234
> 1234 this is a test
> test
> test 1234
>
> I want to print everything with 1234 so:-
>
>
>
> $list = @a =~ /1234/ ;
> chomp($list);
> print ($list);
>
> Why doesn't this work?
>
> Be grateful if someone can point me in the right direction.


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-26-2005
Geezer From The Freezer wrote:
> @a contains:-
>
> abcdefg1234
> 1234 this is a test
> test
> test 1234
>
> I want to print everything with 1234 so:-
>
> $list = @a =~ /1234/ ;
> chomp($list);
> print ($list);
>
> Why doesn't this work?


The binding operator (=~) operates on scalars so the array is used in
scalar context which in this case results in the expression

'4' =~ /1234/

and the match operator (m//) is evaluated in scalar context because it
is being assigned to a scalar which in this case is assigned the value
'' (false) because /1234/ doesn't match '4'.

perldoc perlop


> Be grateful if someone can point me in the right direction.


perldoc -f grep


John
--
use Perl;
program
fulfillment
 
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
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
Match doesn't match Volkan Civelek Ruby 4 07-19-2006 07:44 AM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments