Front page | perl.beginners |
Postings from February 2002
RE: Creating a string with quotes and delimiters from an Array
Thread Previous
|
Thread Next
From:
Timothy Johnson
Date:
February 27, 2002 15:00
Subject:
RE: Creating a string with quotes and delimiters from an Array
Message ID:
C0FD5BECE2F0C84EAA97D7300A500D5002580FFE@SMILEY
In english, it says:
========================================
$string .=
"append the result of the following expression to $string"
========================================
/D/ ? "'$_'," : "$_,";
"if 'there are no non-digits'(/D/) is true, then return the default
variable ($_) with quotes around it followed by a comma, otherwise return
the default variable followed by a comma without quotes"
========================================
In mine, I used /^\d+$/, which means "if it has only digits from the start
(^) to the finish ($)"
========================================
note: if there is a newline in the string at the end, then you might end up
returning the newline, too, but for the most part this works.
-----Original Message-----
From: Angus Laycock [mailto:angus.laycock@tesco.net]
Sent: Tuesday, February 26, 2002 10:33 AM
To: beginners@perl.org
Subject: Re: Creating a string with quotes and delimiters from an Array
I know I now have the result but how does it do it?
$string .= /\D/ ? "'$_'," : "$_,";
If you have the time.
Thanks again
Gus
----- Original Message -----
From: Tanton Gibbs <thgibbs@deltafarms.com>
To: Angus Laycock <angus.laycock@tesco.net>; <beginners@perl.org>
Sent: Wednesday, February 27, 2002 10:30 PM
Subject: Re: Creating a string with quotes and delimiters from an Array
> Your best bet is to do this with a foreach loop
>
> my $string = '';
> my @array = qw( alf bert charlie 4 )
> foreach( @array ) {
> $string .= /\D/ ? "'$_'," : "$_,";
> }
> ----- Original Message -----
> From: "Angus Laycock" <angus.laycock@tesco.net>
> To: <beginners@perl.org>
> Sent: Tuesday, February 26, 2002 1:07 PM
> Subject: Creating a string with quotes and delimiters from an Array
>
>
> Hi,
>
> 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 >
>
> Thanks
>
>
> Gus
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
--------------------------------------------------------------------------------
This email may contain confidential and privileged
material for the sole use of the intended recipient.
If you are not the intended recipient, please contact
the sender and delete all copies.
Thread Previous
|
Thread Next