Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

how to print two array with new line at end of each second array element, using a single print statement?

by gourish (Initiate)
on Apr 21, 2012 at 06:01 UTC ( #966310=perlquestion: print w/ replies, xml ) Need Help??
gourish has asked for the wisdom of the Perl Monks concerning the following question:

hi, I am trying to print two array with a newline at the end of the second array element. The sample code is as below
my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); print join("\n",values @nums), join("\n", values @dat), "\n";

The result what I get it a follows
1
2
4
5
8
93
9
14
10
12
8
is there any way to get a one line print statement so that I get the output as
1 3
2 9
4 14
5 10
8 12
9 8

Thanks, Gourish

Comment on how to print two array with new line at end of each second array element, using a single print statement?
Download Code
Replies are listed 'Best First'.
Re: how to print two array with new line at end of each second array element, using a single print statement?
by eyepopslikeamosquito (Canon) on Apr 21, 2012 at 06:32 UTC

    Here's a simple way:

    my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); for my $i ( 0 .. $#nums ) { print "$nums[$i] $dat[$i]\n"; }
    Here's an alternative way using the List::MoreUtils CPAN module:
    use List::MoreUtils qw(zip natatime); my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); my $it = natatime( 2, zip(@nums, @dat) ); while ( my @vals = $it->() ) { print "@vals\n"; }
    Update: Simpler solution using the pairwise function:
    use List::MoreUtils qw(pairwise); my @nums = (1, 2, 4, 5, 8, 9); my @dat = (3, 9, 14, 10, 12, 8); print pairwise { "$a $b\n" } @nums, @dat;

      I was just about to follow up with that pairwise option. Glad I saw your update before I did. But I still wanted to mention that I find it necessary to write it something like this:

      print pairwise{ no warnings 'once'; "$a $b\n" } @nums, @dat;

      ...lest I get a pair of warnings that $a and $b are used only once.

      List::MoreUtils provides several ways to do it. This isn't quite as elegant, but still seemed neat to me.

      my $ea = each_array( @nums, @dat ); while( my( $n, $d ) = $ea->() ) { print "$n $d\n"; }

      Update: I've submitted a patch and a corresponding test targeting the 'used only once' warnings.


      Dave

Re: how to print two array with new line at end of each second array element, using a single print statement?
by davido (Archbishop) on Apr 21, 2012 at 06:31 UTC

    print "$nums[$_] $dat[$_]\n" for 0 .. $#nums;

    Dave

Re: how to print two array with new line at end of each second array element, using a single print statement?
by trizen (Hermit) on Apr 21, 2012 at 13:28 UTC
    is there any way to get a one line print statement?

    Yes:
    my $str = q{}; { my $i = 0; $str .= join(q{ }, $nums[$i] // last, $dat[$i++] // last) . "\n" w +hile 1; } print $str;
Re: how to print two array with new line at end of each second array element, using a single print statement?
by BillKSmith (Deacon) on Apr 21, 2012 at 17:47 UTC
    Refer to the example in the documentation for the Array::Each module.
Re: how to print two array with new line at end of each second array element, using a single print statement?
by 2teez (Priest) on Apr 22, 2012 at 06:57 UTC

    A little addition, we can still use 'our' basic hash: like this

    #!/usr/bin/perl use warnings; use strict; my @nums=(1,2,4,5,8,9); my @dats=(3,9,14,10,12,8); my %ha; @ha{@nums}=@dats; print $_,' ',$ha{$_},$/ for sort keys %ha;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966310]
Approved by Happy-the-monk
Front-paged by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-07-08 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls