Ask a Question related to PERL Miscellaneous, Design and Development.
-
jend #1
how should i fix this??
my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
my $this_array = "'".join("','", map { $_ =~ /\'/ ? $_ =~ s/\'/\\'/g
: $_ } @thoseLabels)."'";
print $this_array;
However I want it to come out like this:>> '1','1','1','mike','blah','bloh'
>> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
is it possible to rewrite this one liner to do this?
any tips would be greatly appreciated!!
~D
jend Guest
-
Gunnar Hjalmarsson #2
Re: how should i fix this??
jend wrote:
my $this_array => my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
>
> my $this_array = "'".join("','", map { $_ =~ /\'/ ? $_ =~ s/\'/\\'/g
> : $_ } @thoseLabels)."'";
>
> print $this_array;
>>>>>'1','1','1','mike','blah','bloh'
> However I want it to come out like this:
>>>>>'Tom\'s','John\'s','Jim\'s','mike','blah','bloh '
>
> is it possible to rewrite this one liner to do this?
"'".join("','", map { s/'/\\'/g; $_ } @thoseLabels)."'";
--
Gunnar Hjalmarsson
Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]
Gunnar Hjalmarsson Guest
-
Bob Walton #3
Re: how should i fix this??
jend wrote:
....> my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
>
> my $this_array = "'".join("','", map { $_ =~ /\'/ ? $_ =~ s/\'/\\'/g
> : $_ } @thoseLabels)."'";
>
> print $this_array;
>
>>>>>'1','1','1','mike','blah','bloh'
>>>
> However I want it to come out like this:
>
>>>>>'Tom\'s','John\'s','Jim\'s','mike','blah','bloh '
>>>
>
> is it possible to rewrite this one liner to do this?
> ~D
Try:
my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
my $this_array="'".join("','",map{$_=~/'/?$_=~ s/'/\\'/g
:$_;$_}@thoseLabels)."'";
print $this_array;
Read carefully about what values map() and s///g return. Also, ' is not
a regexp metacharacter, so it is not necessary to quote it in a regexp.
--
Bob Walton
Bob Walton Guest
-
John W. Krahn #4
Re: how should i fix this??
jend wrote:
If you want to escape all non-word characters then this will work:>
> my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
>
> my $this_array = "'".join("','", map { $_ =~ /\'/ ? $_ =~ s/\'/\\'/g
> : $_ } @thoseLabels)."'";
>
> print $this_array;
>>> >> '1','1','1','mike','blah','bloh'
> However I want it to come out like this:
>>> >> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
> is it possible to rewrite this one liner to do this?
> any tips would be greatly appreciated!!
my $this_array = join ',', map "'\Q$_\E'", @thoseLabels;
If you only want to escape the apostrophes then this will work:
my $this_array = join ',', map { s/'/\\'/g; "'$_'" } @thoseLabels;
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
John J. Trammell #5
Re: how should i fix this??
On 9 Sep 2003 18:27:45 -0700, jend <dejen321@yahoo.com> wrote:
[untested]> my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
>
> my $this_array = "'".join("','", map { $_ =~ /\'/ ? $_ =~ s/\'/\\'/g
>: $_ } @thoseLabels)."'";
>
> print $this_array;
>>>>> '1','1','1','mike','blah','bloh'
> However I want it to come out like this:
>>>>> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
>
> is it possible to rewrite this one liner to do this?
> any tips would be greatly appreciated!!
>
my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
my $this_array = join ",", map "'$_'", map { s/'/\\'/; $_ } @thoselabels;
John J. Trammell Guest
-
Earle Martin #6
Re: how should i fix this??
[email]dejen321@yahoo.com[/email] (jend) wrote in message news:<cc028093.0309091727.7f2ddd71@posting.google. com>...
You need two functions: join and s//.> I want it to come out like this:
>> >> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
my $thing = join(" ", @thoseLabels);
$thing =~ s/'/\\'/g;
print "$thing\n";
I would note that what you call $this_array is not an array, it is a string.
Earle Martin Guest
-
Earle Martin #7
Re: how should i fix this??
[email]dejen321@yahoo.com[/email] (jend) wrote in message news:<cc028093.0309091727.7f2ddd71@posting.google. com>...
Of course I misread this the first time. Try again.> However I want it to come out like this:
>> >> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
foreach my $foo (@thoseLabels) {
$foo =~ s/'/\\'/;
$foo = "'$foo'";
}
my $thing = join(',', @thoseLabels);
print "$thing\n";
Earle Martin Guest



Reply With Quote

