Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: splice and indexes

by johngg (Canon)
on Nov 29, 2018 at 00:11 UTC ( [id://1226475]=note: print w/replies, xml ) Need Help??


in reply to splice and indexes

As suggested by Anonymonk, warnings show that using your @indexes unaltered means you are trying to access a element off the end of @arr.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my @arr = qw{ zero one two three four }; my @idx = ( 0 .. $#arr ); my $one = splice @arr, 1, 1; say $one; say join q{ }, @arr; say join q{ }, @idx; say join q{ }, @arr[ @idx ]; pop @idx; say join q{ }, @idx; say join q{ }, @arr[ @idx ];' one zero two three four 0 1 2 3 4 Use of uninitialized value in join or string at -e line 8. zero two three four 0 1 2 3 zero two three four

I hope this is helpful.

Update: Expanded example with more descriptive output.

johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my @arr = qw{ zero one two three four }; say qq{\@arr before splice : @arr}; my @idx = ( 0 .. $#arr ); say qq{Original \@idx : @idx}; my $one = splice @arr, 1, 1; say qq{\$one from splice : $one}; say qq{\@arr after splice : @arr}; say qq{\@arr bad slice : @arr[ @idx ]}; my @newidx = ( 0 .. $#arr ); say qq{New \@newidx : @newidx}; say qq{\@arr good slice : @arr[ @newidx ]};' @arr before splice : zero one two three four Original @idx : 0 1 2 3 4 $one from splice : one @arr after splice : zero two three four Use of uninitialized value in join or string at -e line 9. @arr bad slice : zero two three four New @newidx : 0 1 2 3 @arr good slice : zero two three four

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-18 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found