Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File printing

Status
Not open for further replies.

NesPes

Programmer
Dec 15, 2007
17
US
I have a very basic question on deleting/printing from/to a file.

My input file is something like this:

A1:This is the first line
A2:Welcome to the first line
A3:haaaaaaaaaaaaaaaaaaaaa

A1:This is the first line
A2:Welcome to the second line
A3:heeeeeeeeeeeeeeeeeeeee

I want to first search for A2 and then look for "first" in the string. If i find it, i then want to remove the first set of A1,A2 and A3 lines from this file. Can somebody help me with this?
 
A stab..
Code:
use Tie::File;
tie @array, 'Tie::File', 'test.txt' or die "can't open test.txt: $!\n";;
#If the second line starts with A2 and contains first
if ($array[1] =~ /^A2/ && $array[1] =~ /first/) {
        #remove the first 3 lines of the array
	for (1..3) {
		pop @array;
	}
}
untie @array;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis,

Did you mean to use "shift" instead of "pop"?

NesPes,

Is A1, A2 and A3 always the first three lines of the file?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Um.. yep :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Kevin

The input file always has multiple sets of A1,A2,A3 but all of them are in the same order as shown in the example.
 
Oh.. if it has multiple sets in one file don't use my code :) I'll try and re-write it in a bit if someone else hasn't posted a solution.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
NesPes,

Have you tried to write some code?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Tie::File[/green][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]test.txt[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/tie.html][black][b]tie[/b][/black][/url] [black][b]my[/b][/black] [blue]@array[/blue], [red]'[/red][purple]Tie::File[/purple][red]'[/red], [blue]$file[/blue] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[maroon]LINE[/maroon][maroon]:[/maroon]
[olive][b]for[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue] = [fuchsia]0[/fuchsia][red];[/red] [blue]$i[/blue] <= [blue]$#array[/blue][red];[/red] [blue]$i[/blue]++[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$array[/blue][red][[/red][blue]$i[/blue][red]][/red] =~ [red]/[/red][purple]^A1[/purple][red]/[/red] && [blue]$array[/blue][red][[/red][blue]$i[/blue]+[fuchsia]1[/fuchsia][red]][/red] =~ [red]/[/red][purple]first[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/splice.html][black][b]splice[/b][/black][/url] [blue]@array[/blue], [blue]$i[/blue], [fuchsia]3[/fuchsia][red];[/red]
		[olive][b]last[/b][/olive] LINE[red];[/red]
	[red]}[/red]
[red]}[/red]
[url=http://perldoc.perl.org/functions/untie.html][black][b]untie[/b][/black][/url] [blue]@array[/blue][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]Tie::File - Access the lines of a disk file via a Perl array[/li]
[/ul]
[/tt]

- Miller
 
won't the 'last' stop the search through the array?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Yep.

His specs are unclear. However, I'm pretty sure they expressed a single removal being the goal. If not, then the last should be a redo. But we can only work with what we're told.

- Miller
 
[afro]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis needs a haircut ;)

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top