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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very short perl script - Why doesn't it work? 1

Status
Not open for further replies.

johndoe3344

Programmer
Jul 5, 2007
13
US
Hi, I'm a newbie to perl. I just started learning it a few days ago. The short script that I made is:

#!/usr/bin/perl
use strict;
use warnings;

my $input = "sample.txt";
open (INPUT, $input) || die "File not found.\n";

@array = ();

while (my $input == INPUT) {

if (my $expression =~ /^\d+\t/) {
#matches at the beginning of the line for any number followed by a tab
push (@array, $expression);

} else {
$expression .= $expression;
}

}

unless ($expression =~ /KEYWORD/) {
print "$expression \n";
}

The file that it takes as input, sample.txt is something like as follows:

1 TEXTasdfasdf
TEXTasdfasdfasdf
2 TEXTasdfasdfasdf
asdfasdfasd KEYWORD asdfasdf
3 asdfasdfasdfasdf
adfasdfasdf

What I want the script to do is to display only the numbers containing "KEYWORD" so for this case, I just want it to display 2, and the corresponding text, but not 1 or 3.

But my script doesn't work? Why?
 
There are a few things wrong with it. Some logic, some use of assignment operators, lack of using of read operator <>. Here's a working version, I'll let you do the translation:

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$input[/blue] = [red]"[/red][purple]sample.txt[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]INPUT, [blue]$input[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$input[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]@array[/blue] = [red]([/red][red])[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$input[/blue] = <INPUT>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$input[/blue] =~ [red]/[/red][purple]^[purple][b]\d[/b][/purple]+[/purple][red]/[/red][red])[/red] [red]{[/red]
		[gray][i]#matches at the beginning of the line for any number followed by a tab[/i][/gray]
		[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@array[/blue], [red]'[/red][purple][/purple][red]'[/red][red];[/red]
	[red]}[/red]
	[blue]$array[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red] .= [blue]$input[/blue][red];[/red]
[red]}[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$expression[/blue] [red]([/red][blue]@array[/blue][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$expression[/blue] =~ [red]/[/red][purple]KEYWORD[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$expression[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

- Miller
 
Thanks a lot!

But could you explain the use of this line:

$array[-1] .= $input;

What is the [-1]?
 
When I try to use your code, I seem to get this message:

Modification of non-creatable array value attempted, subscript -1 at testscript.pl line 15, INPUT line 1.

Why is this?

 
My guess is that most likely the first line of your data file did not contain a number like your sample file did. You can add error checking for this very easily.

Code:
[gray]#!/usr/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$input[/blue] = [red]"[/red][purple]sample.txt[/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]INPUT, [blue]$input[/blue][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$input[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]@array[/blue] = [red]([/red][red])[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red][black][b]my[/b][/black] [blue]$input[/blue] = <INPUT>[red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$input[/blue] =~ [red]/[/red][purple]^[purple][b]\d[/b][/purple]+[/purple][red]/[/red][red])[/red] [red]{[/red]
		[gray][i]#matches at the beginning of the line for any number followed by a tab[/i][/gray]
		[url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@array[/blue], [red]'[/red][purple][/purple][red]'[/red][red];[/red]
	[red]}[/red] [olive][b]elsif[/b][/olive] [red]([/red]! [blue]@array[/blue][red])[/red] [red]{[/red]
		[black][b]die[/b][/black] [red]"[/red][purple]Line without record found: [blue]$input[/blue][/purple][red]"[/red][red];[/red]
	[red]}[/red]

	[blue]$array[/blue][red][[/red]-[fuchsia]1[/fuchsia][red]][/red] .= [blue]$input[/blue][red];[/red]
[red]}[/red]

[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$expression[/blue] [red]([/red][blue]@array[/blue][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][blue]$expression[/blue] =~ [red]/[/red][purple]KEYWORD[/purple][red]/[/red][red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$expression[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

Alternatively, if you just want to continue searching through your data file until you actually discover the beginning of real data, then just change the die statement to a "next;" command.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top