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;
}
}
[/code]
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..
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
>> if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)
> I don't know why the ? is inside the () in the above regex.
Then read the "Extended Patterns" section in:
perldoc perlre
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
I did, and it's not pretty! I read:
The stability of these extensions varies widely. Some have been part
of the core language for many years. Others are experimental and may
change without warning or be completely removed. Check the documenta‐
tion on an individual feature to verify its current status.
.... and decided I'd leave this alone for a while. There's some very
complex pattern matching available in there. Maybe I'll get my head
around it sometime, maybe I'll find another way to do it!
Thanks for the pointer.
Justin.
--
Justin C, by the sea.