#!/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); }
In reply to Re^3: putting text into array word by word
by Marshall
in thread putting text into array word by word
by jms53
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |