Re: printing arrays
by borisz (Canon) on Dec 31, 2008 at 14:35 UTC
|
use List::MoreUtils qw/mesh/;
print join ' ', mesh @ID, @name;
or
# you lose the order and every id must be unique
my %h;
@h{@ID} = @name ;
print join ' ', %h;
or
...
| [reply] [d/l] [select] |
|
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,
rir
Updated: join usage
Updated: efficiency comment deleted. | [reply] [d/l] [select] |
|
Using $, is just another way. But internally $, use join and is __not__ faster or more efficient as the join statement.
| [reply] [d/l] [select] |
|
|
| [reply] |
Re: printing arrays
by Arunbear (Prior) on Dec 31, 2008 at 14:41 UTC
|
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";
}
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
|
..thanks kdj - have a good new year's eve..
| [reply] |
|
..thanks for the input - have a safe new year's eve!
| [reply] |
Re: printing arrays
by johngg (Canon) 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.
Cheers, JohnGG | [reply] [d/l] [select] |
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
| [reply] |
|
...so sorry - my fault! ..have a safe new year's eve!
| [reply] |
Re: printing arrays
by Perlbotics (Archbishop) 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)
| [reply] [d/l] |
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;
| [reply] [d/l] [select] |
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 ! :) | [reply] [d/l] |
Re: printing arrays
by hda (Chaplain) on Jan 01, 2009 at 14:19 UTC
|
Hi!
If you use PDL (Perl Data Language http://pdl.perl.org/), 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! | [reply] [d/l] |
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...)
| [reply] |