Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Getting Matching Items From An Array

by rob_au (Abbot)
on Jul 05, 2004 at 19:58 UTC ( #371991=note: print w/replies, xml ) Need Help??

in reply to Getting Matching Items From An Array

This is an excellent post Limbic~Region++. One note which I would make however is that the exists function can be of use with arrays where testing that an index is within the bounds of the defined array (without autovivifying a non-existent element) - This may be something useful which you can add to this text.


perl -le "print unpack'N', pack'B32', '00000000000000000000001011100111'"

Replies are listed 'Best First'.
Re^2: Getting Matching Items From An Array
by Limbic~Region (Chancellor) on Jul 06, 2004 at 08:58 UTC
    Instead of adding it, I have updated the node to remove exists in that section as it was not necessary. It did not occur to me that novices might see exists as improper with an array and total miss using a string as an index.

    While your point is well taken, I believe "Autovivication Pitfalls and How To Avoid Them" deserves to be a tutorial all to itself.

    Cheers - L~R

Re^2: Getting Matching Items From An Array
by hardburn (Abbot) on Jul 06, 2004 at 10:55 UTC

    exists still autovivifies in some situations:

    use Data::Dumper; my %h = ( foo => 1 ); print "Exists" if exists $h{bar}[2]; print Data::Dumper::Dumper \%h; __END__ $VAR1 = { 'bar' => [], 'foo' => 1 };

    Which probably isn't the right thing to do.

    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      It should only autoviv that array element if the hash key is already defined IMHO...
Re^2: Getting Matching Items From An Array
by dragonchild (Archbishop) on Jul 06, 2004 at 11:21 UTC
    I'm not so sure there's a huge benefit here.
    my @x; $x[9] = 1; print "$#x ", scalar(@x), $/; print exists $x[2] ? 1 : 0, $/; print defined $x[2] ? 1 : 0, $/; + $x[2] = undef; print exists $x[2] ? 1 : 0, $/; print defined $x[2] ? 1 : 0, $/; ---- 9 10 0 0 1 0

    Basically, all you're saying is whether that element was assigned to. That's different from "Is this index within the bounds of the array?", which is tested by if ($i <= $#array).

    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2019-06-24 23:20 GMT
Find Nodes?
    Voting Booth?
    Is there a future for codeless software?

    Results (100 votes). Check out past polls.