Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

printing arrays

by Spooky (Beadle)
on Dec 31, 2008 at 14:16 UTC ( #733471=perlquestion: print w/replies, xml ) Need Help??
Spooky has asked for the wisdom of the Perl Monks concerning the following question:

I have two arrays (@ID @name) that I wish to print out on a single line e.g., 123 Smith 456 Doe 789 Allen etc. (as you can see, there's a number followed by a nsame). I know in FORTRAN (yes, FORTRAN) you could use an implied loop but I'm sure how to do this in Perl. Any suggestions?

Replies are listed 'Best First'.
Re: printing arrays
by borisz (Canon) on Dec 31, 2008 at 14:35 UTC
    There are several ways:
    use List::MoreUtils qw/mesh/; print join ' ', mesh @ID, @name;
    # you lose the order and every id must be unique my %h; @h{@ID} = @name ; print join ' ', %h;
    or ...
      Once you have the list that you want to print; you don't need to use a join join, instead you can just print it with $, set appropriately. $, is also known as $OFS and $OUTPUT_FIELD_SEPARATOR; use perldoc perlvar. This can be much more efficient even with small lists.
      my @arr = ( 1, "Ali", 2, "Bobbi", 3, "Charli" ); local $, = " "; print @arr, $/;
      Be well,

      Updated: join usage

      Updated: efficiency comment deleted.

        Using $, is just another way. But internally $, use join and is __not__ faster or more efficient as the join statement.
      ..thanks so much!
Re: printing arrays
by Arunbear (Parson) on Dec 31, 2008 at 14:41 UTC

    Assuming there are as many ids as names:

    use strict; use warnings; my @ids = (123, 456, 789); my @names = qw(Smith Doe Allen); foreach my $i (0 .. $#ids) { print "$ids[$i] $names[$i]\n"; }

      You actually wouldn't want the \n on the end of

           print "$ids[$i] $names[$i]\n";

      That would only print one id/name combination per line, and (s)he wants the entire contents of both arrays on one line. Replace the \n with a space and it should work.

        ..thanks kdj - have a good new year's eve..
      ..thanks for the input - have a safe new year's eve!
Re: printing arrays
by johngg (Abbot) on Dec 31, 2008 at 16:15 UTC

    TIMTOWTDI again! You can use the default behaviour that separates array or list elements with a space in double-quoted strings. This construct, @{ [ ... ] }, allows you to interpolate bits of code inside double-quoted strings.

    $ perl -e ' @ids = qw{ 123 456 789 }; @names = qw{ ann joe flo }; print qq{@{ [ map qq{$ids[ $_ ] $names[ $_ ]}, 0 .. $#ids ] }\n};' 123 ann 456 joe 789 flo $

    I hope this is of interest.



Re: printing arrays
by swampyankee (Parson) on Dec 31, 2008 at 14:45 UTC

    Please, it's not FORTRAN any longer; it's Fortran.

    Fortran has better i/o and string handling than a lot of people seem willing to accept. However, even I, who remains a Fortran afficionado and partisan, will admit that Perl's are, on the whole, better. It's C that's made a mess its string handling routines (strcat, anyone?).

    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc sorry - my fault! ..have a safe new year's eve!
Re: printing arrays
by Perlbotics (Canon) on Dec 31, 2008 at 14:57 UTC

    TIMTOWTDI: Limits the output to the shortest sequence but comes at the cost of an implicite extra-array...

    use strict; use warnings; my @ID = qw(123 456 789 666); my @name = qw(Smith Doe Allen); my $line = join(' ', map { $ID[$_], $name[$_] } 0..($#ID < $#name ? $#ID : $#name) ); print "($line)\n"; #output: (123 Smith 456 Doe 789 Allen)
Re: printing arrays
by kyle (Abbot) on Dec 31, 2008 at 17:06 UTC

    If you don't mind destroying the arrays, you can do it this way.

    printf "%s %s\n", shift( @ID ), shift( @name ) while @ID && @name;

    My first thought, however, was the List::MoreUtils solution that borisz gave.

    Update with a slightly silly way:

    my @nyuck = ( @ID, reverse @name ); printf "%s %s\n", shift( @nyuck ), pop( @nyuck ) while @nyuck;
Re: printing arrays
by spx2 (Deacon) on Dec 31, 2008 at 18:14 UTC
    You could use map and an additional counter like this
    @name = ("one","two","three"); @ID = (1,2,3); $i=0; print map{ ($_,$ID[$i++]); } @name;
    or maybe you can have a look at how interleave two arrays.
    of course someone has thought of a more general map operator , it's called mapcar :) (I found about it in the link above),it seems very handy

    happy new year ! :)
Re: printing arrays
by apl (Monsignor) on Dec 31, 2008 at 15:14 UTC
    I know in FORTRAN (yes, FORTRAN)
    -4, -10, -77 or -9X? (Fortran-4 was one of my first two languages...)
Re: printing arrays
by hda (Hermit) on Jan 01, 2009 at 14:19 UTC

    If you use PDL (Perl Data Language, you can collapse dimensions of piddles using the function "clump". This will allow you to do what you want in amazingly few steps:
    $a = pdl @your_array; $b = clump($a,2); # collapses the first two dimensions print $b;

    Hope this helps!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://733471]
Approved by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-01-21 05:09 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (182 votes). Check out past polls.