Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: while loop returning numbers not strings

by SuicideJunkie (Vicar)
on Dec 05, 2012 at 18:11 UTC ( #1007354=note: print w/replies, xml ) Need Help??

in reply to while loop returning numbers not strings

What do you think split is doing, and why are you using a scalar to store the result?

Try changing $word to @words

Replies are listed 'Best First'.
Re^2: while loop returning numbers not strings
by Lotus1 (Curate) on Dec 05, 2012 at 20:01 UTC

    Even using @words the code in the OP will print the length of each array since the print statement is using the . operator to concatenate.

    my @words = qw( this is the end ); print "words array is this long ".@words."\n"; print @words , "\n"; __END__ words array is this long 4 thisistheend
Re^2: while loop returning numbers not strings
by jaffinito34 (Acolyte) on Dec 05, 2012 at 18:20 UTC

    I'm trying to split the file.. each line is a new word

      ...I take in a text file (words.txt) where there is a new word each line.

      If each line is one word, splitting isn't necessary. Adapting your script, you can do the following:

      #!/usr/bin/env perl use strict; use warnings; open my $words, '<', 'words.txt' or die $!; while ( my $word = <$words> ) { print $word; } print "\n\n"; close $words;

      You may have noticed from the excellent responses that lexical variables (my) are used. It's best to follow this practice--even with file handles. Also, always use strict; and use warnings;. It's also best to handle open errors, in case there was a problem opening a file.

      Hope this helps!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007354]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2018-05-28 10:50 GMT
Find Nodes?
    Voting Booth?