Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   pattern match (http://www.velocityreviews.com/forums/t893447-pattern-match.html)

Geezer From The Freezer 07-26-2005 02:42 PM

pattern match
 
@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.

Niall Macpherson 07-26-2005 03:26 PM

Re: pattern match
 

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.


Jürgen Exner 07-26-2005 03:28 PM

Re: pattern match
 
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



it_says_BALLS_on_your forehead 07-26-2005 04:08 PM

Re: pattern match
 
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.



John W. Krahn 07-26-2005 08:20 PM

Re: pattern match
 
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


All times are GMT. The time now is 10:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.