Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

array of hash ref?

by teamassociated (Sexton)
on Nov 12, 2014 at 01:07 UTC ( [id://1106914]=perlquestion: print w/replies, xml ) Need Help??

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

Hi y'all! I have made a lot of progress on my own, but I think I'm stuck. As you can see in the output, vhost0 is the key and the values are the l02s strings likewise for vhost5. When the code gets into the if condition, it only wants to print 1 one of these lines. Why? Please correct any inefficient code too. My goal is to not print any vhost that has values of 'NO VIRTUAL TARGET DEVICE FOUND', hence my grep. Thank you!
use strict; use warnings; my (@vhost,%vhosts,$vh,$vdata,$vadapter,$lsm); open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while ($lsm = (<LSMAP>)) { $lsm =~ s/backing device//i; $lsm =~ s/vtd//i; $lsm =~ s/U9117.*//i; $lsm =~ s/0x0.*//; next if $lsm =~ /^\s*$/; next if $lsm =~ /^--/; next if $lsm =~ /^SVSA|^Physloc.?|^Mirrored.*|status.*|lun.*|vtscs +i\d+/i; ($vh) = map { (split)[0], } $lsm if $lsm =~ /vhost.*/; push( @{$vhosts{$vh}}, $lsm); } use Data::Dumper; #print Dumper \%vhosts; for my $key (keys %vhosts) { $vadapter = $key; #print @{$vhosts{$vadapter}}; #if ( grep /no virtual target device found/i, @{$vhosts{$vadapter} +} ) { # pop @{$vhosts{$vadapter}}; # print @{$vhosts{$vadapter}}; #} if ( !grep /no virtual target device found/i, @{$vhosts{$vadapter} +} ) { # print @{$vhosts{$vadapter}}; print split(/\n|\.\w+/,"lu -map -clustername asccl-eta1 -sp st +p-eta1 -vadapter @{$vhosts{$vadapter}}[0] -lu @{$vhosts{$vadapter}}[1 +]" ),"\n"; } } __OUT__ Notice vhost3 is not in this out. vhost0 l02duapofdev_r1.58ff94782d7c325acc49a8dd9c04bb1e l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 vhost5 l02duapofqa_r1.4746343ea4f12fe1092615c8734d9844 l02duapofqa1_d1.449583501d812f69e6189ea7f4a29e06 # perl viomap_N_bulk.plx # Only printing one of the 2 lines, I should see l02duapofdev_r1 and + l02duapofdev_b1 lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost0 + -lu l02duapofdev_r1 lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost5 + -lu l02duapofqa_r1 __OUT_w_printing_AoH__ # perl viomap_N_bulk.plx vhost7 l02dudbofqa_r1.b524c0f6b39978167278afa7adfc186b l02dudbofqa1_a1.682aa3eab2f95f41f4e61267f1eed32d l02dudbofqa1_a2.0063f2629d0245d7c1733b612dd60ab6 l02dudbofqa1_a3.32d800ee33ed30ef8a3608c6159782ac l02dudbqa1_d1.fe1b01b781bc4b5060ee46cf76280d2d l02dudbofqa1_d2.2f70fff82f86b6a671feac7a4fd87b35 l02dudbofqa1_d3.b409863d75262fc9b79acf517c676e0b l02dudbofqa1_d4.2f6e94f56e6153ade4f92a98c802da5c l02dudbofqa1_d5.a2f5295a1c384b48fd9e5ae5f2a948b2 l02dudbofqa1_d6.42a707c7c349d835a35230606836e1da vhost6 l02dzuapofqa_r1.095f2dc597a4bf3b2813353e51ac5100 l02dzuapofqa_d1.afb45ce1f5d7680be6121fc52621e9da vhost3 NO VIRTUAL TARGET DEVICE FOUND __LSMAP-ALL_OUT__ # /usr/ios/cli/ioscli lsmap -all SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost0 U9117.MMD.218A8B7-V1-C2 0x0000000 +b VTD vtscsi0 Status Available LUN 0x8100000000000000 Backing device l02duapofdev_r1.58ff94782d7c325acc49a8dd9c04bb1e Physloc Mirrored N/A VTD vtscsi1 Status Available LUN 0x8200000000000000 Backing device l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 Physloc Mirrored N/A SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- -------------------------------------------- ------------------ vhost3 U9117.MMD.218A8B7-V1-C14 0x0000000 +0 VTD NO VIRTUAL TARGET DEVICE FOUND SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost5 U9117.MMD.218A8B7-V1-C16 0x0000000 +7 VTD vtscsi22 Status Available LUN 0x8100000000000000 Backing device l02duapofqa_r1.4746343ea4f12fe1092615c8734d9844 Physloc Mirrored N/A VTD vtscsi23 Status Available LUN 0x8200000000000000 Backing device l02duapofqa1_d1.449583501d812f69e6189ea7f4a29e06 Physloc Mirrored N/A

Replies are listed 'Best First'.
Re: array of hash ref?
by GrandFather (Saint) on Nov 12, 2014 at 03:21 UTC

    I'm not altogether sure what you are after, but maybe the following does it?

    use strict; use warnings; my %vhosts; my $host; #open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while (defined(my $lsm = <DATA>)) { if ($lsm =~ /^Backing device\s+(\w+)\.\w+$/) { push @{$vhosts{$host}}, $1; next; } my ($p1, $p2, $p3) = split /\s+/, $lsm; $host = $p1 if defined $p3 && $p1 !~ /^(SVSA|-+)$/; } for my $host (sort keys %vhosts) { print "lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter $hos +t -lu $_\n" for @{$vhosts{$host}}; } __DATA__ SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost0 U9117.MMD.218A8B7-V1-C2 0x0000000 +b VTD vtscsi0 Status Available LUN 0x8100000000000000 Backing device l02duapofdev_r1.58ff94782d7c325acc49a8dd9c04bb1e Physloc Mirrored N/A VTD vtscsi1 Status Available LUN 0x8200000000000000 Backing device l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 Physloc Mirrored N/A SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- -------------------------------------------- ------------------ vhost3 U9117.MMD.218A8B7-V1-C14 0x0000000 +0 VTD NO VIRTUAL TARGET DEVICE FOUND SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost5 U9117.MMD.218A8B7-V1-C16 0x0000000 +7 VTD vtscsi22 Status Available LUN 0x8100000000000000 Backing device l02duapofqa_r1.4746343ea4f12fe1092615c8734d9844 Physloc Mirrored N/A VTD vtscsi23 Status Available LUN 0x8200000000000000 Backing device l02duapofqa1_d1.449583501d812f69e6189ea7f4a29e06 Physloc Mirrored N/A

    Prints:

    lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost0 -lu l02d +uapofdev_r1 lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost0 -lu l02d +uapofdev_b1 lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost5 -lu l02d +uapofqa_r1 lu -map -clustername asccl-eta1 -sp stp-eta1 -vadapter vhost5 -lu l02d +uapofqa1_d1

    It's more about "what you want" rather than "what you don't want".

    Perl is the programming world's equivalent of English
Re: array of hash ref?
by pme (Monsignor) on Nov 12, 2014 at 07:56 UTC
    Hi teamassociated, I hope it helps.

    #!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my %vhosts; my $host; #open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while (defined(my $lsm = <DATA>)) { if ($lsm =~ /^(vhost\d+)/) { $host = $1 } if ($lsm =~ /^Backing device\s+([\w\.]+)/) { $vhosts{$host} = [] unless defined $vhosts{$host}; push $vhosts{$host}, $1; } } print Dumper( \%vhosts ) . "\n"; __DATA__ SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost0 U9117.MMD.218A8B7-V1-C2 0x0000000 +b VTD vtscsi0 Status Available LUN 0x8100000000000000 Backing device l02duapofdev_r1.58ff94782d7c325acc49a8dd9c04bb1e Physloc Mirrored N/A VTD vtscsi1 Status Available LUN 0x8200000000000000 Backing device l02duapofdev_b1.0ee86903d4951ce38a9b691b322ff1d2 Physloc Mirrored N/A SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- -------------------------------------------- ------------------ vhost3 U9117.MMD.218A8B7-V1-C14 0x0000000 +0 VTD NO VIRTUAL TARGET DEVICE FOUND SVSA Physloc Client Pa +rtition ID --------------- -------------------------------------------- --------- +--------- vhost5 U9117.MMD.218A8B7-V1-C16 0x0000000 +7 VTD vtscsi22 Status Available LUN 0x8100000000000000 Backing device l02duapofqa_r1.4746343ea4f12fe1092615c8734d9844 Physloc Mirrored N/A VTD vtscsi23 Status Available LUN 0x8200000000000000 Backing device l02duapofqa1_d1.449583501d812f69e6189ea7f4a29e06 Physloc Mirrored N/A

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found