Erwin Zavala wrote:
> I know I could use the filehandle but I am trying to understand why I
> cannot use the array and print each element at the time.
>
> #!/usr/bin/perl
> use strict;
> use Fcntl;
>
> $/ = "\n\n";
> sysopen(readFile, "cnnArticle.txt", O_RDONLY) || die $!;
> sysopen(writeFile, "fortuneCookie.txt", O_WRONLY|O_CREAT) || die $1;
>
> my @articleArray = <readFile>;
All fine so far. You have the contents of the file in your array.
> while(<@articleArray>)
Wrong here. You're using the file globbing operator <> on the
array. This first enforces scalar content on it operand by
doing
join ' ', @articleArray
which is a single string containing all of the file records separated
with spaces. 'glob' then produces a list of all of the 'filenames' in
this string, which will return one space-separated 'word' from
the string on each call. This convoluted mechanics gives you
one execution of the loop for each word in the file.
> {
> print $_;
The same as
print;
> }
What you need is 'foreach' on the array, but without the
glob operator <>.
foreach ( @articleArray ) {
print;
}
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]