Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

printing unequal sized lists side by side

by nad04299 (Initiate)
on May 28, 2011 at 02:34 UTC ( [id://907099]=perlquestion: print w/replies, xml ) Need Help??

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

Title says it all, really; looking for a suggestion on a good way of printing 2 unequally-sized lists side by side. I'm not a great one for text formatting to begin with; hoping someone has a cooler way of doing so than the kludginess I could come up with. Thanks.

Replies are listed 'Best First'.
Re: printing unequal sized lists side by side
by toolic (Bishop) on May 28, 2011 at 03:47 UTC
    Array::Each
    use warnings; use strict; use Array::Each; my @nums = 1 .. 5; my @letts = 'a' .. 'g'; my $obj = Array::Each->new(set=>[\@nums, \@letts], bound=>0, undef=>' +'); while (my ($n, $l) = $obj->each) { print "$n, $l\n"; } __END__ 1, a 2, b 3, c 4, d 5, e , f , g
Re: printing unequal sized lists side by side
by wind (Priest) on May 28, 2011 at 05:46 UTC

    Using a basic loop and List::Util. Most of the additional code is there for simple formatting.

    use List::Util qw(max); use warnings; use strict; my @array1 = (1..5, 10); my @array2 = ('a'..'h'); my $length = max map {length} @array1; for my $i (0..max($#array1, $#array2)) { printf "% ${length}s %s\n", $array1[$i] // '', $array2[$i] // ''; } __END__ 1 a 2 b 3 c 4 d 5 e 10 f g h
Re: printing unequal sized lists side by side
by CountZero (Bishop) on May 28, 2011 at 08:37 UTC
    Or using List::MoreUtils:
    use Modern::Perl; use List::MoreUtils qw/each_array/; my @first = 1 .. 10; my @second = 'a' .. 'h'; my $ea = each_array(@first, @second); while ( my ($first, $second) = $ea->() ) { say "$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

Re: printing unequal sized lists side by side
by nvivek (Vicar) on May 28, 2011 at 05:14 UTC
    You can get it by following way
    use strict; use warnings; my @array1=(1 .. 20); my @array2=('a' .. 'e' ); my $max_array_length=(scalar(@array1) > scalar(@array2)) ? scalar(@arr +ay1) : scalar(@array2); for(my $index=0; $index<$max_array_length; $index++) { print $array1[$index] if $array1[$index]; print ","; print $array2[$index] if $array2[$index]; print "\n"; }
      + + (esp for the economy of solving OP's problem withOUT adding module overhead) ...but with a minor nitpick.

      Consider your output, if @array1 and @array2 are swapped, making @array2 the longer of the two.

      But one can make that output slightly more elegant (a matter of taste, of course; YMMV) by using the ternary again in print statements which make visual allowance for non-existent indices:

      for(my $index=0; $index<$max_array_length; $index++) { print $array1[$index] ? $array1[$index] : ' '; print ", "; print $array2[$index] ? $array2[$index] : '-'; print "\n"; }
        Thanks. This is close to what I had been doing and it helps greatly to see it in a post ;)
        I do tend to prefer to avoid adding modules if only for peace of mind that the customer can push the scripts around systems w/o fear of it breaking.

        I appreciate the help :)

        Instead of the ternary operator, might I recommend the "defined-or" test instead. It's more succinct and it doesn't fail on a value of '0'.

        print $array1[$index] // ' ';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found