Re: TIMTOWTDI - printing an array
by morgon (Priest) on Jun 20, 2012 at 17:21 UTC
|
map {print} @arr;
| [reply] [d/l] |
|
|
grep { print } @array;
Or even...
use List::MoreUtils qw<first>;
first { not print } @array;
I can imagine many newbies scratching their heads if they saw that one! But I told it to "not print" the array! ;-)
Update: of course, there's the venerable...
print shift @array while @array;
... which prints, but also dismantles an array.
my @r;@r = reverse @array and do { print pop @r while @r };
# Now I'm just being silly!
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
| [reply] [d/l] [select] |
|
|
knoppix@Microknoppix:~$ perl -E '
> @arr = qw{ 11 22 33 44 };
> sub { say shift while @_ }->( @arr );
> say qq{@arr};'
11
22
33
44
11 22 33 44
knoppix@Microknoppix:~$
| [reply] [d/l] [select] |
Re: TIMTOWTDI - printing an array
by AnomalousMonk (Archbishop) on Jun 20, 2012 at 18:33 UTC
|
As long as you're permuting over the for and foreach synonyms, why not include
foreach (my $x = 11; $x < 33; $x += 11) { print $x }
And for that matter, how about array references:
my @array = (11, 22, 33, 44);
my $array_ref = \@array;
print for @array;
print for @$array_ref;
etcetera.
| [reply] [d/l] [select] |
Re: TIMTOWTDI - printing an array
by johngg (Canon) on Jun 20, 2012 at 19:33 UTC
|
knoppix@Microknoppix:~$ perl -E '
> @arr = qw{ 11 22 33 44 };
> sub { say shift while @_ }->( @arr );'
11
22
33
44
knoppix@Microknoppix:~$
| [reply] [d/l] |
|
|
sub { print @_ }->($_) for @arr;
| [reply] [d/l] |
|
|
| [reply] |
|
|
use strict;
use warnings;
use Benchmark qw{ cmpthese };
my @arr = ( 1 .. 1e6 );
my $rcOneElement = sub
{
my $aref = shift;
sub { my $var = shift }->( $_ ) for @$aref;
};
my $rcAllElements = sub
{
my $aref = shift;
sub { my $var = shift while @_ }->( @$aref );
};
cmpthese (
-5,
{
OneElement => sub { $rcOneElement->( \ @arr ) },
AllElements => sub { $rcAllElements->( \ @arr ) },
}
);
The output.
Rate OneElement AllElements
OneElement 2.59/s -- -58%
AllElements 6.16/s 138% --
However, I am good at cocking up benchmarks so I could be deluding myself :-/
| [reply] [d/l] [select] |
Re: TIMTOWTDI - printing an array
by frozenwithjoy (Priest) on Jun 20, 2012 at 17:28 UTC
|
| [reply] |
Re: TIMTOWTDI - printing an array
by stevieb (Canon) on Jun 20, 2012 at 17:41 UTC
|
$p=japh;push@a,w();$s=j4;sub n{"8fbac6c6e252"};unshift@a,
"b4d6c7ea52a7";$k=crypt($s,$p);$o="aeafa7cfdbd58c";@h=
map{sprintf"%x",ord$_}split//,$k;push@a,$o;$a[3]=pop@a;
$a[2]=n();sub w{"bcb3d8dec8dd"}$x.=$_ for@a;@b=($x=~m/..?/g);
push@z,@h until @z>@b;for(@b){push@japh,hex($_)-hex($z
[$n]);$n++;}say map{chr$_}@japh;
;) | [reply] [d/l] |
Re: TIMTOWTDI - printing an array
by choroba (Cardinal) on Jun 20, 2012 at 20:46 UTC
|
my @arr = (11, 22, 33, 44);
open my $FH, '<', \("@arr");
print while <$FH>;
| [reply] [d/l] |
Re: TIMTOWTDI - printing an array
by stevieb (Canon) on Jun 20, 2012 at 23:22 UTC
|
In more recent versions of Perl, you can use each():
my @arr = qw(a b c);
while ( my ( $i, $e ) = each @arr ){
say "iter $i = $e";
}
| [reply] [d/l] [select] |
Re: TIMTOWTDI - printing an array
by Tux (Canon) on Jun 21, 2012 at 06:57 UTC
|
{ local $" = " "; print "@arr'; }
print "@{[@arr]}";
print "@{[map{"$_"}@arr]}";
print $arr[-$_-1] for reverse 0..$#arr;
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] |
Re: TIMTOWTDI - printing an array
by tobyink (Canon) on Jun 21, 2012 at 09:17 UTC
|
my @array = qw(foo bar baz);
system echo => $_ for @array;
And if you think the above is insane...
@array = qw(foo bar baz);
use File::Temp;
foreach my $string (@array)
{
my $tmpfile = File::Temp->new;
printf $tmpfile <<'HERE' => quotemeta($string);
{
package Local::App::PrintString;
use 5.010;
sub run {
say "%s";
}
}
Local::App::PrintString->run(@ARGV)
unless scalar caller;
HERE
system $^X, $tmpfile->filename;
}
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
| [reply] [d/l] [select] |