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

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

by pg (Canon)
on Oct 26, 2004 at 16:10 UTC ( #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? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-19 02:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (71 votes). Check out past polls.

    Notices?