Kryten <kryte...@googlemail.com> asked: > This is embarrassing. > > All I want to do is read the content of a simple 10 line .txt file > into an array > and print out the lines, and I just can't seem to get it to work! > /* > $target = "D:\testfile.txt"; > open NEWBIE, "<"$target"; > @arr = <WAWA>; > foreach $line (@arr) { > print "$line\n"; > } > close(NEWBIE) > */
#!/usr/bin/perl # always turn on warnings and enforce strict use strict; use warnings; # use lexical variables. # modern Win32 knows about "normal" path separators, i.e. / instead of \ # use single quotes unless you want to interpolate the string i.e. # include variables or escape sequences. my $target = 'd:/testfile.txt'; # use a three argument open, use a lexical variable as a filehandle, # check for result and die() giving an error on failure. open( my $fh, '<', $target ) or die "Can't open '$target': $!"; # read the array from the filehandle you opened before my @array = <$fh>; # closing files is optional, but we're not messy programmers... close( $fh ); foreach my $line (@array){ # $line already has cr/lf; no need to add one more print $line; } __END__ HTH, Thomas -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/