Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Do values from string exist within array?

by Anonymous Monk
on Sep 30, 2013 at 16:08 UTC ( [id://1056382]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array @GrpIDs which looks like this when printed with dumper.
'GrpID' => '123', 'GrpID' => '456'

I have a string containing a csv of grpids: $query = "123,567". I want to compare and match. How can I find out if one or more of the values in $query exist within @GrpIDs.

Replies are listed 'Best First'.
Re: Do values from string exist within array?
by davido (Cardinal) on Sep 30, 2013 at 16:28 UTC

    You have a flat array that looks like this?

    @GrpIDs = ( 'GrpID', '123', 'GrpID', '456' );

    Or is there some other data structure? It doesn't make sense to have 50% of the elements in the @GrpIDs array containing the text "GrpID". And without a better understanding of what your @GrpIDs data structure looks like, it's hard to give a good answer.

    As for your query string, it can be parsed into a list of fields using Text::CSV. At that point, comparison of two arrays (assuming that's what you've got) is a matter of programming.


    Dave

Re: Do values from string exist within array?
by talexb (Chancellor) on Sep 30, 2013 at 16:37 UTC

    Do you actually have a structure that looks like this?

    my %hash = ( 'GrpID' => [ 123, 456 ] );

    That would be a hash with a single key, whose value is an arrayref. You could find out how many values were present by looking at the value of scalar @{$hash{'GrpID'}} -- in this example, the value should be 2, meaning there are multiple values.

    And you'd put values into that hash entry with

    push ( @{$hash{'GrpID'}}, 789 );
    I'd recommend you pop into the Perl debugger and try this stuff out. The debugger's a great place to try out Perl constructs.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Do values from string exist within array?
by Eily (Monsignor) on Sep 30, 2013 at 16:29 UTC

    How do you print your dump? The fat comma (=>) makes it look like a hash (but since it's just a list it could indeed be an array), but you have twice the same key, so it's not possible. And since your variable is @GrpIDs, it has to be an array.

    You should look at grep and Text::CSV for your problem. But I can't help any further unless I have the right data structure.

Re: Do values from string exist within array?
by Anonymous Monk on Sep 30, 2013 at 17:10 UTC
    For less than a few hundred-thousand entries, a simple search ("grep" function) will do as well as anything. If the computer can keep all the data in physical memory at the same time, even a sequential search is fast-enough. Don't solve a problem until you have verified that it actually hurts.
Re: Do values from string exist within array?
by Anonymous Monk on Oct 01, 2013 at 13:15 UTC
    Thanks for the comments so far, my array @GrpIDs now contains:
    warn Dumper @GrpIDs: $VAR1='123'; $VAR2='456'; ...

    String $query contains '123'. I'd like to know how to create a match/test so that I get a positive result for things which are in the array @GrpIDs that have an entry in the string $query.
      use strict; use warnings; use List::MoreUtils qw(any); my @GrpIDs = qw(123 456); my $query = '123,567'; my $QUERY = join '|', split(/,/, $query); if (any {/$QUERY/} @GrpIDs){ print "We have a match\n"; }
      Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-23 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found