Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Avoiding Perl warning "uninitialized value"

0 views
Skip to first unread message

neilsolent

unread,
Nov 26, 2009, 4:18:57 AM11/26/09
to
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;
}
}
[/code]

Alan Curry

unread,
Nov 26, 2009, 4:44:51 AM11/26/09
to
In article <5e70e393-3623-49ab...@g26g2000yqe.googlegroups.com>,
neilsolent <n...@solenttechnology.co.uk> 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

neilsolent

unread,
Nov 26, 2009, 4:54:18 AM11/26/09
to
> 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..

Justin C

unread,
Nov 26, 2009, 10:07:26 AM11/26/09
to

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

Tad McClellan

unread,
Nov 26, 2009, 11:50:29 AM11/26/09
to
Justin C <justi...@purestblue.com> wrote:
> On 2009-11-26, neilsolent <n...@solenttechnology.co.uk> wrote:

>> 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/"

Justin C

unread,
Nov 27, 2009, 6:19:20 PM11/27/09
to
In article <slrnhgtc8v...@tadbox.sbcglobal.net>, Tad McClellan wrote:
> Justin C <justi...@purestblue.com> wrote:
>> On 2009-11-26, neilsolent <n...@solenttechnology.co.uk> wrote:
>
>>> 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

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.

0 new messages