This really is two questions. First, you want to know how to make a modification to a string, and second, you want to know how to write to a file. (Modifying a file in place is harder, something that as a beginning Perl programmer you will probably want to leave for later.)

The second question, writing to a file, has already been addressed by the other answers. For what you're doing, you basically will loop through the input file, and write each line, either modified or not, out to the output file. (Then, if you're brave, you can move the modified output overtop of the input, but for now I would probably leave that to do manually after inspecting the output.)

However, nobody seems to have addressed the first question, making the modification to the string in the first place. You can do this with a regular expression substitution...

$_ =~ s/=.*?$/=Whatever/;

Also note that the substitution operator binds to $_ by default, so if you're operating on $_ you may leave off the $_ =~ part. (If you want to operate on the contents of any _other_ variable, though, you'd have to specify it.) Note too that since the pattern that I'm substuting for starts with =, I had to include the = in the substitution, since otherwise it would be removed. It is possible to avoid this (by excluding the = from the pattern (using a lookbehind assertion)), but the way I've written it here is easier to understand and should work just as well. The only caveat is that you do have to leave this inside the if statement that checks for your keyword (Identify in your example), but you had that already. Using the lookbehind would allow you to combine that step with the substitution:

$_ =~ s/(?<=Identity=).*?$/Whatever/;

However, there are limitations to this approach (e.g., you can't straightforwardly allow an optional space before the = sign, as the other version implicitely does), and the only tangible benefit is terseness. So on the whole I would tend to go the other way, putting the substitution inside the if block and including the = in both the pattern and the replacement string.

As for writing to the file, you can just write $_ out to the output file after the close of the if block, so that it's written whether it's been modified or not. That way the whole contents of the input file is copied over to the output file, except that it's modified if any changes are made to $_ in the if block. It's also possible to chain several elsif blocks onto the if block, thereby allowing for several possible changes. More complex variations (e.g., using a hash or array as a list of changes to make) are beyond the scope of this reply.

HTH.HAND.


"In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68

In reply to Re: Modifying entries in a file. by jonadab
in thread Modifying entries in a file. by nisha

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.