Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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";


 print $this_word;

in the foreach loop, yet nothing is printed

Replies are listed 'Best First'.
Re^3: putting text into array word by word
by Marshall (Abbot) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947058]
[PriNet]: go figure, i just tried that, it retains some of the values (not the key) of the pre-assigned value
[PriNet]: i guess i'll just have to use two seperate hashes, there are two, but one is larger than the other, and when i reference the smaller one, it holds onto the values that were assigned to the larger hash (the keynames change, but unless i reassign a new valu

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2017-06-28 03:02 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (619 votes). Check out past polls.