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:
Michael Fowler
Date:
February 27, 2002 15:15
Subject:
Re: Creating a string with quotes and delimiters from an Array
Message ID:
20020227231428.GA15155@shoebox.net
On Tue, Feb 26, 2002 at 06:07:33PM -0000, Angus Laycock wrote:
> 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.

$str = join(',', map { "'$_'" } @array);

 
> 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 >

$str = join(',', map { /\D/ ? "'$_'" : $_ } @array);

"value is numeric" is a little vague; I went with a regex that's simple, but
won't catch quite a few variations.  See perldoc -q 'is a number' for
alternatives.


By the way, what is this for?  It looks suspiciously like you're using it
for database inserts or something along those lines; there are better
alternatives if that is the case.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

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