file name from command line

N

nicolas-laurent

New to Programming, I am trying to open a file from a command line
split.pl in.txt and print each word on separate line. Something is
missing but I don<t know what ? Please help

#
while ( $_ = <ARGV> ) {

# splits the line into an array of words
@words = split(/ /, $line);
}
print "@words;
 
P

phaylon

nicolas-laurent said:
New to Programming, I am trying to open a file from a command line
split.pl in.txt and print each word on separate line. Something is missing
but I don<t know what ? Please help

First, please read the posting guidelines that are posted frequently.
There are many helpful hints which make your and our life easier.

First of all, you should use

use warnings;
use strict;

which raise the common errors. For example, it says

Can't find string terminator '"' anywhere before EOF at - line 6.
while ( $_ = <ARGV> ) {

Try using

while (<>) {
# do_stuff();
}

hth,p
 
T

Tad McClellan

nicolas-laurent said:
New to Programming,


We can easily see that for ourselves.

I am trying to open a file from a command line
split.pl in.txt and print each word on separate line. Something is
missing but I don<t know what ?


There are several things missing:

It is not even a Perl program, it has syntax errors.

You put stuff into $_, but never do anything else with $_.

You are attempting to split $line when you have not given it a value.

Since you stomp over @words each loop iteration, the print outside
the loop will show only words from the very last line of the file.
If the last line happens to be blank, it will make no output at all.

while ( $_ = <ARGV> ) {

# splits the line into an array of words
@words = split(/ /, $line);
}
print "@words;


while ( <> ) {
print "$_\n" for split / /;
}
 
T

Tad McClellan

phaylon said:
nicolas-laurent wrote:
First of all, you should use

use warnings;
use strict;

which raise the common errors. For example, it says

Can't find string terminator '"' anywhere before EOF at - line 6.


You will get that message even *without* strict and warnings,
as it is for all syntax errors.

Try using

while (<>) {


That is the same thing, only written differently, so it is unlikely
to solve the problem.
 
P

phaylon

Tad said:
You will get that message even *without* strict and warnings, as it is for
all syntax errors.

Ouch. Ok, that was a bad error.
That is the same thing, only written differently, so it is unlikely to
solve the problem.

That was my bad memory, sorry for not double-checking before posting.
 
R

robic0

use strict;

if (!@ARGV) {
print "Enter a filename as a cmdline argument.\n";
exit (1);
}
if (!open(FILE, $ARGV[0])) {
die "can't open $ARGV[0]\n";
}

print "\nFile: @ARGV[0]\n";
my $lcnt = 1;
while (<FILE>) {
chomp;
print "L - ".$lcnt++."\n";
my $wcnt = 1;
for (my @words=split(' ')) {
my $info = sprintf (" %d\t%s", $wcnt++, $_);
print "$info\n";
}
}
close (FILE);

1;
 
T

Tad McClellan

if (!@ARGV) {
print "Enter a filename as a cmdline argument.\n";
exit (1);
}


Error message should go on STDERR, so you can replace
that whole if statement with:

warn "Enter a filename as a cmdline argument.\n" unless @ARGV;

print "\nFile: @ARGV[0]\n";

You should always enable warnings when developing Perl code!

(especially when you purport to be answering someone's question.)

my $lcnt = 1;


Why not just use the $. variable instead of maintaining your own?

for (my @words=split(' ')) {


A pattern match should *look like* a pattern match.

Why do you bother putting the words into @words?

You never use the @words variable after this.


for ( split / / ) {

my $info = sprintf (" %d\t%s", $wcnt++, $_);
print "$info\n";


Why not just use printf()?

printf (" %d\t%s\n", $wcnt++, $_);



Why is there a one there?
 
T

Tad McClellan

Tad McClellan said:
Error message should go on STDERR, so you can replace
that whole if statement with:

warn "Enter a filename as a cmdline argument.\n" unless @ARGV;


Doh!

I meant die() instead of warn(), else there will be no exit().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top