Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > A newbie question on pattern matching

Reply
Thread Tools

A newbie question on pattern matching

 
 
DelphiDude
Guest
Posts: n/a
 
      07-22-2003
REAL newbie. I just got my book in the mail yesterday. And of course at
this point, I am not trying to produce anything, just understand stuff
that is a long way from Turbo Pascal and C++.

So far everything falls into place once I code it up and change it around
and play with it.

However, some questions on the following snippit (which doesn't work, by
the way).

#!/usr/bin/perl -w

testsub("testword");

sub testsub{
print $_[0]; # just as a test to see if it works.
if ($_[0] = /test/){
print "found it";
}
}

First off, as I understand it, a call to a sub with a value(s)
automatically puts it into a temporary array called $_[].

And it actually does work. I can print the value of $_[0] which correctly
comes out as "testword". But the match comparison errors out with...

Use of uninitialized value in pattern match (m//) at ./test2 line 7.

What value? The left side variable is ok. The /test/ is just a string
pattern as far as I can see.

My Perl book kind of glosses over this part and move on to much more
indepth pattern matching. Can anybody clue me in?

DD

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-22-2003
DelphiDude wrote:
> REAL newbie. I just got my book in the mail yesterday. And of course
> at this point, I am not trying to produce anything, just understand
> stuff that is a long way from Turbo Pascal and C++.
>
> So far everything falls into place once I code it up and change it
> around and play with it.
>
> However, some questions on the following snippit (which doesn't work,
> by the way).
>
> #!/usr/bin/perl -w
>
> testsub("testword");
>
> sub testsub{
> print $_[0]; # just as a test to see if it works.
> if ($_[0] = /test/){
> print "found it";
> }
> }
>
> First off, as I understand it, a call to a sub with a value(s)
> automatically puts it into a temporary array called $_[].


Well, not temporary. It is the local variable for the arguments inside of
the sub.

> And it actually does work. I can print the value of $_[0] which
> correctly comes out as "testword". But the match comparison errors
> out with...
>
> Use of uninitialized value in pattern match (m//) at ./test2 line 7.
>
> What value? The left side variable is ok. The /test/ is just a
> string pattern as far as I can see.
>
> My Perl book kind of glosses over this part and move on to much more
> indepth pattern matching. Can anybody clue me in?


Actually quite simple if you know what to look for

Unless specified otherwise the match operator will try to match $_. But
there is no place in the whole script that defines a value for $_. Therefore
you are getting the uninitialized value error message.
The second mistake is that you are assigning the result of the match to
$_[0]. That does not make any sense at this place.

What you want instead is to bind the match operator to $_[0] such that is
will match $_[0] instead of $_.
And this is done by using the binding operator "=~" (without the quotes).

So just add the tilde to your code and it should work.

Two things to learn here:
- you need to write and read your Perl code carefully. Missing a single
character can change the meaning of your whole program. And Perl will not
always warn you!
- to look up the functionality of an operator use "perldoc perlop" (that
would have explained the "=" and the "=~" operators), to look up the
functionality of a function use "perldoc -f functionname". "perldoc -f m"
would have told you that m is the match operator and that you should check
it out in the perldoc perlop manpage again.

jue


 
Reply With Quote
 
 
 
 
Joe
Guest
Posts: n/a
 
      07-26-2003

"DelphiDude" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> REAL newbie. I just got my book in the mail yesterday. And of course at
> this point, I am not trying to produce anything, just understand stuff
> that is a long way from Turbo Pascal and C++.
>
> So far everything falls into place once I code it up and change it around
> and play with it.
>
> However, some questions on the following snippit (which doesn't work, by
> the way).
>
> #!/usr/bin/perl -w
>
> testsub("testword");
>
> sub testsub{
> print $_[0]; # just as a test to see if it works.
> if ($_[0] = /test/){
> print "found it";

This is pretty nasty code man..
> }
> }



I would rewrite this..

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

if($word =~ /test)
{
print "FOUND WORD in \"$word\"\n";
}

exit;
#######################################

then once it works in std conext put it into subroutine

#######################################
#!/usr/bin/perl -w

my $word = "testword";
print "WORD: \"$word\"\n";

$find = findtest($word);
if($find eq "yes")
{ print "FOUND WORD in \"$word\"\n"; }
else
{ print "NO FOUND WORD in \"$word\"\n"; }

exit;

##### SUBS

sub findtest
{
my $word = shift(@_);
my $found = "no";
if($word =~ /test)
{
print "FOUND WORD in \"$word\"\n";
$found = "yes";
}
return $found;
}

#######################################



>
> First off, as I understand it, a call to a sub with a value(s)
> automatically puts it into a temporary array called $_[].
>
> And it actually does work. I can print the value of $_[0] which correctly
> comes out as "testword". But the match comparison errors out with...
>
> Use of uninitialized value in pattern match (m//) at ./test2 line 7.
>
> What value? The left side variable is ok. The /test/ is just a string
> pattern as far as I can see.
>
> My Perl book kind of glosses over this part and move on to much more
> indepth pattern matching. Can anybody clue me in?
>
> DD
>



 
Reply With Quote
 
Joe
Guest
Posts: n/a
 
      07-26-2003

"Joe" <(E-Mail Removed)> wrote in message
news:MQuUa.983210$(E-Mail Removed)...
>
> "DelphiDude" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> > REAL newbie. I just got my book in the mail yesterday. And of course at
> > this point, I am not trying to produce anything, just understand stuff
> > that is a long way from Turbo Pascal and C++.
> >
> > So far everything falls into place once I code it up and change it

around
> > and play with it.
> >
> > However, some questions on the following snippit (which doesn't work, by
> > the way).
> >
> > #!/usr/bin/perl -w
> >
> > testsub("testword");
> >
> > sub testsub{
> > print $_[0]; # just as a test to see if it works.
> > if ($_[0] = /test/){
> > print "found it";

> This is pretty nasty code man..
> > }
> > }

>
>
> I would rewrite this..
>
> #######################################
> #!/usr/bin/perl -w
>
> my $word = "testword";
> print "WORD: \"$word\"\n";
>
> if($word =~ /test)


Opps...if($word =~ /test/) # should be this

> {
> print "FOUND WORD in \"$word\"\n";
> }
>
> exit;
> #######################################
>
> then once it works in std conext put it into subroutine
>
> #######################################
> #!/usr/bin/perl -w
>
> my $word = "testword";
> print "WORD: \"$word\"\n";
>
> $find = findtest($word);
> if($find eq "yes")
> { print "FOUND WORD in \"$word\"\n"; }
> else
> { print "NO FOUND WORD in \"$word\"\n"; }
>
> exit;
>
> ##### SUBS
>
> sub findtest
> {
> my $word = shift(@_);
> my $found = "no";
> if($word =~ /test)


Opps...if($word =~ /test/) # should be this

> {
> print "FOUND WORD in \"$word\"\n";
> $found = "yes";
> }
> return $found;
> }
>
> #######################################
>
>
>
> >
> > First off, as I understand it, a call to a sub with a value(s)
> > automatically puts it into a temporary array called $_[].
> >
> > And it actually does work. I can print the value of $_[0] which

correctly
> > comes out as "testword". But the match comparison errors out with...
> >
> > Use of uninitialized value in pattern match (m//) at ./test2 line 7.
> >
> > What value? The left side variable is ok. The /test/ is just a string
> > pattern as far as I can see.
> >
> > My Perl book kind of glosses over this part and move on to much more
> > indepth pattern matching. Can anybody clue me in?
> >
> > DD
> >

>
>



 
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
Pattern matching [newbie] vivek_12315 Perl Misc 3 02-13-2013 08:54 PM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Newbie needs help on pattern matching Madhusudan Singh Perl Misc 7 09-03-2004 04:33 PM
Perl Pattern Matching-- Newbie Vandana Perl Misc 4 01-19-2004 11:21 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments