Hey everyone, I'm trudging along the perl tutorial on the perl website (or, let's say I took the first step).
They've got sample exercises at the bottom, and one says to "A word frequency counter. How often does each word show up in an array of words? Print out a report. (Hint: Use a hash to count of the number of appearances of each word.)"
We've been taught for(), print, keys() and int()..
The basic idea is :
create array
create hash (because I don't know if I can create a hash on the fly in a loop and it'll stay there and be updated and all)
for $i (@words)
{
check that it exists in hash and if it does, add one to the count, otherwise add it to the hash with a count of one
}
I'm thinking about it wrong, probably, as my previous experience is with C++ ..
Could you tell me how I'm supposed to think about it?
"That time in Seattle... was a nightmare. I came out of it dead broke, without a house, without anything except a girlfriend and a knowledge of UNIX."
"Well, that's something," Avi says. "Normally those two are mutually exclusive."
-- Neal Stephenson, "Cryptonomicon"
They've got sample exercises at the bottom, and one says to "A word frequency counter. How often does each word show up in an array of words? Print out a report. (Hint: Use a hash to count of the number of appearances of each word.)"
We've been taught for(), print, keys() and int()..
The basic idea is :
create array
create hash (because I don't know if I can create a hash on the fly in a loop and it'll stay there and be updated and all)
for $i (@words)
{
check that it exists in hash and if it does, add one to the count, otherwise add it to the hash with a count of one
}
I'm thinking about it wrong, probably, as my previous experience is with C++ ..
Could you tell me how I'm supposed to think about it?

"That time in Seattle... was a nightmare. I came out of it dead broke, without a house, without anything except a girlfriend and a knowledge of UNIX."
"Well, that's something," Avi says. "Normally those two are mutually exclusive."
-- Neal Stephenson, "Cryptonomicon"