in reply to Splitting an array loaded from a file handle.

I think you're confusing yourself by using the term "split" in two different ways - one of which has nothing to do with splitting. You don't "split" a filehandle into an array, but you can slurp it - which is exactly what you're doing above, with the '@l=<FH3>' line. Perhaps what you mean is that you would like to process the array that you've created by slurping, one element (i.e., one line) at a time:

for my $line (@l){ chomp $line; # Remove the newline from the end print $line . " - processed by 'trenchwar'\n"; # Add a little co +ntent to each line }

Or you could do the same thing by using Perl's implicit iterator, '$_':

for (@l){ chomp; print "$_ - processed by 'trenchwar'\n"; }

or even by using the 'map' function:

chomp @l; # Remove the newline from all elements print map { "$_ - processed by 'trenchwar'\n" } @l;

I note, by the way, that your script has several doubtful constructs in it:

open (FH3, ">lessons.txt"); ### You should *always* check the return from any calls to the system. open FH3, ">lessons.txt" or die "lessons.txt: $!\n"; ### You should also close this filehandle; don't assume that your file + has actually ### been written before you do so! close FH3; $l="lessons.txt"; open(FH3, $l) || die("Could not open file!"); ### Don't create variables that you're only going to use once; just us +e the value. ### *And* check the return of the call. And don't pre-assume the reaso +n for failure: ### find out what it is with the '$!' (actual error) variable. open FH3, "lessons.txt" or die "lessons.txt: $!\n";

Update: Added a description of the errors in the script.


-- 
Human history becomes more and more a race between education and catastrophe. -- HG Wells

Replies are listed 'Best First'.
Re^2: Splitting an array loaded from a file handle.
by trenchwar (Beadle) on Apr 14, 2008 at 16:12 UTC
    That makes much more sense, I cleaned it up and think I have a better grasp on what I was doing.
    One more small question, if I define the variable before reading it into an array can I print it out of the block?
    Thank you for what seems to be your infinite patients when passing along your knowledge. I appreciate it more than you will ever know.
    #!/usr/bin/perl use warnings; use strict; my @l; open (FH3, ">lessons.txt"); print FH3 "Perl*Lesson1\n"; print FH3 "Perl*Lesson2\n"; print FH3 "Perl*Lesson3\n"; print FH3 "Java*Lesson1\n"; print FH3 "Java*Lesson2\n"; print FH3 "Java*Lesson3\n"; print FH3 "PHP*Lesson1\n"; print FH3 "PHP*Lesson2\n"; print FH3 "PHP*Lesson3\n"; close (FH3); my $l="lessons.txt"; open FH3, "<", $l or die "$l: $!\n"; @l=<FH3>; for my $line (@l){ chomp $line; my ($part1, $part2) = split /\*/,$line; print "$part1 $part2\n"; }