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

Hi Monks!

I have this sample code out of what I am trying to do that can explain if it is possible to be done.
After find the values I need from a XML file, I will have to arrays "@account" and "@names". After that I have to print them as you can see on the second "foreach" I just think that it would be better using a hash, can anyone suggest if that would be better and may be how?
#sample code ... foreach my $acc ($xml->findnodes('/account')) { push @account, $acc->find('numbers'); push @names, $acc->find('names'); } my $c=-1; foreach (@acount) { print "Account: <b>$_</b> - Name: $names[$c]"; } ...

Thanks for the Help!

Replies are listed 'Best First'.
Re: Arrays to Hash Issue
by kennethk (Abbot) on Sep 10, 2010 at 18:15 UTC
    Having synchronized lists is usually an easy way to introduce bugs, but is sometimes the best answer. Do the order of the accounts matter? Can you have repeat names or repeat account numbers? If the answer to both is no, a simple way to implement what I assume you mean to accomplish (your $c = -1 means you output the same name over and over) using a hash would be:

    #sample code ... my %name; foreach my $acc ($xml->findnodes('/account')) { $name{$acc->find('numbers')} = $acc->find('names'); } foreach (keys %name) { print "Account: <b>$_</b> - Name: $name{$_}"; } ...

      One change I'd make to that, is to use while each instead of for if there is any chance that this list can grow to any substantial size.

      while( my( $account, $name ) = each %names ) { print "Account: $account Name: $name\n" }

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Arrays to Hash Issue
by CountZero (Bishop) on Sep 10, 2010 at 18:50 UTC
    The ever useful List::MoreUtils module to the rescue:
    use strict; use warnings; use 5.012; use List::MoreUtils qw/each_array/; my @first = qw/1 2 3 4 5 6/; my @second = qw/a b c d e f/; my $ea = each_array(@first, @second); while ( my ($f, $s) = $ea->() ) { say "First: $f -> second: $s" }
    Output:
    First: 1 -> second: a First: 2 -> second: b First: 3 -> second: c First: 4 -> second: d First: 5 -> second: e First: 6 -> second: f
    Or even shorter (but less flexible):
    use strict; use 5.012; use List::MoreUtils qw/pairwise/; my @first = qw/1 2 3 4 5 6/; my @second = qw/a b c d e f/; pairwise { say "First: $a -> second: $b" } @first, @second;

    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