Mocky123: It's unfortunate that the previous answers have been hasty and glib (and if they haven't, I pre-apologize for misreading their tone). Your question, while showing a lack of effort, is not uninteresting. But when people are handed some data and asked for a solution, they understandably assume that their kindness is being abused through the laziness of the person asking for help. My first reaction was the same, "Show us what you've tried, or go try something and let us know where you got stuck." ...that's really not bad advice. But I do think there's something more interesting still left to discuss, even if the question is low-effort.

There are actually two parts to your problem. We tend to gravitate to what seems like the simplest answer. A simple answer is that you need to read the file line-by-line, printing each line out to a new file. As you arrive at 6, you need to add the text 'horse', and then print that line. Then continue on line-by-line reading and printing to the end of the file. When you're done, the new file can replace the old one.

That's treating the file as just text, which seems simple, though it could be deceptively so.

This file has some sort of structure. Lines 4, 5, and 6, probably correspond to lines 8, 9, and 10. There may also be a correlation with lines 1, 2, and 12; we aren't sure by your explanation of the data. If it turns out that cat => tall, dog => short, and horse => skinny, it's probably better to treat the file as a data format that can be thawed/parsed into a data-structure, and later frozen/encoded into the file format again.

Consider, for example, JSON. Most solutions that deal with JSON just parse it into a structure as it comes in, deal with it in its datastructural format internally, and then reencode it to its JSON representation after manipulations are complete. It's not considered a generally sound practice to directly modify the JSON file without first parsing it on a semantic level.

Your data may fall into that category, and if we knew more about it, we might provide more useful advice.

Decode -> Manipulate Datastructure -> Encode.

So the two parts to your problem are (1) Data representation, and (2) Semantic meaning of the data. The answer that seems the simplest at the outset may not be the simplest after all. Unless this is a one-off, it's usually simpler to separate concerns; code to deal with the freezing/thawing of the data, and code to manipulate the internal representation of the thawed data.


Dave


In reply to Re: How to insert a new line? by davido
in thread How to insert a new line? by Mocky123

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.