capser capser - 1 year ago 105
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
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:

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

I want this:

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

Answer Source

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; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download