Avoiding Perl warning "uninitialized value"

Discussion in 'Perl Misc' started by neilsolent, Nov 26, 2009.

  1. neilsolent

    neilsolent Guest

    Hi

    I get a warning message "Use of uninitialized value $3 in hash element
    at test.pl line 13." when I run the code below (using Perl 5.10). How
    I can fix this in a neat way ?
    Thanks for any help !


    Code:
    !/usr/bin/perl -w
    
    use strict;
    
    my %fs;
    my @df;
    $df[0] = "udev 125320 152 125168 1% /dev";
    
    foreach (@df)
    {
    if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)
    {
    $fs{$3} = 1;
    }
    }
    
     
    neilsolent, Nov 26, 2009
    #1
    1. Advertising

  2. neilsolent

    Alan Curry Guest

    In article <>,
    neilsolent <> wrote:
    >Hi
    >
    >foreach (@df)
    >{
    > if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)
    > {
    > $fs{$3} = 1;
    > }
    >}


    You expected (?!\/cdrom) to act as a third set of capturing parentheses,
    setting $3 to everything after the %\s+ as well as causing the match to fail
    if what comes next is "/cdrom"? It doesn't. Look-aheads, like other fun
    things starting with "(?", are not capturing groups. Those are non-capturing
    parentheses. Add another (.*) to the end of the regexp.

    --
    Alan Curry
     
    Alan Curry, Nov 26, 2009
    #2
    1. Advertising

  3. neilsolent

    neilsolent Guest

    > You expected (?!\/cdrom) to act as a third set of capturing parentheses,
    > setting $3 to everything after the %\s+ as well as causing the match to fail
    > if what comes next is "/cdrom"? It doesn't. Look-aheads, like other fun
    > things starting with "(?", are not capturing groups. Those are non-capturing
    > parentheses. Add another (.*) to the end of the regexp.
    >
    > --
    > Alan Curry


    Alan - many thanks. You were right - I did not relaise that was how it
    worked..
     
    neilsolent, Nov 26, 2009
    #3
  4. neilsolent

    Justin C Guest

    On 2009-11-26, neilsolent <> wrote:
    > Hi
    >
    > I get a warning message "Use of uninitialized value $3 in hash element
    > at test.pl line 13." when I run the code below (using Perl 5.10). How
    > I can fix this in a neat way ?
    > Thanks for any help !
    >
    >
    >
    Code:
    > !/usr/bin/perl -w
    >
    > use strict;
    >
    > my %fs;
    > my @df;
    > $df[0] = "udev 125320 152 125168 1% /dev";
    >
    > foreach (@df)
    > {
    >         if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)[/color]
    
    I don't know why the ? is inside the () in the above regex. If you shift
    it one place left, outside the () then the code does not error. Though I
    don't know what difference that'll make to your regex.
    
    Justin.
    
    --
    Justin Catterall                               www.masonsmusic.co.uk
    Director                                       T: +44 (0)1424 427562
    Masons Music Ltd                               F: +44 (0)1424 434362
    For full company details see our web site
     
    Justin C, Nov 26, 2009
    #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. Liang
    Replies:
    2
    Views:
    763
    Liang
    Aug 11, 2004
  2. BradK
    Replies:
    0
    Views:
    3,055
    BradK
    Apr 10, 2007
  3. Nirvana
    Replies:
    1
    Views:
    738
    Michele Dondi
    Nov 27, 2004
  4. Jon
    Replies:
    4
    Views:
    154
    Paul Lalli
    Sep 19, 2005
  5. Replies:
    4
    Views:
    167
    Anno Siegel
    Sep 20, 2005
Loading...

Share This Page