Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Getting Matching Items From An Array

by rob_au (Abbot)
on Jul 05, 2004 at 23: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'"


Comment on Re: Getting Matching Items From An Array
Download Code
Re^2: Getting Matching Items From An Array
by Limbic~Region (Chancellor) on Jul 06, 2004 at 12:58 UTC
    rob_au,
    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 14: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 15: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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (12)
As of 2014-12-18 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (48 votes), past polls