Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: putting text into array word by word

by jms53 (Monk)
on Jan 09, 2012 at 18:50 UTC ( #947058=note: print w/ replies, xml ) Need Help??


in reply to Re: putting text into array word by word
in thread putting text into array word by word

I have added  print "$this_word";

and

 print $this_word;

in the foreach loop, yet nothing is printed


Comment on Re^2: putting text into array word by word
Select or Download Code
Re^3: putting text into array word by word
by Marshall (Prior) on Jan 09, 2012 at 19:05 UTC
    Your file "open mode" is wrong, use '<' for read-only.
    #!/usr/bin/perl -w use strict; my @words; open FILE, "<", "input.txt" or die "unable to open input.txt $!"; print "file loaded \n"; while (<FILE>) { @words = split(' ', $_); push @all_words, @words; } foreach $word (@words) { print "$word\n"; } __END__ # if you want to count the words # then that is different - use a hash # table of "word => count" #the default split (/\s+/,$_) #differs only between this special case split(' ',$_) #in how it handles a "null" field at the beginning of the line my %words; while (<FILE>) { my @words = split; foreach my $word (@words) { $words{$word}++; } } === or === my %words; while (<FILE>) { foreach my $word (split) { $words{$word}++; } } ==== or === my %words; while (<FILE>) { $words{$_}++ foreach (split); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947058]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-07-28 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (196 votes), past polls