theleftright has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks! I've googled this but it's difficult finding anything.
@array = qw(one two, three four five, six);
how would i go about printing out this array so that it includes the comma? example output:
one two, three four five, six
it's important that I use qw. Thanks
Re: comma within array
by choroba (Cardinal) on Jan 05, 2016 at 20:58 UTC
|
Just use double quotes. Array elements are separated by spaces in double quotes by default (see $" in perlvar).
#!/usr/bin/perl
use warnings;
use strict;
use Test::More tests => 1;
my @array = do {
no warnings 'qw';
qw(one two, three four five, six);
};
is("@array", 'one two, three four five, six', 'same');
qw// warns about commas, so I turned off that particular warning category.
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] [select] |
|
Hi, didnt think it would matter, but that array I showed was a sample array.. here is part of the real code:
@arrayCode = qw( $cn=join(",",@names); print@names; );
$ac = join("",@arrayCode);
eval($ac);
this seems very strange to you, why the hell would i need to do this?! but unfortunately I do :D eval is unable to execute this code because of the comma :/ | [reply] [d/l] |
|
You haven't shown the relevant part of the code. I can run it without problems, with your join and with my double quotes:
#!/usr/bin/perl
use warnings;
use strict;
my @names = qw( John Jane Jake );
my @arrayCode = do {
no warnings 'qw';
qw( $cn=join(",",@names); print@names; )
};
for my $ac ( "@arrayCode",
join q(), @arrayCode
) {
my $cn;
eval "$ac; 1" or die $@;
print $cn, "\n";
}
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord
}map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
| [reply] [d/l] [select] |
|
| [reply] |
Re: comma within array
by NetWallah (Canon) on Jan 05, 2016 at 20:59 UTC
|
Have you tried printing it ?
It works fine for me:
perl -e "@array = qw(one two, three four five, six); print qq|@array\n
+|"
one two, three four five, six
"I can cast out either one of your demons, but not both of them." -- the XORcist
| [reply] [d/l] |
Re: comma within array
by dbuckhal (Chaplain) on Jan 06, 2016 at 19:20 UTC
|
choroba covers the details, but here is my odd variation:
perl -le '@names = qw( abe bill chuck );
@arrayCode = qw| $cn=join(",",@names); print@names; print$cn
+ |;
eval ($_) for @arrayCode
'
__output__
abebillchuck
abe,bill,chuck
Never tried eval in this manner. Now I have, :) | [reply] [d/l] |
|
|