how should i fix this??

J

jend

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:


is it possible to rewrite this one liner to do this?
any tips would be greatly appreciated!!

~D
 
G

Gunnar Hjalmarsson

jend said:
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:



is it possible to rewrite this one liner to do this?

my $this_array =
"'".join("','", map { s/'/\\'/g; $_ } @thoseLabels)."'";
 
B

Bob Walton

jend said:
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:




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

John W. Krahn

jend said:
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:


is it possible to rewrite this one liner to do this?
any tips would be greatly appreciated!!

If you want to escape all non-word characters then this will work:

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
 
J

John J. Trammell

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:



is it possible to rewrite this one liner to do this?
any tips would be greatly appreciated!!

[untested]

my @thoseLabels = qw(Tom's John's Jim's mike blah bloh);
my $this_array = join ",", map "'$_'", map { s/'/\\'/; $_ } @thoselabels;
 
E

Earle Martin

I want it to come out like this:

You need two functions: join and s//.

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

Earle Martin

However I want it to come out like this:

Of course I misread this the first time. Try again.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top