Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Why "use of uninitialized value" warning comes?

by Corion (Patriarch)
on Nov 01, 2006 at 12:25 UTC ( #581663=note: print w/replies, xml ) Need Help??

in reply to Why "use of uninitialized value" warning comes?

When looking at line 9 in your source code, there is a string eq test. That warning warns you that you are using a value ($need[$incr]) in a string equality test (eq '') and that value is undefined. Maybe you are confused how to iterate over an array. for(;;) is not the way to iterate over an array. And while all elements of an array after its defined length will be equal to the empty string, that will produce the warning, as all accesses to an array outside its defined length return undef. If you are doing array manipulation, you want to read foreach and push.

Your loop can be written even shorter by using grep:

my @array1 = grep { /^sanj/ } @need;

Replies are listed 'Best First'.
Re^2: Why "use of uninitialized value" warning comes?
by suaveant (Parson) on Nov 01, 2006 at 14:05 UTC
    Or even
    for my $need (grep { /^sanj/ } @need) { $array1[$incr1++] = $need; } #although maybe in that case you would just do the more readable for my $need (@need) { if($need =~ /^sanj/) { $array1[$incr1++] = $need; } }
    And to make that warning go away in places you really need the eq there is if(defined $foo && $foo eq 'bar')

                    - Ant
                    - Some of my best work - (1 2 3)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://581663]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-08-15 13:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found