Avoiding Perl warning "uninitialized value"

N

neilsolent

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;
}
}
 
A

Alan Curry

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.
 
N

neilsolent

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 - many thanks. You were right - I did not relaise that was how it
worked..
 
J

Justin C

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)/)[/QUOTE]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top