Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Concordance Printing of Two Arrays

by Anonymous Monk
on Oct 17, 2005 at 03:11 UTC ( #500644=perlquestion: print w/replies, xml ) Need Help??

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


Please be patient with me I am new in Perl and programming.
How I can modify my code below:
my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); # these two arrays always come in same size foreach my $num (@num){ foreach my $alp(@alp){ print "$num-$alp\n"; } }
To simply print just:
1-A 2-B 3-C 4-D 5-E
So I want them to each element from each array printed in concordance respective to their position. If possible I want to keep it as 'foreach' structure, so it allows me to apply it to more complex array structure (array of array).

Thanks so much.

Replies are listed 'Best First'.
Re: Concordance Printing of Two Arrays
by pg (Canon) on Oct 17, 2005 at 03:18 UTC
    my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); #One way for my $index (0 .. $#num) { print "$num[$index]-$alp[$index]\n"; } #Or print "$num[$_]-$alp[$_]\n" for (0 .. $#num);
Re: Concordance Printing of Two Arrays
by sk (Curate) on Oct 17, 2005 at 03:20 UTC
    In your code you have a nested foreach structure. You will loop through num of elements in first array times the number of elements in the second array. This means you will be printing more than the output you want. For example you will call print 25 times instead of 5 times. Surely you can work with the structure you have by putting flags or using internal indices but it just gets messy and the problem you are trying to solve clearly does not require a nested loop. If you really like the foreach structure you can create an AoA (shown in my code below).

    You said you are new so let me explain some things that are not immediately obvious -

    $/ - think of this as your new line character (line separator character). see perlvar for more details.

    [value1,value2] - creates a reference to the list  (value1,value2). Note square brackets

    $_ - for/foreach loops populate this variable when we don't specify the looping variable explicitly.

    update: missed this one-- @$_[0] and @$_[1] - In the AoA case each element is a "reference" to a list. So you have treat them as arrays when you want individual elements of each ref. $_ in the AoA contains a list now. The @ before $_ helps in dereferencing the list for us.

    #!/usr/bin/perl -w use strict; my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); for (0..$#num) { print $num[$_], "-", $alp[$_],$/; } ### OR CREATE AOA my @AoA = (); for (0..$#num) { push @AoA, [$num[$_], $alp[$_]]; } print $/; foreach (@AoA) { print @$_[0], "-", @$_[1],$/; } __END__ 1-A 2-B 3-C 4-D 5-E 1-A 2-B 3-C 4-D 5-E
Re: Concordance Printing of Two Arrays
by japhy (Canon) on Oct 17, 2005 at 03:19 UTC
    The simplest way is to loop over the indices of the array(s):
    for my $i (0 .. $#numbers) { do_something_with($numbers[$i], $letters[$i]); }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Concordance Printing of Two Arrays
by gopalr (Priest) on Oct 17, 2005 at 08:54 UTC

    Hi Eddie,

    Use List::MoreUtils Module to do this.

    use List::MoreUtils qw(pairwise); use Data::Dumper; my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); @result=pairwise{($a.'-'.$b)}@num, @alp; print Dumper(\@result);


    $VAR1 = [ '1-A', '2-B', '3-C', '4-D', '5-E' ];


Re: Concordance Printing of Two Arrays
by ioannis (Abbot) on Oct 17, 2005 at 05:07 UTC
    Just making sure you know about Data::Dumper . Then you could explore contents of data structures very easily, like this:
    use Data::Dumper; print Dumper \@alp;
Re: Concordance Printing of Two Arrays
by NateTut (Deacon) on Oct 17, 2005 at 18:55 UTC
    Depending on what else you want to do with these values you might want to put them in a hash:
    use strict; use warnings; my @num = qw (1 2 3 4 5); my @alp = qw (A B C D E); my %numalp; # # Build the hash... # for my $index (0 .. $#num) { $numalp{$num[$index]} = $alp[$index]; } # # Print it # foreach (sort(keys(%numalp))) { print("numalp\[$_\]\[$numalp{$_}\]\n"); }
    This way they remain associated if you need them again...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2023-02-09 10:08 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.