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

Hi there Monks!!
I would like to search a hash by using the numeric value only of an array (@array_code), how could a get this done, any ideas and thanks so much for the help!
my @array_code= qw(WE4321 PP7654 8973 7865 TT3422); my %code_numbers = ( "4321" =>"XX", "7654" => "BB", "8973" => "" ); # Using the number part only of the array elements (@array_code), how +to search for a match into the hash %code_numbers and print # results like: WE4321 XX # PP7654 BB # if value is empty like will be for this # 8973 # add "IN",and get it like this: # 8973 IN

Thanks!

Replies are listed 'Best First'.
Re: Search Hash with array element Help!
by ikegami (Patriarch) on Mar 04, 2010 at 20:23 UTC
    - For every array element, - Extract the numeric component of the element. - If the hash contains a key matching the extracted component, - If the hash value is empty, - Print whatever - Else - Print whatever

    Update: I was letting you do the work yourself, but since others have posted code,

    for my $id (@array_code) { my ($num) = $id =~ /(\d+)/ or next; next if !exists($code_numbers{$num}); my $code = $code_numbers{$num} || 'IN'; print("$id $code\n"); }

    Or if %code_numbers can contain a value of '0',

    for my $id (@array_code) { my ($num) = $id =~ /(\d+)/ or next; next if !exists($code_numbers{$num}); my $code = $code_numbers{$num}; $code = 'IN' if !length($code); print("$id $code\n"); }

      I really liked ur concise code. :) I didn't understand how the following statement:

      my ($num) = $id =~ /(\d+)/ or next;

      I understand what the result is but I didn't understand how does that gives you a numeric form and removes the characters. Actually, this is the first time I have seen something like this therefore, can you please forward me some reference material too.

      Thanks.

        The same with parens that illustrate precedence:
        ( my ($num) = ( $id =~ /(\d+)/ ) ) or next;

        The first thing that happens is that $id is matched against /(\d+)/.

        If it matches, a list of all the captured data (that which the content of parens in the pattern matched) is returned by the match operator. The first (and only) element of that list is assigned to $num. The assignment returns 1 (the size of the list returned by the match), so next is skipped.

        If it doesn't matches, an empty list is returned. undef is assigned to $num (which won't be used). The assignment returns zero (the size of the list returned by the match), so next is evaluated and the rest of the loop is skipped.

        All of these operators (match, list assignment and or) are documented in perlop.

Re: Search Hash with array element Help!
by jrsimmon (Hermit) on Mar 04, 2010 at 20:26 UTC
    my @array_code= qw(WE4321 PP7654 8973 7865 TT3422); my %code_numbers = ( "4321" =>"XX", "7654" => "BB", "8973" => "" ); foreach my $value (@array_code){ my $nums = $value; $nums =~ s/\D//g; if(exists($code_numbers{$nums})){ if($code_numbers{$nums} eq ''){ print "$value\tIN\n"; }else{ print "$value\t$code_numbers{$nums}\n"; } } }
Re: Search Hash with array element Help!
by toolic (Bishop) on Mar 04, 2010 at 20:27 UTC
    use strict; use warnings; my @array_code= qw(WE4321 PP7654 8973 7865 TT3422); my %code_numbers = ( "4321" =>"XX", "7654" => "BB", "8973" => "" ); for (@array_code) { my $c = $_; $c =~ s/\D//g; if (exists $code_numbers{$c}) { print "$_ ", ($code_numbers{$c} || 'IN'), "\n"; } }
      Could this be done in reverse?
Re: Search Hash with array element Help!
by Corion (Patriarch) on Mar 04, 2010 at 20:23 UTC

    What have you tried so far, and how did it fail for you?

Re: Search Hash with array element Help!
by snape (Pilgrim) on Mar 04, 2010 at 21:02 UTC
    #!/usr/bin/perl use warnings; use strict; my @array_code= qw(WE4321 PP7654 8973 7865 TT3422); my $val; my %code_numbers = ( "4321" =>"XX", "7654" => "BB", "8973" => "" ); my $key; my $len = $#array_code; for(my $i= 0; $i <= $len; $i++){ $key = $array_code[$i]; $key =~ s/\D//g; if (exists $code_numbers{$key}){ my $val = $code_numbers{$key}; if($val eq ''){ print "Key:$key \t Value:$val \n"; $code_numbers{$key} = "IN"; } print "Key:$key \t Value:$code_numbers{$key} \n"; } } exit;