Brady Jacksan wrote:
>
> Hi
Hello,
> I am writing a script that reads file from command line and places
> each line from the files into an array, and prints out the total lines
> read and then
> each line read.
>
> while (defined ($files = <>)) {
^^^^^^^ ^
Unless you are using an older version of Perl the defined() is already
done by perl whenever you use the diamond operator in a while
conditional.
perldoc perlop
[snip]
Ordinarily you must assign the returned value to a vari�
able, but there is one situation where an automatic
assignment happens. If and only if the input symbol is
the only thing inside the conditional of a `while' state�
ment (even if disguised as a `for(;;)' loop), the value is
automatically assigned to the global variable $_, destroy�
ing whatever was there previously. (This may seem like an
odd thing to you, but you'll use the construct in almost
every Perl script you write.) The $_ variables is not
implicitly localized. You'll have to put a `local $_;'
before the loop if you want that to happen.
The following lines are equivalent:
while (defined($_ = <STDIN>)) { print; }
while ($_ = <STDIN>) { print; }
while (<STDIN>) { print; }
for (;<STDIN>;) { print; }
print while defined($_ = <STDIN>);
print while ($_ = <STDIN>);
print while <STDIN>;
This also behaves similarly, but avoids $_ :
while (my $line = <STDIN>) { print $line }
In these loop constructs, the assigned value (whether
assignment is automatic or explicit) is then tested to see
whether it is defined. The defined test avoids problems
where line has a string value that would be treated as
false by Perl, for example a "" or a "0" with no trailing
newline. If you really mean for such values to terminate
the loop, they should be tested for explicitly:
while (($_ = <STDIN>) ne '0') { ... }
while (<STDIN>) { last unless $_; ... }
> chomp;
chomp() by default chomps the $_ variable but you have assigned the
current line to the $files variable.
chomp $files;
> print "Total lines in the files:@files\."
If you want to print the number of elements in an array you have to
force a scalar context on the array.
print 'Total lines in the files:', scalar @files, '.';
Or:
print 'Total lines in the files:' . @files . '.';
> print "@files\n"
>
> why do keep getting compilation errors?
If you want help with specific error messages then you should include
those error messages in your email.
A simpler way to read all the lines into an array is:
chomp( my @files = <> );
print 'Total lines in the files: ' . @files . ".\n@files\n";
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]