pattern match

Discussion in 'Perl Misc' started by Geezer From The Freezer, Jul 26, 2005.

  1. @a contains:-

    abcdefg1234
    1234 this is a test
    test
    test 1234

    I want to print everything with 1234 so:-



    $list = @a =~ /1234/ ;
    chomp($list);
    print ($list);

    Why doesn't this work? :(

    Be grateful if someone can point me in the right direction.
     
    Geezer From The Freezer, Jul 26, 2005
    #1
    1. Advertising

  2. Geezer From The Freezer wrote:
    >
    > $list = @a =~ /1234/ ;
    > chomp($list);
    > print ($list);
    >
    > Why doesn't this work? :(
    >
    > Be grateful if someone can point me in the right direction.


    It's not clear from your post what you expect this to produce
    I'll assume you're expecting the altered array to be printed.

    However you are assigning an array (@a) to a scalar ($list)

    Doing this results in the $list variable being assigned the number of
    elements in the array , not the contents of the array - i.e. in this
    case 4.
     
    Niall Macpherson, Jul 26, 2005
    #2
    1. Advertising

  3. Geezer From The Freezer wrote:
    > @a contains:-
    >
    > abcdefg1234
    > 1234 this is a test
    > test
    > test 1234
    >
    > I want to print everything with 1234 so:-
    >
    >
    >
    > $list = @a =~ /1234/ ;
    > chomp($list);
    > print ($list);
    >
    > Why doesn't this work? :(


    "it doesn't work" is about the worst possible description of a problem.
    What _does_ it do? Print something you didn't expect? Fail with an error
    message? Hang in a loop? ...?

    Well, at least you sort of told us what you did expect.

    If you would have "use warnings;" then perl would have told you already what
    is probably wrong (see output of warnings enabled).
    Another hint can be found in the man page for m// (perldoc perlop, section
    Regexp Quote-Like Operators):

    If the "/g" option is not used, "m//" in list context returns a
    list consisting of the subexpressions matched by the parentheses
    in the pattern, i.e., ("$1", "$2", "$3"...). [...]

    I don't see any capturing parathesis in your code.

    When there are no parentheses in the pattern, the
    return value is the list "(1)" for success.

    Well, guess that's not what you are looking for, either. Seems like m// is
    the wrong tool for the job.

    > Be grateful if someone can point me in the right direction.


    From your description it seems you are looking for a plain old grep()?

    jue
     
    Jürgen Exner, Jul 26, 2005
    #3
  4. yeah, for list-filtering, grep is best.

    my @test = qw(
    test1234
    2134
    1234
    blah
    test
    );

    print "@test\n";
    my @nums = grep /1234/,@test;
    print "@nums\n";


    Geezer From The Freezer wrote:
    > @a contains:-
    >
    > abcdefg1234
    > 1234 this is a test
    > test
    > test 1234
    >
    > I want to print everything with 1234 so:-
    >
    >
    >
    > $list = @a =~ /1234/ ;
    > chomp($list);
    > print ($list);
    >
    > Why doesn't this work? :(
    >
    > Be grateful if someone can point me in the right direction.
     
    it_says_BALLS_on_your forehead, Jul 26, 2005
    #4
  5. Geezer From The Freezer wrote:
    > @a contains:-
    >
    > abcdefg1234
    > 1234 this is a test
    > test
    > test 1234
    >
    > I want to print everything with 1234 so:-
    >
    > $list = @a =~ /1234/ ;
    > chomp($list);
    > print ($list);
    >
    > Why doesn't this work? :(


    The binding operator (=~) operates on scalars so the array is used in
    scalar context which in this case results in the expression

    '4' =~ /1234/

    and the match operator (m//) is evaluated in scalar context because it
    is being assigned to a scalar which in this case is assigned the value
    '' (false) because /1234/ doesn't match '4'.

    perldoc perlop


    > Be grateful if someone can point me in the right direction.


    perldoc -f grep


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jul 26, 2005
    #5
    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. hiwa
    Replies:
    0
    Views:
    646
  2. Victor
    Replies:
    2
    Views:
    654
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    386
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    498
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    171
Loading...

Share This Page