I'm totally unclear on what you want to have happen. You want to force the user to type 'grape' three times before the program removes grape once? Seems to be an unlikely need.
You want to look at line one of the file, take user input, and if line one matches the user input remove it, then move on to the next line, and so on?
- File: "Apple\nBananna\nGrape". Script looks at "Apple". User inputs "Grape". Grape doesn't match Apple. Move on.
- File: "Apple\nBananna\nGrape". Script looks at Bananna. Uer inputs Grape. Grape doesn't match Bananna. Move on.
- File: "Apple\nBananna\nGrape". Script looks at 'Grape'. User inputs 'Grape'. Grape matches Grape. Remove Grape from file.
- File: "Apple\nBananna\n". Script finds no more lines in the file; close the file and exit.
Is that what you're looking for? As I mentioned in a previous post, a single flat newline delimited file where each line can be of arbitrary length is not well suited to this task. The best I can suggest is to open a second file for output. At step one, write Apple to the second file. At step two, write Bananna to the second file. At step three, do not write Grape to the second file. At step four, close the output file, and rename it to take the place of the original input file.
In this way, you're essentially re-writing the original file by making a copy that has whatever modifications you need, and then replacing the original with the copy.
A newline-delimited file where lines may be of arbitrary length is not suited for in-place editing. You almost always have to write out a copy.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.