$_ in condition

Discussion in 'Perl' started by jason@cyberpine.com, Aug 11, 2004.

  1. Guest

    Pardon the perl noobie post, this one is silly one I'm sure, but
    nonetheless, I'm stuck on it. why doesn't $_ substitute with the data
    when the program is passed the string as such. The eventual objective
    of the program being to test the string against a list of regexes.

    perl test.pl 'xxxxxxxxxxx test1 xxxxxxxxxxxxxxx'

    note commented line works!

    Along the same lines, how could I pass regex lines in that same data
    to condition that a string fed to the program does in fact match.

    Many thanks.

    #!/usr/bin/perl -w
    use strict;
    my $request = shift @ARGV;
    print $request;
    my $p='n';
    while(<DATA>) {
    print $_;
    if ($request =~ $_)
    # if ($request =~ 'test1')

    {
    $p='y';
    }
    }
    if ($p =~ 'y')
    {
    print "\n Passed! \n";
    }
    else
    {
    print "\n Failed! \n";

    }
    __DATA__
    test1
    test2
    test3
     
    , Aug 11, 2004
    #1
    1. Advertising

  2. wrote:
    > why doesn't $_ substitute with the data
    > when the program is passed the string as such.


    Substitute? Suppose you mean match.

    The reason is that $_ includes a trailing "\n".

    <snip>

    > while(<DATA>) {


    chomp; # this should fix the problem

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 11, 2004
    #2
    1. Advertising

  3. Guest

    > chomp; # this should fix the problem

    This did not appear to fix the issue, and actually the code only
    appears to "pass" when I send it the last entry in the data.


    $ perl m2.pl 'test2'
    test2
    Failed!
    $ perl m2.pl 'test3'
    test3
    Passed!

    The code I'm testing with:

    #!/usr/bin/perl -w
    use strict;
    my $request = shift @ARGV;
    print $request;
    my $p='n';
    while(<DATA>) {
    chomp;
    if ($request =~ $_)
    {
    $p='y';
    }
    }
    if ($p =~ 'y')
    {
    print "\n Passed! \n";
    }
    else
    {
    print "\n Failed! \n";

    }
    __DATA__
    test1
    test2
    test3


    Thanks for any help or information.
     
    , Aug 12, 2004
    #3
  4. Guest

    > chomp; # this should fix the problem


    Did not appear to fix the problem. And pardon the repost, but to add
    further to my confusion on while loops, I made a few changes to the
    this little program and added displays. Why doesnt my condition trip
    and/or stay set for $p in the below code. Here are the three test
    attempts. Also, note how $p displays on top of the first display
    until the the last record - why is that????


    $ perl m2.pl test2

    p=xuest====test2 current====test1

    p=xuest====test2 current====test2

    request====test2 current====test3 p=x
    $ perl m2.pl test3

    p=xuest====test3 current====test1

    p=xuest====test3 current====test2

    request====test3 current====test3 p=y

    Passed!
    $ perl m2.pl test2

    p=xuest====test2 current====test1

    p=xuest====test2 current====test2

    request====test2 current====test3 p=x
    $


    #!/usr/bin/perl -w
    use strict;
    my $request = shift @ARGV;
    my $p='x';
    while(<DATA>)
    {
    chomp;
    if ($request =~ $_){$p='y';}
    print "\n";
    print ' request====';
    print $request;
    print ' current====';
    print $_;
    print ' p=';
    print $p;
    print "\n";
    }
    if ($p =~ 'y') {print "\n Passed! \n";}

    __DATA__
    test1
    test2
    test3


    Thanks for any help or information.
     
    , Aug 12, 2004
    #4
  5. Joe Smith Guest

    wrote:

    >> chomp; # this should fix the problem

    >
    > This did not appear to fix the issue, and actually the code only
    > appears to "pass" when I send it the last entry in the data.


    Here's your program with better indenting, more perl idioms, and
    using // with =~.

    unix% cat temp.pl
    #!/usr/bin/perl -w
    use strict;
    my $request = shift @ARGV;
    my $p;
    while(<DATA>) {
    print "Checking '$request' for $_";
    chomp;
    $p = $. if $request =~ /$_/;
    }
    if ($p) {
    print "'$request' matched line $p\n";
    } else {
    print "'$request' Failed!\n";
    }
    __DATA__
    test1
    test2
    test3
    unix% ./temp.pl test2
    Checking 'test2' for test1
    Checking 'test2' for test2
    Checking 'test2' for test3
    'test2' matched line 2
    unix% ./temp.pl ::test3::
    Checking '::test3::' for test1
    Checking '::test3::' for test2
    Checking '::test3::' for test3
    '::test3::' matched line 3
    unix% ./temp.pl test4
    Checking 'test4' for test1
    Checking 'test4' for test2
    Checking 'test4' for test3
    'test4' Failed!
    unix% perl -v
    This is perl, v5.8.3 built for sun4-solaris
     
    Joe Smith, Aug 12, 2004
    #5
  6. wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> chomp; # this should fix the problem

    >
    > This did not appear to fix the issue, and actually the code only
    > appears to "pass" when I send it the last entry in the data.


    It now works as intended for me.

    <code snipped>

    > __DATA__
    > test1
    > test2
    > test3


    Did you possibly have trailing spaces (besides the newlines) after
    "test1" and "test2"?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 12, 2004
    #6
  7. Joe Smith Guest

    wrote:

    > Also, note how $p displays on top of the first display
    > until the the last record - why is that????


    That's what happens when you move a file from Windows to Unix
    and forget to use ASCII mode. It looks lik you've got
    __DATA__
    test1\r\n
    test2\r\n
    test3\n


    > while(<DATA>)
    > {
    > chomp;


    while(<DATA>) {
    chomp;
    print "Unexpected carriage-return detected: '$_'\n" if /\r/;

    I expect that it will print
    'nexpected carriage-return detected: 'test1
    with the closing single quote coming after "test1\r".

    Better to strip leading and trailing whitespace.

    while(<DATA>){
    s/^\s*//;
    s/\s*$//; # No need for chomp after doing this
    $p = 'y' if $request =~ /\Q$_\E/;
    }

    -Joe
     
    Joe Smith, Aug 12, 2004
    #7
  8. Jim Gibson Guest

    In article <>,
    <> wrote:

    > > chomp; # this should fix the problem

    >
    >
    > Did not appear to fix the problem. And pardon the repost, but to add
    > further to my confusion on while loops, I made a few changes to the
    > this little program and added displays. Why doesnt my condition trip
    > and/or stay set for $p in the below code.


    As you have already been told, you need to put slashes (/$_/) around
    your regular expressions. People are not likely to help you if you
    ignore their advice.

    > Here are the three test
    > attempts. Also, note how $p displays on top of the first display
    > until the the last record - why is that????


    That will happen when you have a carriage return ("\r") in your string
    without a line feed. Perhaps you are reading a file with DOS line
    endings in a Unix environment that expects only line feeds.

    And once again, this newsgroup is defunct. Try comp.lang.perl.misc in
    the future.
     
    Jim Gibson, Aug 12, 2004
    #8
    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. Mohammed  A khader

    one-hot encoding and fale-safe condition.

    Mohammed A khader, Jan 24, 2005, in forum: VHDL
    Replies:
    12
    Views:
    3,305
    Mike Treseler
    Feb 1, 2005
  2. Daniel

    while condition

    Daniel, May 20, 2005, in forum: VHDL
    Replies:
    7
    Views:
    3,532
    Ralf Hildebrandt
    May 23, 2005
  3. Replies:
    0
    Views:
    431
  4. -
    Replies:
    12
    Views:
    697
    Remon van Vliet
    Jun 15, 2005
  5. Bill W.
    Replies:
    13
    Views:
    295
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page