http://qs1969.pair.com?node_id=402655

Diakoneo has asked for the wisdom of the Perl Monks concerning the following question:

User: New to programming, new to Perl, even newer to Perl Monks.
Scenario: I'm trying to do an exercise from a "Learn Perl in 30 seconds" book. OK, I'm totally over-engineering the excercise, but I'm too curious about the solution to give up.
Problem: I have a file with several lines of data. Each line is made up of words separated by spaces (much like a paragraph in a book.) The number of words per line and size of each word is random. There won't be any punctuation, just words separated by spaces.

Ex.
three blind mice
three blind mice
see how they run

I want to read in a line, put each word into an array, then perform my horrible machinations on the words. I've figured out:

open FILE, "bingo_in.txt"; while (<FILE>) { chomp; foreach (@word = <FILE>) {

but I can't figure out how to break the words of the sentence (line) into an array. I've tried shift, etc. but it always treats the sentence (line) as one big chunk of data.

Thanks for any help with the problem and any tips on how to format my questions (I actually have a couple more.)

Replies are listed 'Best First'.
Re: How do you place words from a sentence into an array?
by JediWizard (Deacon) on Oct 26, 2004 at 15:55 UTC

    So you want to iterate through each word in a file doing some processing on it? Try this:

    open(my $fh, "< bingo_in.txt") || die "Couldn't read file: $!\n"; while(<$fh>){ foreach my $word (split){ #process word } }
    May the Force be with you
Re: How do you place words from a sentence into an array?
by kvale (Monsignor) on Oct 26, 2004 at 15:56 UTC
    Use split to break the sentence into words:
    open FILE, "bingo_in.txt"; while (<FILE>) { my @words = split; }
    This form of split uses the defaults of splitting $_ and using whitespace as a delimiter.

    -Mark

Re: How do you place words from a sentence into an array?
by pg (Canon) on Oct 26, 2004 at 16:10 UTC

    As everybody else pointed out, split will save you ;-)

    What I want to add is, to use hash instead of array in this case. Why not go one step further and make your exercise more meaningful, and think about better data structure.

    The benefit of using hash is so that you can count the frequency of each word:

    use Data::Dumper; use strict; use warnings; my $words; while (<DATA>) { for (split) { $words->{$_} ++; } } print Dumper($words); __DATA__ three blind mice three blind mice see how they run
      Thanks everyone, all your examples helped me think through what I really wanted to do! {Turns to see what the Sage Camel has to say about 'split'.}
Re: How do you place words from a sentence into an array?
by borisz (Canon) on Oct 26, 2004 at 15:56 UTC
Re: How do you place words from a sentence into an array?
by gaal (Parson) on Oct 26, 2004 at 16:34 UTC
    Everybody said split, but note that if you have some punctuiation, you may be disappointed; for this sentence, for example, $words[2] is "split,".

    You must have heard of CPAN: it has lots of modules. Lingua::EN::Splitter is one that (if you're looking for a real-world solution) can help you here. Of course, split *is* useful to know! But when it comes to human language, problems are often more complex than you think.

Re: How do you place words from a sentence into an array?
by Happy-the-monk (Canon) on Oct 26, 2004 at 15:58 UTC

    You are looking for split:

    open( FILE, "bingo_in.txt" ) or die $!;
    while (<FILE>) {
        # chomp; - isn't needed as pg points out in the node following up.
        my @words = split; #
    no arguments: split $_ on white space
        foreach my $word (@words) { ...

    Cheers, Sören

    Update: chomp-line commented out

      chomp is not needed here, you can see this through this demo:

      use Data::Dumper; use strict; use warnings; $_ = "1 2\n3\t4\r\n5\n"; my @words = split; print Dumper(\@words);

      It gives you:

      $VAR1 = [ '1', '2', '3', '4', '5' ];
Re: How do you place words from a sentence into an array?
by prasadbabu (Prior) on Oct 26, 2004 at 16:05 UTC
    open FILE, "bingo_in.txt"; undef $/; $file = <FILE>; @lines = split /\n/, $file; for (@lines ) { @words = split; }

    TIMTOWTDI

    --Prasad

      There is a bug, @words ends up only containing the "words" in the last line ;-) as you are resetting it for each line.

      Also there was no need for two splits, just one is good enough, and better use strict ;-)

      use Data::Dumper; use strict; use warnings; open FILE, "a.txt"; undef $/; $_ = <FILE>; close(FILE); my @words = split; print Dumper(\@words);
Re: How do you place words from a sentence into an array?
by TedPride (Priest) on Oct 26, 2004 at 21:10 UTC
    The following tracks word frequency:
    use strict; use warnings; my %hash; while (<DATA>) { chomp; $hash{$_}++ for (split); } print "ALPHABETIC\n"; print "$_ $hash{$_}\n" for (sort keys %hash); print "\nBY NUMBER\n"; print "$_ $hash{$_}\n" for (sort {$hash{$b} <=> $hash{$a} || $a cmp $b} keys %hash); __DATA__ three blind mice three blind mice see how they run