how should i fix this??

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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;
    >> '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!!

    ~D
    jend Guest

  2. #2

    Default 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?
    my $this_array =
    "'".join("','", map { s/'/\\'/g; $_ } @thoseLabels)."'";

    --
    Gunnar Hjalmarsson
    Email: [url]http://www.gunnar.cc/cgi-bin/contact.pl[/url]

    Gunnar Hjalmarsson Guest

  3. #3

    Default 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

  4. #4

    Default 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?
    > 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
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #5

    Default Re: how should i fix this??

    On 9 Sep 2003 18:27:45 -0700, jend <dejen321@yahoo.com> 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?
    > 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;




    John J. Trammell Guest

  6. #6

    Default Re: how should i fix this??

    [email]dejen321@yahoo.com[/email] (jend) wrote in message news:<cc028093.0309091727.7f2ddd71@posting.google. com>...
    > I want it to come out like this:
    >
    > >> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
    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.
    Earle Martin Guest

  7. #7

    Default Re: how should i fix this??

    [email]dejen321@yahoo.com[/email] (jend) wrote in message news:<cc028093.0309091727.7f2ddd71@posting.google. com>...
    > However I want it to come out like this:
    >
    > >> 'Tom\'s','John\'s','Jim\'s','mike','blah','bloh'
    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";
    Earle Martin Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139