Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to insert a new line?

by davido (Cardinal)
on Nov 01, 2014 at 15:26 UTC ( [id://1105792]=note: print w/replies, xml ) Need Help??


in reply to How to insert a new line?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1105792]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found