I sincerely appreciate the tips on improving my code; I implement (or at least take strong note) of all the suggestions I receive. In the code I posted, however, I'm primarily interested in learning if there's a way to avoid opening the file to determine the character encoding, and then opening it again with the character encoding specified. In the second block of code that I originally posted, I only open the file once but I'm consistently encountering the "UTF16: Unrecognised BOM" error that I mentioned.
Does anyone have any ideas how I can make the second block of code work? Or otherwise accomplish the task without opening the .txt file twice? === Douglas Cacialli, M.A. - Doctoral candidate Clinical Psychology Training Program University of Nebraska-Lincoln Lincoln, Nebraska 68588-0308 === On Sun, Apr 4, 2010 at 11:10 AM, Dr.Ruud <rvtol+use...@isolution.nl> wrote: > Doug Cacialli wrote: > >> $datapath =~ s/^\s+//; >> $datapath =~ s/\s+$//; > > Alternative notation: > > s/\s+$//, s/^\s+// for $datapath; > > I have a "sub trim()" in my Toolbox, so I just call "trim($datapath);". > > >> open (my $filehndl , "<", "$datapath") || >> die ("Can't open .txt file $datapath. Exiting program.\n\n"); > > Why quote $datapath? > (It isn't in a class with overloaded stringification.) > > open my $filehndl , "<", $datapath or die "'$datapath': $!\n"; > > >> while (my $line = <$filehdl2>) >> { >> chomp $line; >> my @words = split / /, $line; >> my $nr_words = @words; >> print "\n$line\n"; > >> print "The line above has " . scalar @words ... > > If you wouldn't chomp, you wouldn't have to add a "\n". > But then you need to split on " " (to get rid of empty trailings). > > > while (my $line = <$fh>) { > my @words = split " ", $line; > print $line, "\thas ", scalar(@words), " words.\n"; > } > > > -- > Ruud > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/