Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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'"

Replies are listed 'Best First'.
Re^2: Getting Matching Items From An Array
by Limbic~Region (Chancellor) on Jul 06, 2004 at 12: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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://371991]
[Corion]: I think I'm overdesigning things again. I want to export(later, synchronize) data from Google Keep, by scraping the HTML. And I'm thinking of automating this by having a canary note whose text my program knows and from which it can determine the ...
[Corion]: ... surrounding HTML to scrape all the other notes. Maybe I should better look at dumping all the requests that pass between Google and my "browser" instead.
[choroba]: The older one will even perform twice, once at a retirement home, and then at the music school. It's a day off, but will be pretty busy...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-12-12 08:55 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (327 votes). Check out past polls.