capser capser - 1 month ago 7
Perl Question

Perl printing out ascii test with chr()

On this simple program, how can I put a comma between each entity, but leave it off and the end ?

$ cat array_maker.pl
#!/usr/bin/perl
use strict ;
use warnings ;

my @total = @ARGV ;
my $d_quote = chr(34);
my $comma = chr(44);


foreach my $total(@total) {
print " $d_quote$total$d_quote$comma" ;
}


I get this:

$ ./array_maker.pl one two three four
"one", "two", "three", "four",


I want this:

"one", "two", "three", "four" #no comma at end of string.

Answer

The interative way is:

for (my $i=0;$i<scalar(@total);$i++)
{
    print $d_quote . $total[$i] . $d_quote;
    if ($i != (scalar(@total) - 1)) { print $comma; }
}
Comments