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

Getting Input from a text file 2

Status
Not open for further replies.

Denwarius

Programmer
Joined
Jun 27, 2007
Messages
1
Location
GB
I have the following very simple code snippet as I am very new to perl. I cannot seem to get any of my code examples to accept input from a text file. Both my .pl file and my .txt file reside in the C:\perl\progs directory but I get 0 records found as my output. There is definately data in my text file. I use test.pl < test.txt
What could be the problem ?

#!C:\perl\bin\perl.exe
$i = 0;
while(<STDIN>)
{
$i++;
}
print("$i records found.");
 
Hi

Interesting. Your code works both with the Linux [tt]perl[/tt] compiled for CygWin and the ActiveState [tt]perl[/tt], from CygWin. But when I run it from Windows XP's command.com or cmd.exe, it indeed writes 0.

I suggest to either install CygWin to have a user & application friendly environment, or just pass the filename as parameter and open it from the script :
Code:
#!/cygdrive/c/bin/perl/bin/perl
$i = 0;
[red]open F,"<$ARGV[0]";[/red]
while(<[red]F[/red]>)
  {
  $i++;
  }
[red]close F;[/red]
print("$i records found.");

Feherke.
 
change the

while(<STDIN>) to while(<>)

and execute the script by

test.pl test.txt

not
test.pl <test.txt
 
Expanding your script to be a little more explicit:

Code:
[gray]#!C:\perl\bin\perl.exe[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [blue]$ARGV[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]No parameter passed to script[/purple][red]"[/red][red];[/red]

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

[black][b]my[/b][/black] [blue]$i[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<IN>[red])[/red] [red]{[/red]
	[blue]$i[/blue]++[red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]IN[red])[/red]:

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$i[/blue] records found.[/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

run using :
Code:
test.pl test.txt

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top