in reply to Basic Array Question

Perl calls "associative arrays" hashes. It makes sense and it saves you a bunch of syllables to use at a later date. Anyway, you set a key-value pair in a hash this way: $hash{$key} = $value; Therefore, if your array, @row, has two elements, and you want the first field to be the key and the second field to be the value, you would do: $frequency{$row[0]} = $row[1];

In fact, you might want to use more descriptive names in your while statement:

my %freq; while (my ($page, $hits) = $cursor->fetchrow_array) { $freq{$page} = $hits; }

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Replies are listed 'Best First'.
Re^2: Basic Array Question
by bart (Canon) on May 11, 2006 at 09:53 UTC
    Perl calls "associative arrays" hashes. It makes sense and it saves you a bunch of syllables to use at a later date.
    Yes to the latter, maybe to the former. The fact that associative arrays in perl are implemented using a hashing mechanism, is just an implementation detail that has no influence on how people use them. So I'm more or less unhappy with that name.

    I'd prefer "string-indexed arrays" any day, but of course, people with find that name too long, too.

      I wouldn't mind "map" (except that's overloaded) or "mapping", and Python calls them (or at least used to call them) "dictionaries", which also makes sense (except you tend to think of a dictionary as being sorted alphabetically).

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^2: Basic Array Question
by leocharre (Priest) on May 10, 2006 at 17:28 UTC

    This is one way:

    while (@_ = $cursor->fetchrow) { $freq{$_[0]}=$_[1]; }

    What happens is you are associating the result to an anonymous array (@_)