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

How do I determine for a certain key I have certain values in the anonymous array? I was to determine if vhost5 contains l02duapofqa and not something else. Or I want to determine is l02duapofqa is owned by vhost5. Data structure is:
__DATA__ 'vhost5' => [ 'l02duapofqa_r1', 'l02duapofqa1_d1' ] __CODE__ use strict; use warnings; open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while ($lsm = (<LSMAP>)) { if ($lsm =~ /^Backing device\s+(\w+)\.\w+$/i) { push @{$vhosts{$host}}, $1; next; } my ($p1, $p2, $p3) = split /\s+/, $lsm; $host = $p1 if defined $p3 && $p1 =~ /^(vhost.*)$/i; } $a="vhost11"; $b="l02duapofqa2_d1"; #use Data::Dumper; print Dumper \%vhosts; OUTER: for my $vhname (sort keys %vhosts) { for my $luname ( @{$vhosts{$a}} ) { grep /$b.*/, @{$vhosts{$luname}}; print "\n$vhname -> $luname\n"; if ( !grep /$a.*/, @{$vhosts{$b}} ) { print "\nincorrect vhost\n"; die; } else { print @{$vhosts{$a}},"\ncorrect\n"; last OUTER; } } }
thank you!

Replies are listed 'Best First'.
Re: HoA, grep or exists?
by ikegami (Patriarch) on Nov 20, 2014 at 20:26 UTC

    You seem to have forgotten to specify your data structure. I'm guessing it's

    my %vhosts = ( vhost5 => [ 'l02duapofqa_r1', 'l02duapofqa1_d1', ], );

    Only contains l02duapofqa:

    if (my $vhost5 = $vhosts{vhost5}) { if (@$vhost5 == 1 && $vhost5->[0] eq 'l02duapofqa') { ... } }

    Contains l02duapofqa:

    if (my $vhost5 = $vhosts{vhost5}) { if (grep { $_ eq 'l02duapofqa' } @$vhost5) { ... } }
      date structure is:
      # perl foo $VAR1 = { 'vhost7' => [ 'l02dudbofqa_r1', 'l02dudbofqa1_a1', 'l02dudbofqa1_a2', 'l02dudbofqa1_a3', 'l02dudbqa1_d1', 'l02dudbofqa1_d2', 'l02dudbofqa1_d3', 'l02dudbofqa1_d4', 'l02dudbofqa1_d5', 'l02dudbofqa1_d6' ], 'vhost6' => [ 'l02dzuapofqa_r1', 'l02dzuapofqa_d1' ],
        Here is my entrtie test script:
        # cat foo open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while ($lsm = (<LSMAP>)) { if ($lsm =~ /^Backing device\s+(\w+)\.\w+$/i) { push @{$vhosts{$host}}, $1; next; } my ($p1, $p2, $p3) = split /\s+/, $lsm; $host = $p1 if defined $p3 && $p1 =~ /^(vhost.*)$/i; } $a="vhost11"; $b="l02duapofqa2_d1"; use Data::Dumper; print Dumper \%vhosts; print "\n end data-dumper \n"; OUTER: for my $vhname (sort keys %vhosts) { for my $luname ( @{$vhosts{$vhname}} ) { if ( $a = $vhosts{$a}) { if (grep { $_ =~ /l02duapofqa.*/ } @$vhosts) { print "yes $_\n"; last OUTER; } } } } # perl foo $VAR1 = { 'vhost7' => [ 'l02dudbofqa_r1', 'l02dudbofqa1_a1', 'l02dudbofqa1_a2', 'l02dudbofqa1_a3', 'l02dudbqa1_d1', 'l02dudbofqa1_d2', 'l02dudbofqa1_d3', 'l02dudbofqa1_d4', 'l02dudbofqa1_d5', 'l02dudbofqa1_d6' ], 'vhost6' => [ 'l02dzuapofqa_r1', 'l02dzuapofqa_d1' ], 'vhost2' => [ 'l02dudbofcnv_b2', 'l02dudbofcnv_b1' ], 'vhost11' => [ 'l02duapofqa2_r1', 'l02duapofqa2_d1' ], 'vhost1' => [ 'l02dudbofcnv_b3', 'l02dudbofcnv_b4' ], 'vhost4' => [ 'l02dudbofdev_r1', 'l02dudbofdev_d1', 'l02dudbofdev_d2', 'l02dudbofdev_d3', 'l02dudbofdev_d4', 'l02dudbofdev_d5', 'l02dudbofdev_d6', 'l02dudbofdev_d7', 'l02dudbofdev_d8', 'l02dudbofdev_d9', 'l02dudbofdev_10' ], 'vhost8' => [ 'l02dudbofqa2_a1', 'l02dudbofqa2_r1', 'l02dudbofqa2_a2', 'l02dudbofqa2_a3', 'l02dudbofqa2_d1', 'l02dudbofqa2_d2', 'l02dudbofqa2_d3', 'l02dudbofqa2_d4', 'l02dudbofqa2_d5', 'l02dudbofqa2_d6' ], 'vhost0' => [ 'l02duapofdev_r1', 'l02duapofdev_b1' ], 'vhost5' => [ 'l02duapofqa_r1', 'l02duapofqa1_d1' ] }; end data-dumper # pwd
Re: HoA, grep or exists?
by RonW (Parson) on Nov 20, 2014 at 21:13 UTC

    exists is intended for testing for the presence of a key in a hash. While it can also test for the presence of an array element at a given index, the use of exists on arrays is deprecated.</c>

    Therefore, grep is the appropriate way to test the values of an array.

      Solved it, using:
      OUTER: foreach my $vhname (sort keys %vhosts) { for ($i = 0; $i <= $#{$vhosts{$vhname};}; ++$i) { if ($b eq $vhosts{$vhname}[$i]) { print "found '${b}'\n"; last OUTER; } } } # perl foo found 'l02duapofqa2_d1'
Re: HoA, grep or exists?
by CountZero (Bishop) on Nov 21, 2014 at 07:04 UTC
    Or put all your data in a database (SQLite springs to mind) and query your database with SQL queries.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics