lost array element in 5.8

Discussion in 'Perl' started by Farhad, Sep 19, 2003.

  1. Farhad

    Farhad Guest

    Hi,

    in the following example there is a output difference in version 5.00 and
    5.8 of Perl. In case of a missmatch in element 2, the whole element gets
    lost instead of an "undef" value. Does anybody know why in version 5.8 we
    lost the element and is that an error in Perl interpreter or does it work as
    designed.

    Thanks
    Farhad

    ----------------------------------

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

    my $var="Test";
    my @list=('a', # Element 1
    $var=~m/\n/, # Element 2
    $var=~m/es/ # Element 3
    );

    print scalar(@list)." (should be 3)\n";

    push @list, {
    'd' => $var=~m/\n/, # Element 4
    };


    print scalar(@list)." (should be 4)\n"
     
    Farhad, Sep 19, 2003
    #1
    1. Advertising

  2. Farhad <> wrote:
    > my $var="Test";
    > my @list=('a', # Element 1
    > $var=~m/\n/, # Element 2
    > $var=~m/es/ # Element 3
    > );
    >
    > print scalar(@list)." (should be 3)\n";


    No, it should be 2. m// in list context returns an empty list when
    the match fails.

    --
    Steve
     
    Steve Grazzini, Sep 19, 2003
    #2
    1. Advertising

  3. Farhad

    Jim Guest

    Hi Farhad,

    i believe 5.8 works as designed.

    try running your script in the debugger:
    perl -d yourscriptname.pl

    step through the commands using s, the first time, then return.

    in the debugger, try:
    p $var =~ /\n/
    and
    p $var =~ /es/

    also, play with:
    x @list

    i do not see why $var would match a newline.

    hope this helps, good luck.

    Jim

    "Farhad" <> wrote in message news:<bkev06$>...
    > Hi,
    >
    > in the following example there is a output difference in version 5.00 and
    > 5.8 of Perl. In case of a missmatch in element 2, the whole element gets
    > lost instead of an "undef" value. Does anybody know why in version 5.8 we
    > lost the element and is that an error in Perl interpreter or does it work as
    > designed.
    >
    > Thanks
    > Farhad
    >
    > ----------------------------------
    >
    > #!/usr/local/bin/perl -w
    >
    > my $var="Test";
    > my @list=('a', # Element 1
    > $var=~m/\n/, # Element 2
    > $var=~m/es/ # Element 3
    > );
    >
    > print scalar(@list)." (should be 3)\n";
    >
    > push @list, {
    > 'd' => $var=~m/\n/, # Element 4
    > };
    >
    >
    > print scalar(@list)." (should be 4)\n"
     
    Jim, Sep 19, 2003
    #3
    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. TS
    Replies:
    5
    Views:
    3,708
    Steven Cheng[MSFT]
    Aug 16, 2007
  2. HANM
    Replies:
    2
    Views:
    734
    Joseph Kesselman
    Jan 29, 2008
  3. Szabolcs Borsanyi
    Replies:
    6
    Views:
    671
    Ben Bacarisse
    May 23, 2008
  4. Al Cholic
    Replies:
    18
    Views:
    282
    Al Cholic
    Jul 28, 2007
  5. Replies:
    2
    Views:
    108
Loading...

Share This Page