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

Re: grepping

by dws (Chancellor)
on Mar 25, 2001 at 01:02 UTC ( #66899=note: print w/replies, xml ) Need Help??


in reply to grepping

{ my %data; @data{@data} = 1; # turn @data into a lookup hash foreach ( @data2 ) { print "This $_ was not found.\n" if not exists $data{$_} } }

Update: An aside with MeowChow has convinced me that     @data{@data} = undef;  # turn @data into a lookup hash is less misleading. As with the "= 1" form above, only the value for the first key is set, and all remaining values are set to undef. Since we're just using the hash as a way to trade time for space in detecting the presense of keys, values don't matter. But the originally was slightly misleading nonetheless.

Replies are listed 'Best First'.
Re: Re: grepping
by MeowChow (Vicar) on Mar 25, 2001 at 01:10 UTC
    Replacing the hash slice assignment with the following:
    undef @data{@data};
    is considerably faster, though somewhat obscure. Also, your slice assignment doesn't do quite what you mean it to do (it sets only the first key's value to 1, and the rest to undef).
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Re: grepping
by Anonymous Monk on Mar 25, 2001 at 11:15 UTC
    Okay, question...@data has three variables $id, $name, $ref and @data2 has 5 variables $id, $name, $ref, $type, $owner.

    { my %data; undef @data{@data}; foreach ( @data2 ) { print "This $_ was not found.\n" if not exists $data{$_} } }

    This code is matching all 5 variables in @data2 to all 3 variables in @data, thus returning every row in @data2. For a test I took out all but 1 variable from each row in each array and it returned the results I needed. How can I use this code to handle multiple variables or values? And what if I wanted to lookup $name instead of $id.

    Thanks!
      I think you misundertand the relationship between variables and arrays. Arrays hold values; they do not hold variables.

      Consider:

      my $id = 47; my @data = ( $id );
      @data now holds the value 47, and knows nothing whatsoever about where that value came from.

      The script you've asked for (and gotten) mere tells which values appear in one array and not the other.

        At 1:30am they seem the same to me, but I do know the difference. I will be more clear in my future attempts to get help. Nice example, but it still doesn't answer my last question. And in my original post, I specifically asked which ids are in @data2 and not in @data.

        I just need a little more help in understanding how can I do lookups on any value I want instead of every value or just the first value. Please don't think I'm being scarcastic, I'm very frustrated in trying to learn about hashes and arrays. :) Smile on those who read the docs, Perl Cookbook, The Complete Reference to Perl, and even Perl DBI. Yes, I own all three and I'm trying very hard to learn this...phew. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2021-01-27 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?