substitute character in variable

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

  1. #1

    Default substitute character in variable

    Pardon my ignorance but,

    How do you parse a variable to find every occurrence of a string and
    replace the string with another. I tried using ~s/ but it doesn't work.
    Thanks,
    Michael Goodman Guest

  2. Similar Questions and Discussions

    1. Escape character when setting variable
      I'm unsure on how to escape a character when setting a session variable. Here's my problem, I'm getting an outside variable that contains a dash in...
    2. Substitute for mod function
      Hi all! I need to find a substitute for the mod function which can be found in vbscript and so on. Example: for i = 0 to 100 if (i mod...
    3. Using More Than 1 Substitute ?
      I have a mess of text in 1 field. 140$042$024$104 etc ... I want to have 1 calc field that fixes all of it with a key. Substitute (text,...
    4. character limitation of an application variable
      Does anyone know the character limitation of an application variable ?? How much can one hold ? Will it give an error if it gets too big ? etc...
    5. Need substitute for SSI GLOBAL
      Hello, I'm trying to port a website from GoServer to IIS 5.1 and it makes a lot of use of SSI. Under GoServer I could set GLOBAL variables,...
  3. #2

    Default RE: substitute character in variable

    give one example

    -----Original Message-----
    From: Michael Goodman [mailto:mgoodman@BTBOCES.ORG]
    Sent: Friday, September 12, 2003 3:27 PM
    To: [email]beginners@perl.org[/email]
    Subject: substitute character in variable


    Pardon my ignorance but,

    How do you parse a variable to find every occurrence of a string and
    replace the string with another. I tried using ~s/ but it doesn't work.
    Thanks,

    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Marcos Rebelo Guest

  4. #3

    Default Re: substitute character in variable

    Hi Michael

    Michael Goodman wrote:
    >
    > Pardon my ignorance but,
    This is a beginners list :)
    > How do you parse a variable to find every occurrence of a string and
    > replace the string with another. I tried using ~s/ but it doesn't work.
    use strict;
    use warnings;

    my $variable = "A and B and C and D and E";

    $variable =~ s/and/or/g;

    print "$variable\n";

    OUTPUT

    A or B or C or D or E


    HTH,

    Rob


    Rob Dixon Guest

  5. #4

    Default Re: substitute character in variable

    On Friday, September 12, 2003, at 08:27 AM, Michael Goodman wrote:
    > Pardon my ignorance but,
    >
    > How do you parse a variable to find every occurrence of a string and
    > replace the string with another. I tried using ~s/ but it doesn't
    > work.
    For replacing a string, you want:

    $variable =~ s/STRING/REPLACEMENT/g;

    Just remember that's a Regular Expression, so some characters have
    special meaning in there. Good luck.

    James

    James Edward Gray II Guest

  6. #5

    Default Re: substitute character in variable

    "Michael Goodman" <mgoodman@BTBOCES.ORG> writes:
    > Pardon my ignorance but,
    >
    > How do you parse a variable to find every occurrence of a string and
    > replace the string with another. I tried using ~s/ but it doesn't work.
    Michael,

    It's usually better to post a code example so that we can see what's
    going wrong. However, this quick example does what I think you are
    asking for:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $string = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?\n";
    $string =~ s/wo/qu/g;
    print $string;

    returns:
    How much quod quuld a quodchuck chuck if a quodchuck could chuck
    quod?

    Note the 'g' tacked on to the end of the substitution...

    -RN

    --
    Robin Norwood
    Red Hat, Inc.

    "The Sage does nothing, yet nothing remains undone."
    -Lao Tzu, Te Tao Ching
    Robin Norwood 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