in reply to Edit lines from a file and replace multiple lines.
As you have seen, given scarce or absent effort will not produce code. Brother holli has shown this monastery principle, de facto.
Formatting is important: when you compose a node her, in the bottom of the screen you can find useful links. Put your code and data input and output expected inside <code> code tags </code>.
As this is your first post a bit of mercy can be spent.. ;)
While learning Perl a good principle is: are you able to do this without a computer? If yes write down the solution in basic english, then try to translate it using the syntax you are able to produce.
Let's try:
Open the file named file.txt. Abort if the file is unreadable. Read the file line per line. If solar wind is found annotate it's line number as solar_linenum Print line just read anyway. Read and print two lines more. If the third line has country then replace it with place and print the line but also set solar_linenum to 0. The same for the other requirement.
Good starting points are: perlintro and the open and perlopentut then foreach ( update ..oops see here as wisely stated below AnomalousMonk) also perlvar can be handy (hint: search for $. that holds current line num while reading files.. ;).
Never forget to begin your program with:
use strict; use warnings; # while learning also the following can be handy; uncomment as needed: # use diagnostics; # lower case: is a pragma not a module
HtH*
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Edit lines from a file and replace multiple lines. -- basic english approach
by AnomalousMonk (Archbishop) on Mar 26, 2019 at 18:51 UTC | |
|
Re^2: Edit lines from a file and replace multiple lines. -- basic english approach
by Endurance (Novice) on Mar 29, 2019 at 06:42 UTC | |
by Discipulus (Canon) on Mar 29, 2019 at 08:40 UTC |