Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comma within array

by theleftright (Novice)
on Jan 05, 2016 at 20:54 UTC ( [id://1152021]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
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,
      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 :/
        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,

        The eval works fine for me.

        What is the point of this code??

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

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, :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1152021]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found