Loading ...
Sorry, an error occurred while loading the content.
 

Associative arrays

Expand Messages
  • gordon stewart
    My script (without the dashes ) ================ while (($key, $value) = each %domains) { push @{$keys{$value}}, $key; print Y $value - $keys{$value} -
    Message 1 of 7 , Jun 12, 2004
      My script (without the dashes )
      ================

      while (($key, $value) = each %domains) {
      push @{$keys{$value}}, $key;
      print "Y $value - $keys{$value} - $key<BR>\n";

      }

      foreach $line (@keys){
      chomp ($line);
      print "$line $keys{$line}<BR>\n";
      }

      ============

      What i see on screen :-

      ============
      Y 561 - ARRAY(0x81dc390) - www.kwister.com
      Y 10 - ARRAY(0x81dc3b4) - nzocg.kwister.com
      Y 27 - ARRAY(0x81dc3d8) - immigration.kwister.com
      Y 93 - ARRAY(0x81dc818) - health.kwister.com
      Y 17 - ARRAY(0x81dc83c) - fashion.kwister.com
      Y 68 - ARRAY(0x81dc860) - nzpeople.kwister.com
      ============

      nothing is below this....


      SUMMARY / BACKGROUND

      Basically, im searching through my logs - Receiving
      the domain names, & the number of times people have
      viewed it..

      The problem is - Im having a hard time with
      associative arrays (as recommended in perldocs)

      http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#Data--Hashes-(Associative-Arrays)


      Is there a way to sort a hash - NUMERICALLY
      (descending)...

      the %domain, the key is the domain/subdomain name, &
      the value is the number of times its been viewed..

      PS - Just started - recently, so not many things
      logged - so far

      Can anyone give guidance

      G



      =====
      New Zealand Online Communities Group.
      nzocg@yahoogroups.com or http://nzocg.kwister.com/
      Spam free communities




      __________________________________
      Do you Yahoo!?
      Friends. Fun. Try the all-new Yahoo! Messenger.
      http://messenger.yahoo.com/
    • Charles K. Clarkson
      gordon stewart wrote: Gordon, you know better than to write scripts without strictures and warnings turned on. SO I have to assume you
      Message 2 of 7 , Jun 12, 2004
        gordon stewart <gordonisnz@...> wrote:

        Gordon, you know better than to write scripts without
        strictures and warnings turned on. SO I have to assume
        you have made all variables are declared at the top of
        your script in file scope. Stop doing that. It is not a
        recommended programming habit for perl code.

        [snip]
        : The problem is - Im having a hard time with
        : associative arrays (as recommended in perldocs)
        :
        :
        http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#Data--Hashes-(Associative
        -Arrays)
        :
        : Is there a way to sort a hash - NUMERICALLY
        : (descending)...


        Yes. You can either use a tied hash that keeps things
        numerically sorted for you or you can numerically sort
        the list keys.

        sort { $b <=> $a }, keys %hash;

        OR:

        foreach my $key ( sort { $b <=> $a }, keys %hash ) {
        # do something
        }


        BTW, this question is answered in detail in the link
        you provided under "How do I sort a hash (optionally by
        value instead of key)?"


        <http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#How-do-I-sort-a-hash-(op
        tionally-by-value-instead-of-key)->

        <http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#How-can-I-always-keep-my
        -hash-sorted->

        HTH,

        Charles K. Clarkson
        --
        Mobile Homes Specialist
        254 968-8328
      • chcst53
        ... (Associative-Arrays) ... I think you want to sort by values here (not by keys). This is an example of what you want. The output is: Y 561 -
        Message 3 of 7 , Jun 12, 2004
          --- In perl-beginner@yahoogroups.com, gordon stewart
          <gordonisnz@y...> wrote:
          >
          > My script (without the dashes )
          > ================
          >
          > while (($key, $value) = each %domains) {
          > push @{$keys{$value}}, $key;
          > print "Y $value - $keys{$value} - $key<BR>\n";
          >
          > }
          >
          > foreach $line (@keys){
          > chomp ($line);
          > print "$line $keys{$line}<BR>\n";
          > }
          >
          > ============
          >
          > What i see on screen :-
          >
          > ============
          > Y 561 - ARRAY(0x81dc390) - www.kwister.com
          > Y 10 - ARRAY(0x81dc3b4) - nzocg.kwister.com
          > Y 27 - ARRAY(0x81dc3d8) - immigration.kwister.com
          > Y 93 - ARRAY(0x81dc818) - health.kwister.com
          > Y 17 - ARRAY(0x81dc83c) - fashion.kwister.com
          > Y 68 - ARRAY(0x81dc860) - nzpeople.kwister.com
          > ============
          >
          > nothing is below this....
          >
          >
          > SUMMARY / BACKGROUND
          >
          > Basically, im searching through my logs - Receiving
          > the domain names, & the number of times people have
          > viewed it..
          >
          > The problem is - Im having a hard time with
          > associative arrays (as recommended in perldocs)
          >
          > http://www.perldoc.com/perl5.8.4/pod/perlfaq4.html#Data--Hashes-
          (Associative-Arrays)
          >
          >
          > Is there a way to sort a hash - NUMERICALLY
          > (descending)...
          >
          > the %domain, the key is the domain/subdomain name, &
          > the value is the number of times its been viewed..

          I think you want to sort by values here (not by keys).
          This is an example of what you want. The output is:

          Y 561 - www.kwister.com<BR>
          Y 93 - health.kwister.com<BR>
          Y 68 - nzpeople.kwister.com<BR>
          Y 27 - immigration.kwister<BR>
          Y 17 - fashion.kwister.com<BR>
          Y 10 - nzocg.kwister.com<BR>

          The code:

          #!/usr/bin/perl
          use strict;
          use warnings;

          my %domains = map {split} <DATA>; # (To load %domains hash)

          # Sort on values of %domains
          for (sort {$domains{$b} <=> $domains{$a}} keys %domains) {
          print "Y $domains{$_} - $_<BR>\n";
          }

          __DATA__
          www.kwister.com 561
          nzocg.kwister.com 10
          immigration.kwister 27
          health.kwister.com 93
          fashion.kwister.com 17
          nzpeople.kwister.com 68
        • merlyn@stonehenge.com
          ... Charles sort { $b $a }, keys %hash; remove the comma. Charles OR: Charles foreach my $key ( sort { $b $a }, keys %hash ) { Ditto. --
          Message 4 of 7 , Jun 13, 2004
            >>>>> "Charles" == Charles K Clarkson <cclarkson@...> writes:

            Charles> sort { $b <=> $a }, keys %hash;

            remove the comma.

            Charles> OR:

            Charles> foreach my $key ( sort { $b <=> $a }, keys %hash ) {

            Ditto.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
            See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
          • Charles K. Clarkson
            ... Argh! That s what I get for not testing before I post. Thanks Randal, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
            Message 5 of 7 , Jun 13, 2004
              From: merlyn@... <mailto:merlyn@...> wrote:

              :::::: "Charles" == Charles K Clarkson <cclarkson@...> writes:
              :
              : Charles> sort { $b <=> $a }, keys %hash;
              :
              : remove the comma.
              :
              : Charles> OR:
              :
              : Charles> foreach my $key ( sort { $b <=> $a }, keys %hash ) {
              :
              : Ditto.

              Argh! That's what I get for not testing before I post.

              Thanks Randal,

              Charles K. Clarkson
              --
              Mobile Homes Specialist
              254 968-8328
            • merlyn@stonehenge.com
              ... Charles Argh! That s what I get for not testing before I post. Charles Thanks Randal, The version of Perl I have installed in my brain works pretty
              Message 6 of 7 , Jun 13, 2004
                >>>>> "Charles" == Charles K Clarkson <cclarkson@...> writes:

                Charles> Argh! That's what I get for not testing before I post.

                Charles> Thanks Randal,

                The version of Perl I have installed in my brain works pretty well for
                testing. On the other hand, it took 15 years to install. :-)

                --
                Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
              • Charles K. Clarkson
                ... I ve been spoiled by using strict and warnings for even the most insignificant scripts. You are partly responsible for my handicap. :) Charles K. Clarkson
                Message 7 of 7 , Jun 13, 2004
                  merlyn@... <mailto:merlyn@...> wrote:

                  : The version of Perl I have installed in my brain works
                  : pretty well for testing. On the other hand, it took 15
                  : years to install. :-)

                  I've been spoiled by using strict and warnings for
                  even the most insignificant scripts. You are partly
                  responsible for my handicap. :)


                  Charles K. Clarkson
                  --
                  Mobile Homes Specialist
                  254 968-8328
                Your message has been successfully submitted and would be delivered to recipients shortly.