Re: Pass Match Values to Variable



perl Newbie <Ansher.M@xxxxxxxxx> wrote:
On Mar 31, 6:10 pm, Peter Makholm <pe...@xxxxxxxxxxx> wrote:
perl Newbie <Anshe...@xxxxxxxxx> writes:
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

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


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



Relevant Pages