I'm using the $' operator to append to a string that I find from using a search.

Thats a variable actually.

For example given the string copyright 2003-2004, I want to append an additional year onto the end.

Use the concatenation (thats how comp sci people say "append to the end of a string" :-) operator the dot . so you might say

use strict; use warnings; my $string="copyright 2003-2004"; my $year=2005; $string=$string.",".$year;

But we normally wouldnt say that last line just like that, we would use a shortcut

$string.=", $year";

which does the same thing but is less chars to type ( $x .= $y is the same as $x= $x . $y) and we dont need to actually do concatenation here explicitly but out of lazyness we write it to use interpolation and then perl sort it out for us.

I used s/copyright*/$& $' ,2005/g

No-no-no-no. Thats a bit like using a flame thrower to light a candle. First off the special match variables $` $& and $' are bad to use in general (you can usually rewrite your regex to go without them), but to use them just to do concatenation wouldn't be done except in the wildest obfu/golf mutant code :-) Furthermore that regex says "match 'copyrigh' followed by 0 or more t's and then replace it with itself and everything to its right followed by ',2005'". So effectively we are inserting a copy of everything to after the word 'copyright' with itself follwed by ", 2005". '*' doesnt match any characters like it does on a command line. '*' itself is a qualifier that means "match the thing infront of me 0 or more times." We use the special character . (funny how that keeps coming up eh?) to represent any character (ahem, normally it means "every character but a newline"). So '.*' matches "zero or more of any characters but a newline". Which means that you could say s/(copyright.*)/$1, 2005/; and it would do what you want. But its unlikely anybody would do that. They would be much more likely to do s/(copyright 2003-2004)/copyright 2003-2005/; especially if the relevent string was embedded in a larger one.

I really would recommend some doc time however. Its clear that you need a refresher in the basics.

Good luck.


---
demerphq

<Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

In reply to Re: appending to end of string by demerphq
in thread appending to end of string by jc23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.