Pass Match Values to Variable

P

perl Newbie

Hi

Please let me know what I am doing wrong .. I need to pass the regex
match values to a new variable.

This is the code .. but both $capture_txt & @nettxt is blank, values
I am expecting in @nettxt are

SAMPLE (NET) & SINGLE (net).


@txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);


foreach $txt(@txt) {

if ($txt =~ /\w+\s*\(net\)/i) {
print "True - $txt \n";
$capture_txt = /\w+\s*\(net\)/ ;
print "$capture_txt \n";
push @nettxt, /(\w+\s*\(net\))/g;
}

}

print @nettxt;
 
P

Peter Makholm

perl Newbie said:
Please let me know what I am doing wrong .. I need to pass the regex
match values to a new variable.

You code is missing the following two lines:

use strict;
use warnings;

Insert them and remember to decalre all you variables with my, and
then perl will tell you the two places where you're matching against
uninitialized values.

//Makholm
 
P

perl Newbie

You code is missing the following two lines:

    use strict;
    use warnings;

Insert them and remember to decalre all you variables with my, and
then perl will tell you the two places where you're matching against
uninitialized values.

//Makholm

I tried this but I am getting some error message, I am new to perl if
you could explain me that will be nice

use strict;
use warnings;


my @txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);

my @nettxt=();;
foreach my $txt(@txt) {

if ($txt =~ /\w+\s*\(net\)/i) {
print "True - $txt ";
my $capture_txt = /\w+\s*\(net\)/gi ;
print "$capture_txt \n";
push @nettxt, /(\w+\s*\(net\))/gi;
}

}

print @nettxt;


cmd message ...

Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 20.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 22.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 20.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 22.
True - SAMPLE (NET)
True - SINGLE (net) junk text
 
P

Peter Makholm

perl Newbie said:
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 20.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 22.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 20.
Use of uninitialized value $_ in pattern match (m//) at H:\Test
\temp3.pl line 22.

This tells you two things. You're matching against $_ and it contains
an uninitialized value. You probably want to match against $txt
instead.
 
T

Tad J McClellan

perl Newbie said:
I tried this but I am getting some error message,


They are not error messages. They are warning messages.

You can tell this by looking up the message with

perldoc perldiag

I am new to perl if
you could explain me that will be nice


If you can figure it out for yourself, it will be even nicer...

.... because then you won't be dependant on others every time
you encounter the problem.

if ($txt =~ /\w+\s*\(net\)/i) {


Q: What string are you trying to match the pattern against here?

A: The string in $txt.

my $capture_txt = /\w+\s*\(net\)/gi ;


Q: What string are you trying to match the pattern against here?

A: The warning message gives you the answer. So does the documentation
for the operator that you are using:

perldoc perlop

/PATTERN/cgimosx

If no string is specified via the =~ or !~ operator,
the $_ string is searched.


Q: What will be stored in $capture_text if the pattern match succeeds?

A: A true value

Q: What will be stored in $capture_text if the pattern match fails?

A: A false value


Note that neither of those possibilities is the captured text...


Q: How can you tell what will be stored in $capture_txt?

A: By reading the documentation for the operator that you are using:

perldoc perlop

/PATTERN/cgimosx

Searches a string for a pattern match, and in scalar context returns
true if it succeeds, false if it fails.


Above you are using a pattern match in a scalar context.

push @nettxt, /(\w+\s*\(net\))/gi;


Here you are using a pattern match in a list context.

Why are you including the m//g modifier? Do you expect the pattern
to match more than once on the same line?

Use of uninitialized value $_ in pattern match (m//) at H:\Test ^^
^^
\temp3.pl line 20.


There is the name of the variable that you are using but that has
an uninitialize value.

Figuring out where you are using it leads to the solution to your problem.


So, bind your pattern match to the correct string ($txt), don't include
the m//g option, do include some capturing parenthesis, do a pattern
match in list context rather than in scalar context:

my($capture_txt) = $txt =~ /(\w+\s*\(net\))/i ;

Bind your pattern match to the correct string ($txt), don't include
the m//g option:

push @nettxt, $txt =~ /(\w+\s*\(net\))/i;


Note that you don't need the if() block at all:

-----------------
#!/usr/bin/perl
use warnings;
use strict;

my @txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);

my @nettxt=();;
foreach my $txt(@txt) {
push @nettxt, $txt =~ /(\w+\s*\(net\))/i;
}

print @nettxt;
 
P

perl Newbie

They are not error messages. They are warning messages.

You can tell this by looking up the message with

    perldoc perldiag


If you can figure it out for yourself, it will be even nicer...

... because then you won't be dependant on others every time
you encounter the problem.


Q: What string are you trying to match the pattern against here?

A: The string in $txt.


Q: What string are you trying to match the pattern against here?

A: The warning message gives you the answer. So does the documentation
   for the operator that you are using:

    perldoc perlop

        /PATTERN/cgimosx

        If no string is specified via the =~ or !~ operator,
        the $_ string is searched.

Q: What will be stored in $capture_text if the pattern match succeeds?

A: A true value

Q: What will be stored in $capture_text if the pattern match fails?

A: A false value

Note that neither of those possibilities is the captured text...

Q: How can you tell what will be stored in $capture_txt?

A: By reading the documentation for the operator that you are using:

    perldoc perlop

        /PATTERN/cgimosx

        Searches a string for a pattern match, and in scalar context returns
        true if it succeeds, false if it fails.

Above you are using a pattern match in a scalar context.


Here you are using a pattern match in a list context.

Why are you including the m//g modifier? Do you expect the pattern
to match more than once on the same line?


                             ^^
                             ^^


There is the name of the variable that you are using but that has
an uninitialize value.

Figuring out where you are using it leads to the solution to your problem..

So, bind your pattern match to the correct string ($txt), don't include
the m//g option, do include some capturing parenthesis, do a pattern
match in list context rather than in scalar context:

     my($capture_txt) = $txt =~ /(\w+\s*\(net\))/i ;

Bind your pattern match to the correct string ($txt), don't include
the m//g option:

     push @nettxt, $txt =~ /(\w+\s*\(net\))/i;

Note that you don't need the if() block at all:

-----------------
#!/usr/bin/perl
use warnings;
use strict;

my @txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);

my @nettxt=();;
foreach my $txt(@txt) {
    push @nettxt, $txt =~ /(\w+\s*\(net\))/i;

}

print @nettxt;
-----------------

--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"- Hide quoted text -

- Show quoted text -

Thanks for your detailed explanation.

I am new to Perl (learning for the last 15 days) and using "Learning
Perl" book. But it doesn’t help me much in understanding the way you
have explained the details here :( Some of the other links I freq. use
are

http://www.physics.rutgers.edu/~kotliar/perltut.html
http://www.comp.leeds.ac.uk/Perl/basic.html

Could you please suggest any book or links which will help me to
better understand the concepts?

Thanks again.
 
J

John W. Krahn

perl said:
Please let me know what I am doing wrong .. I need to pass the regex
match values to a new variable.

This is the code .. but both $capture_txt & @nettxt is blank, values
I am expecting in @nettxt are

SAMPLE (NET) & SINGLE (net).


@txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);


foreach $txt(@txt) {

if ($txt =~ /\w+\s*\(net\)/i) {
print "True - $txt \n";
$capture_txt = /\w+\s*\(net\)/ ;
print "$capture_txt \n";
push @nettxt, /(\w+\s*\(net\))/g;
}

}

print @nettxt;

$ perl -le'
my @txt = (
"SAMPLE (NET)",
"Sample txt 1",
"sample txt 2",
"SINGLE (net) junk text",
"Single txt 1",
"Single txt 2",
);
my @nettxt = map /(\w+\s*\(net\))/i ? $1 : (), @txt;
print for @nettxt;
'
SAMPLE (NET)
SINGLE (net)




John
 
J

Jürgen Exner

$capture_txt = /\w+\s*\(net\)/ ;

You are matching against $_, which from the rest of your code ist empty,
so the pattern match will fail. The return value of the pattern match is

m/PATTERN/cgimosx
/PATTERN/cgimosx
Searches a string for a pattern match, and in scalar context
returns true if it succeeds, false if it fails.
print "$capture_txt \n";

Which means you are printing the empty string (= the textual value of
the logical false return value) here.

jue
 
J

Jürgen Exner

Jürgen Exner said:
You are matching against $_, which from the rest of your code ist empty,
so the pattern match will fail. The return value of the pattern match is

m/PATTERN/cgimosx
/PATTERN/cgimosx
Searches a string for a pattern match, and in scalar context
returns true if it succeeds, false if it fails.


Which means you are printing the empty string (= the textual value of
the logical false return value) here.

Forgot to mention:

From your description it seems you want to capture the exact text that
was matched by the RE. To do so you just need to 'group' the part of the
RE, for which you want to capture the text, e.g.

/(\w+\s*\(net\))/

for the whole RE.
Any matched by the RE can then be found in the variable $1 (and $2, $3,
.... if you have more than one group in the RE).

Note that those variable are only valid if the match succeeded.
Therefore the typical usage is

if (//\w+\s*\(net\)/) {
# so something with $1
}

jue
 
A

A Dude

Hi

Please let me know what I am doing wrong .. I need to pass the regex
match values to a new variable.

This is the code .. but both  $capture_txt  & @nettxt is blank, values
I am expecting in @nettxt are

SAMPLE (NET) & SINGLE (net).

@txt=(
"SAMPLE (NET)"
,"Sample txt 1"
,"sample txt 2"
,"SINGLE (net) junk text"
,"Single txt 1"
,"Single txt 2"
);

foreach $txt(@txt) {

        if ($txt =~ /\w+\s*\(net\)/i) {
                print "True - $txt \n";
                $capture_txt = /\w+\s*\(net\)/ ;
                print "$capture_txt \n";
                push @nettxt, /(\w+\s*\(net\))/g;
        }

}

print @nettxt;

Shorter and simpler:

my @nettxt = map { /(\w+\s*\(net\))/gi } @txt;


What I think you wanted to do with $capture_txt is:

my ( $capture_text ) = $txt =~ /(\w+\s*\(net\))/i;
if ( $capture_text ) {
...
}

You're not capturing anything with \(...\), you're just looking for
'(net)'. You need un-escaped '(' ( and ')') to capture anything.
 
T

Tad J McClellan

perl Newbie said:
On Mar 31, 6:49?pm, Tad J McClellan <[email protected]> wrote:

[ snip a bunch ]


It is bad manners to quote .sigs.




Don't quote that silliness either please.

Thanks for your detailed explanation.

I am new to Perl (learning for the last 15 days) and using "Learning
Perl" book.


I am very familiar with that book (check the Acknowledgements).

But it doesn?t help me much in understanding the way you
have explained the details here


Yes it does.

It covers every single thing that I mentioned.

Read it again. Slowly.



Errr, a lot has changed in 10 years...

Could you please suggest any book or links which will help me to
better understand the concepts?


The absolutely most authoritative source of information about Perl
is the documentation that comes with Perl.

It is better than "Learning Perl" or any other printed book.

It is better than any random web page.

In about five or six paragraphs it addressed every problem you were having.

Use the docs, Luke.
 
J

Justin C

Thanks for your detailed explanation.

I am new to Perl (learning for the last 15 days) and using "Learning
Perl" book. But it doesn?t help me much in understanding the way you
have explained the details here :( Some of the other links I freq. use
are

Learning Perl is the most read book I have. Until you've actually read
it, and worked through the exercises, it's not that helpful to 'dip'
into. You really need to work through it, cover to cover, do all the
exercises, and then you can use it as a reference.

Don't dismiss that book!

Justin.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top