Result from a regex substitute

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

  1. #1

    Default Result from a regex substitute

    Hi there all,

    I sure this is really obvious but what's the syntax for this please:

    my $string1 = "mark had a little dog";
    $string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"


    But ................
    my $string1 = "mark had a little dog";
    my $string2 = $string1 =~ s/dog/lamb/g;
    This returns
    $string2 = 1 (presumably a boolean for substitute done)
    whereas I want it to return $string2 = "mary had a little lamb";

    Thanks in advance






    Julia deSilva Guest

  2. Similar Questions and Discussions

    1. Flash as a DRM substitute
      I'd like to be able to stream audio to end users, but do not want those users to be able to play back songs outside of the streaming. If I'm using...
    2. Substitute a color in an EPS
      Hi, I must edit an EPS to change the color of the shapes. Since it's an EPS, I cannot edit each shape separately (moreover, it's a vectorized...
    3. 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...
    4. 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,...
    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: Result from a regex substitute

    Also sprach Julia deSilva:
    > I sure this is really obvious but what's the syntax for this please:
    >
    > my $string1 = "mark had a little dog";
    > $string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"
    >
    >
    > But ................
    > my $string1 = "mark had a little dog";
    > my $string2 = $string1 =~ s/dog/lamb/g;
    > This returns
    > $string2 = 1 (presumably a boolean for substitute done)
    Yes, it returns the number of replacements done. You need to add
    parens:

    (my $string2 = $string1) =~ s/dog/lamb/g;

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval Guest

  4. #3

    Default Re: Result from a regex substitute

    Julia deSilva wrote:
    > Hi there all,
    >
    > I sure this is really obvious but what's the syntax for this please:
    >
    > my $string1 = "mark had a little dog";
    > $string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"
    >
    >
    > But ................
    > my $string1 = "mark had a little dog";
    > my $string2 = $string1 =~ s/dog/lamb/g;
    > This returns
    > $string2 = 1 (presumably a boolean for substitute done)
    > whereas I want it to return $string2 = "mary had a little lamb";
    If you want to keep the $string1 to remain as "mark had a little dog"
    and have $string2 be the changed string do this:

    my string1 = my string2 = "mark had a little dog";
    $string2 =~ s/dog/lamb/g;

    BTW This substitution won't ever give "mary had a little lamb" it will
    give you "mark had a little lamb".

    Chris Guest

  5. #4

    Default Re: Result from a regex substitute

    Chris <no_thanks@bms.umist.ac.uk> wrote:
    > my string1 = my string2 = "mark had a little dog";
    ^^ ^^


    Perl variables require a funny character (sigil) before their names.


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  6. #5

    Default Re: Result from a regex substitute

    On Tue, 29 Jul 2003 12:33:56 +0100
    "Julia deSilva" <jds@trumpetweb.co.uk> wrote:
    >
    > Hi there all,
    >
    > I sure this is really obvious but what's the syntax for this please:
    >
    > my $string1 = "mark had a little dog";
    > $string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"
    >
    >
    > But ................
    > my $string1 = "mark had a little dog";
    > my $string2 = $string1 =~ s/dog/lamb/g;
    > This returns
    > $string2 = 1 (presumably a boolean for substitute done)
    > whereas I want it to return $string2 = "mary had a little lamb";
    >
    > Thanks in advance
    >
    >
    >
    (my $string2 = $string1) =~ s/dog/lamb/g;

    unless you really want to change "mark" to "mary":
    my %lut=qw/mark mary dog lamb/;
    (my $string2 = $string1) =~ s/(mark|dog)/$lut{$1}/g;



    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    drop the .thetenant to get me via mail
    John Strauss Guest

  7. #6

    Default Re: Result from a regex substitute

    Thanks everyone,
    > my $string1 = "mark had a little dog";
    Sorry, I did get my nursery rhyme wrong, and for YOUR information, I live 2
    miles from Kilmersdon, the village where Jack and Jill fell down the hill,
    but that's another story.....



    Julia deSilva 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