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

matching an array

Expand Messages
  • TORO_MFG
    I have a list say, @category = (dog, dog, cat, mouse, cat, dog); and I want to parse through it and return a list with just unique names. @uniqueCategories =
    Message 1 of 3 , Dec 31, 2003
    • 0 Attachment
      I have a list say, @category = (dog, dog, cat, mouse, cat, dog);

      and I want to parse through it and return a list with just unique names.

      @uniqueCategories = (dog, cat, mouse);

      I tried

      @uniqueCategories = ();
      foreach (@category) {
      if(!/$_/, @uniqueCategories){
      push @uniqueCategories, $_;
      }
      }

      but this doesn't seem to work. Help anyone?
    • merlyn@stonehenge.com
      ... TORO I have a list say, @category = (dog, dog, cat, mouse, cat, dog); TORO and I want to parse through it and return a list with just unique names. TORO
      Message 2 of 3 , Dec 31, 2003
      • 0 Attachment
        >>>>> "TORO" == TORO MFG <toro_mfg@...> writes:

        TORO> I have a list say, @category = (dog, dog, cat, mouse, cat, dog);
        TORO> and I want to parse through it and return a list with just unique names.

        TORO> @uniqueCategories = (dog, cat, mouse);

        TORO> I tried

        TORO> @uniqueCategories = ();
        TORO> foreach (@category) {
        TORO> if(!/$_/, @uniqueCategories){
        TORO> push @uniqueCategories, $_;
        TORO> }
        TORO> }

        TORO> but this doesn't seem to work. Help anyone?

        This is a FAQ. The perlfaqs are on your disk. "perldoc perlfaq" prints
        the table of contents. "perldoc -q KEYWORD" searches the faqs for a keyword.

        In particular, "perldoc -q unique" returns the following:

        Found in /usr/libdata/perl5/pod/perlfaq4.pod
        How can I get the unique keys from two hashes?
        First you extract the keys from the hashes into lists, then
        solve the "removing duplicates" problem described above. For
        example:

        %seen = ();
        for $element (keys(%foo), keys(%bar)) {
        $seen{$element}++;
        }
        @uniq = keys %seen;

        Or more succinctly:

        @uniq = keys %{{%foo,%bar}};

        Or if you really want to save space:

        %seen = ();
        while (defined ($key = each %foo)) {
        $seen{$key}++;
        }
        while (defined ($key = each %bar)) {
        $seen{$key}++;
        }
        @uniq = keys %seen;

        Found in /usr/libdata/perl5/pod/perlfaq8.pod
        How can I call my system's unique C functions from Perl?
        In most cases, you write an external module to do it--see the
        answer to "Where can I learn about linking C with Perl? [h2xs,
        xsubpp]". However, if the function is a system call, and your
        system supports syscall(), you can use the syscall function
        (documented in perlfunc).

        Remember to check the modules that came with your distribution,
        and CPAN as well--someone may already have written a module to
        do it.

        Please learn to use the docs on your disk before asking questions
        on mailing lists.

        --
        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!
      • Nick Keeling
        You could use foreach (@category) { $uniqueCategories{$_}+=1; } This will place them in a hash and count how many occurrences of each item in the @category
        Message 3 of 3 , Jan 3, 2004
        • 0 Attachment
          You could use

          foreach (@category)
          {
          $uniqueCategories{$_}+=1;
          }

          This will place them in a hash and count how many occurrences of each
          item in the @category array.

          >>> toro_mfg@... 12/31/03 12:06PM >>>
          I have a list say, @category = (dog, dog, cat, mouse, cat, dog);

          and I want to parse through it and return a list with just unique
          names.

          @uniqueCategories = (dog, cat, mouse);

          I tried

          @uniqueCategories = ();
          foreach (@category) {
          if(!/$_/, @uniqueCategories){
          push @uniqueCategories, $_;
          }
          }

          but this doesn't seem to work. Help anyone?


          Unsubscribing info is here:
          http://help.yahoo.com/help/us/groups/groups-32.html

          Yahoo! Groups Links

          To visit your group on the web, go to:
          http://groups.yahoo.com/group/perl-beginner/

          To unsubscribe from this group, send an email to:
          perl-beginner-unsubscribe@yahoogroups.com

          Your use of Yahoo! Groups is subject to:
          http://docs.yahoo.com/info/terms/
        Your message has been successfully submitted and would be delivered to recipients shortly.