pattern match

  • Thread starter Geezer From The Freezer
  • Start date
G

Geezer From The Freezer

@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.
 
N

Niall Macpherson

Geezer said:
$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

Jürgen Exner

Geezer said:
@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
 
I

it_says_BALLS_on_your forehead

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";
 
J

John W. Krahn

Geezer said:
@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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top