Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: The error says the value is uninitialized, but it works anyway

by dbuckhal (Hermit)
on Aug 18, 2019 at 05:01 UTC ( #11104629=note: print w/replies, xml ) Need Help??


in reply to The error says the value is uninitialized, but it works anyway

Looks like you have been well taken care of regarding your inquiry, but I would like to add a cool use of each I recently discovered. Should fit well with your code assignment. You can use each to collect the indices of an array, or both indices and values. From the docs:
 each HASH
 each ARRAY
            When called on a hash in list context, returns a 2-element list
            consisting of the key and value for the next element of a hash. In
            Perl 5.12 and later only, it will also return the index and value
            for the next element of an array so that you can iterate over it;
            older Perls consider this a syntax error. When called in scalar
            context, returns only the key (not the value) in a hash, or the
            index in an array.
So, using each, your code can go from:
$ perl -e ' my @colors = qw(red green blue yellow pink purple brown); my $count = @colors; my @drop = qw(pink brown); my $num = 0; foreach $num (1..$count){ $num--; if ($colors[$num] eq $drop[0] or $colors[$num] eq $drop[1]){ splice (@colors, $num, 1); } } print "@colors \n"; '
to:
$ perl -e ' my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink brown); while ( my ($num, $val) = each @colors ) { if ($val eq $drop[0] or $val eq $drop[1]) { splice (@colors, $num, 1); } } print "@colors \n"; '
both produce the following output:
__output__ red green blue yellow purple

I hope that was the correct answer! :)

Replies are listed 'Best First'.
Re^2: The error says the value is uninitialized, but it works anyway
by AnomalousMonk (Bishop) on Aug 18, 2019 at 06:34 UTC

    Unfortunately, it's still altering an array (or it might have been a hash) over which you're iterating and so still incorrect:

    c:\@Work\Perl\monks>perl -wMstrict -le "my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink purple); while ( my ($num, $val) = each @colors ) { if ($val eq $drop[0] or $val eq $drop[1]) { splice (@colors, $num, 1); } } print qq{@colors}; " red green blue yellow purple brown

    Update: each sez:

    If you add or delete a hash's elements while iterating over it, the effect on the iterator is unspecified; for example, entries may be skipped or duplicated--so don't do that.
    The same obviously applies to arrays. The each doc goes on to discuss a specific exception involving delete and hashes, but still no joy WRT arrays.


    Give a man a fish:  <%-{-{-{-<

      Ahh, of course... thanks!

      adding "save" array, and "if" to "unless"...

      perl -wMstrict -e ' my @colors = qw(red green blue yellow pink purple brown); my @drop = qw(pink purple); my @save = (); while ( my ($num, $val) = each @colors ) { unless ($val eq $drop[0] or $val eq $drop[1]) { push @save, $colors[$num]; } } print "@save \n"; ' __output__ red green blue yellow brown

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2019-12-09 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?