develooper Front page | perl.beginners | Postings from February 2002

Re: Creating a string with quotes and delimiters from an Array

Thread Previous | Thread Next
From:
John W. Krahn
Date:
February 27, 2002 16:03
Subject:
Re: Creating a string with quotes and delimiters from an Array
Message ID:
3C7D73F2.78D37D09@acm.org
Angus Laycock wrote:
> 
> Hi,

Hello,

> I have an array ----> @array = qw(alf bert charlie 4)
> 
> and I want a string that contains this value <
> 'alf','bert','charlie','4' > with the single quotes and commas.
> 
> I know I can do this $string = join ( ',' , @array);
> which gives me this ----> < one|two|three|four> but how
> do I get the single quotes around each word.
> 
> Also, how can I check if one of the values is numeric so
> I dont put quotes around it? So the result is like this
> ---> <  'alf','bert','charlie',4 >


my @array = qw(alf bert charlie 4 -3 0 6.43 -9.234);
my $string;

{ local $^W;  # turn off warnings
$string = join ',', map { $_ eq $_ + 0 ? $_ : "'$_'" } @array;
}

print "$string\n";



John
-- 
use Perl;
program
fulfillment

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About