Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How do you place words from a sentence into an array?

by pg (Canon)
on Oct 26, 2004 at 16:10 UTC ( [id://402673]=note: print w/replies, xml ) Need Help??


in reply to How do you place words from a sentence into an array?

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

Replies are listed 'Best First'.
Re^2: How do you place words from a sentence into an array?
by Diakoneo (Beadle) on Oct 26, 2004 at 16:22 UTC
    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'.}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found