A newbie question on pattern matching

Discussion in 'Perl' started by DelphiDude, Jul 22, 2003.

  1. DelphiDude

    DelphiDude Guest

    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
    DelphiDude, Jul 22, 2003
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Jul 22, 2003
    #2
    1. Advertising

  3. DelphiDude

    Joe Guest

    "DelphiDude" <> wrote in message
    news:p...
    > 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
    >
    Joe, Jul 26, 2003
    #3
  4. DelphiDude

    Joe Guest

    "Joe" <> wrote in message
    news:MQuUa.983210$...
    >
    > "DelphiDude" <> wrote in message
    > news:p...
    > > 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
    > >

    >
    >
    Joe, Jul 26, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    233
    Marc Bissonnette
    Jan 13, 2004
  2. Vandana

    Perl Pattern Matching-- Newbie

    Vandana, Jan 19, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    102
    Tad McClellan
    Jan 19, 2004
  3. Madhusudan Singh

    Newbie needs help on pattern matching

    Madhusudan Singh, Sep 2, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    124
    William Park
    Sep 3, 2004
  4. Bobby Chamness
    Replies:
    2
    Views:
    226
    Xicheng Jia
    May 3, 2007
  5. vivek_12315

    Pattern matching [newbie]

    vivek_12315, Feb 13, 2013, in forum: Perl Misc
    Replies:
    3
    Views:
    183
    Jürgen Exner
    Feb 13, 2013
Loading...

Share This Page