Avoiding Perl warning "uninitialized value" 
Author Message
 Avoiding Perl warning "uninitialized value"

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;

$df[0] = "udev 125320 152 125168 1% /dev";


{
        if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)
        {
                $fs{$3} = 1;
        }

Quote:
}

[/code]


Mon, 14 May 2012 10:18:57 GMT  
 Avoiding Perl warning "uninitialized value"

Quote:

>Hi


>{
>        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



Mon, 14 May 2012 10:44:51 GMT  
 Avoiding Perl warning "uninitialized value"

Quote:
> 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..


Mon, 14 May 2012 10:54:18 GMT  
 Avoiding Perl warning "uninitialized value"

Quote:
> 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;

> $df[0] = "udev 125320 152 125168 1% /dev";


> {
>         if (/\d+\s+\d+\s+(\d+)\s+(\d+)\%\s+(?!\/cdrom)/)

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



Mon, 14 May 2012 16:07:26 GMT  
 Avoiding Perl warning "uninitialized value"

Quote:


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



Mon, 14 May 2012 17:50:29 GMT  
 Avoiding Perl warning "uninitialized value"

Quote:



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



Wed, 16 May 2012 00:19:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. avoiding "Use of uninitialized value..." warning

2. "Use of uninitialized value" warnings

3. uninitialized value warning for "truth condition"

4. "Use of uninitialized value"

5. Closing pipes results in "uninitialized value"

6. Solution for "Use of uninitialized value"?

7. better way to avoid "uninitialized value in join" ?

8. avoid chaos -or- "protected" writing

9. Using "fork" to avoid crashing script

10. How to prevent the warning "Use of uninitialized value at..."

11. removing "Use of uninitialized value..." warning from list assignment

12. perl "-w" warning for popup_menu()

 

 
Powered by phpBB® Forum Software