Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move data from Array to hash? 2

Status
Not open for further replies.

Trevoke

Programmer
Joined
Jun 6, 2002
Messages
1,142
Location
US
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"
 
You don't need to create the array and hash (though you would need to predeclare them if you're using the strict pragma and they're globals); they are created automatically.

Your code would be something like:
[tt]
foreach(@words)
{
$counts{$_}++;
}
foreach (keys %counts)
{
print "$_: $counts{$_}\n";
}
[/tt]
 
Well, I don't know how to get it to go through a list which I don't hard-code (interaction is in the next chapter - I peeked ahead).
So I could have something like..

Code:
@words # array definition
for $i(@words)
  {
    $counts($i)++;
  }
for $i (keys %counts)
  {
    print "$i : $counts{&i}\n";
  }

Or is the $counts($_) a required way of writing it for hash creation/modification ? What is $_ exactly?

"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"
 
# Syntax Error: Should be $count{$i}++;

Code:
@words # array definition
for $i(@words)
  {
    $counts[COLOR=red]([/color]$i[COLOR=red])[/color]++;
  }
for $i (keys %counts)
  {
    print "$i : $counts{&i}\n";
  }
 
Make that 2 syntax errors:
Code:
@words # array definition
for $i(@words)
  {
    $counts[COLOR=red]{[/color]$i[COLOR=red]}[/color]++;
  }
for $i (keys %counts)
  {
    print "$i : $counts{[COLOR=red]$[/color]i}\n";
  }
 
Oh-oh.. Thanks!

So what's the $_ syntax?

"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"
 
Trevoke said:
So what's the $_ syntax?

- Search for "$ARG"

In this instance, he was using it as a default iterator in the for loop. It comes in handy for small code. But during the learning process, I think that it just confuses newbs. Especially those of you coming from more verbose languages like C++ :)
 
The $_ is the default variable; if you don't specify a particular variable, like $i, in the "for" header, $_ gets used instead. Perl is full of little (and big) shortcuts like that.
 
Interesting. This language gets more and more interesting by the second. I wonder why it gets bashed so much.

"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"
 
Trevoke said:
Interesting. This language gets more and more interesting by the second. I wonder why it gets bashed so much.

Because not every programmer is able to enforce good programming practices on their own initiative. Perl's motto TIMTOWTDI (There is more than one way to do it) provides for an incredible amount of flexibility and power for those who know what they are doing. But for those who don't, the motto should be TIMTOWTDIB or TIMTOWTDIW (There is more than one way to do it badly/wrong).

Enjoy :)
 
Perl: There's more than one way to do it badly.

I'm not sure how many people are going to take it up with a tag line like that. :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top