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

RE: [PBML] Comparing strings with array of values

Expand Messages
  • Charles K. Clarkson
    ... Random lookups are better handled with hashes, not arrays. If you need to maintain order use an ordered hash. if ( $is_valid_color{ $string } ) { Or: if (
    Message 1 of 11 , Sep 1, 2005
    • 0 Attachment
      James Colannino <> wrote:
      : Hey everyone. I was wondering if there are any single functions that
      : take a string and compare it against every string in an array to see
      : if
      : a match is found. I could do this manually with a foreach loop, but
      : I'd prefer to use a single builtin function is one exists. Thanks in
      : advance.

      Random lookups are better handled with hashes, not arrays. If you
      need to maintain order use an ordered hash.

      if ( $is_valid_color{ $string } ) {

      Or:

      if ( defined $colors{ $string } ) {


      HTH,

      Charles K. Clarkson
      --
      Mobile Homes Specialist
      254 968-8328
    • merlyn@stonehenge.com
      ... James Hey everyone. I was wondering if there are any single James functions that take a string and compare it against every James string in an array to
      Message 2 of 11 , Sep 1, 2005
      • 0 Attachment
        >>>>> "James" == James Colannino <perl-beginner@...> writes:

        James> Hey everyone. I was wondering if there are any single
        James> functions that take a string and compare it against every
        James> string in an array to see if a match is found. I could do this
        James> manually with a foreach loop, but I'd prefer to use a single
        James> builtin function is one exists. Thanks in advance.

        This is a FAQ. Please learn to at least scan the titles provided in
        "perldoc perlfaq" before posting here. Even better, read through the
        FAQ instead of wasting time surfing the web or filling out your blog
        or chatting on IRC.

        Found in /usr/libdata/perl5/pod/perlfaq4.pod
        How can I tell whether a certain element is contained in a list or array?
        Hearing the word "in" is an *in*dication that you probably should have
        used a hash, not a list or array, to store your data. Hashes are
        designed to answer this question quickly and efficiently. Arrays aren't.

        That being said, there are several ways to approach this. If you are
        going to make this query many times over arbitrary string values, the
        fastest way is probably to invert the original array and maintain a hash
        whose keys are the first array's values.

        @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
        %is_blue = ();
        for (@blues) { $is_blue{$_} = 1 }

        Now you can check whether $is_blue{$some_color}. It might have been a
        good idea to keep the blues all in a hash in the first place.

        If the values are all small integers, you could use a simple indexed
        array. This kind of an array will take up less space:

        @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
        @is_tiny_prime = ();
        for (@primes) { $is_tiny_prime[$_] = 1 }
        # or simply @istiny_prime[@primes] = (1) x @primes;

        Now you check whether $is_tiny_prime[$some_number].

        If the values in question are integers instead of strings, you can save
        quite a lot of space by using bit strings instead:

        @articles = ( 1..10, 150..2000, 2017 );
        undef $read;
        for (@articles) { vec($read,$_,1) = 1 }

        Now check whether "vec($read,$n,1)" is true for some $n.

        Please do not use

        ($is_there) = grep $_ eq $whatever, @array;

        or worse yet

        ($is_there) = grep /$whatever/, @array;

        These are slow (checks every element even if the first matches),
        inefficient (same reason), and potentially buggy (what if there are
        regex characters in $whatever?). If you're only testing once, then use:

        $is_there = 0;
        foreach $elt (@array) {
        if ($elt eq $elt_to_find) {
        $is_there = 1;
        last;
        }
        }
        if ($is_there) { ... }


        --
        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!
      • James Colannino
        ... I should have. Honestly I had forgotten that there was a perl faq I could read. If it had occured to me I would have done so. I ll remember to read it
        Message 3 of 11 , Sep 1, 2005
        • 0 Attachment
          Randal L. Schwartz wrote:

          >This is a FAQ. Please learn to at least scan the titles provided in
          >"perldoc perlfaq" before posting here.
          >

          I should have. Honestly I had forgotten that there was a perl faq I
          could read. If it had occured to me I would have done so. I'll
          remember to read it from now on.

          >Even better, read through the
          >FAQ instead of wasting time surfing the web or filling out your blog
          >or chatting on IRC.
          >
          >

          I'll keep my comments short and polite and simply state that you're very
          rude to have sent such a post publically to this list (if you wanted to
          accuse me of not having put any effort into searching for the answer,
          especially in such a sarcastic and hostile tone, you should have done so
          privately.) I would have done a google search but had no idea what to
          look for being that I AM A BEGINNER with perl (implied by the fact that
          I'm a member of the "perl-beginner" mailing list. Had I known what to
          look for, I would have looked for it. Not only that, but I did decide
          that I was going to continue looking even though I didn't know what to
          look for. Anyway, I'll get back to my job now (I'm trying to write a
          script to manage updates for work and just got out of an hour-long
          meeting; a far cry from surfing the web or filling out my blog.)

          > Found in /usr/libdata/perl5/pod/perlfaq4.pod[...]
          >

          Thanks for taking the time to copy the entry from the faq. I do
          appreciate that.

          James
        • James Colannino
          ... Thanks :) James
          Message 4 of 11 , Sep 1, 2005
          • 0 Attachment
            Charles K. Clarkson wrote:

            >James Colannino <> wrote:
            >: Hey everyone. I was wondering if there are any single functions that
            >: take a string and compare it against every string in an array to see
            >: if
            >: a match is found. I could do this manually with a foreach loop, but
            >: I'd prefer to use a single builtin function is one exists. Thanks in
            >: advance.
            >
            > Random lookups are better handled with hashes, not arrays. If you
            >need to maintain order use an ordered hash.
            >
            >if ( $is_valid_color{ $string } ) {
            >
            > Or:
            >
            >if ( defined $colors{ $string } ) {
            >
            >

            Thanks :)

            James
          • merlyn@stonehenge.com
            ... James I should have. Honestly I had forgotten that there was a perl faq I James could read. If it had occured to me I would have done so. I ll James
            Message 5 of 11 , Sep 1, 2005
            • 0 Attachment
              >>>>> "James" == James Colannino <perl-beginner@...> writes:

              James> I should have. Honestly I had forgotten that there was a perl faq I
              James> could read. If it had occured to me I would have done so. I'll
              James> remember to read it from now on.

              How could you have also forgotten that Perl comes with approximately
              ONE THOUSAND pages of documentation and looked there though. At least
              at the table of contents? The table of contents lists no less than
              *nine* lines (ten if you count the TOC) that match something like
              "FAQ".

              >> Even better, read through the
              >> FAQ instead of wasting time surfing the web or filling out your blog
              >> or chatting on IRC.
              >>

              James> I'll keep my comments short and polite and simply state that you're
              James> very rude to have sent such a post publically to this list (if you
              James> wanted to accuse me of not having put any effort into searching for
              James> the answer, especially in such a sarcastic and hostile tone, you
              James> should have done so privately.)

              I'm just going to repeat... if the shoe fits, wear it, otherwise brush
              it off. And the response is not just for you... it's also a warning
              to others to AT LEAST DO YOUR BASIC SEARCH BEFORE ASKING.

              The fact that you're trying to use Perl, but didn't even *remember*
              there was a FAQ, let alone look in it, already says everything about
              you that I need to know with respect to this exchange. Maybe you're a
              different person about other programming languages, or in other
              situations, but I really don't feel I'm out of line to point out
              rather bluntly that you were either lazy or distracted this time
              around, and that WASTES ALL OF OUR TIME, including yours.

              --
              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!
            • Grier Johnson
              On 01 Sep 2005 16:00:58 -0700, merlyn@stonehenge.com ... Is there any chance you can be convinced to lay off the venom and just ignore people you don t think
              Message 6 of 11 , Sep 1, 2005
              • 0 Attachment
                On 01 Sep 2005 16:00:58 -0700, merlyn@...
                <merlyn@...> wrote:

                > The fact that you're trying to use Perl, but didn't even *remember*
                > there was a FAQ, let alone look in it, already says everything about
                > you that I need to know with respect to this exchange. Maybe you're a
                > different person about other programming languages, or in other
                > situations, but I really don't feel I'm out of line to point out
                > rather bluntly that you were either lazy or distracted this time
                > around, and that WASTES ALL OF OUR TIME, including yours.

                Is there any chance you can be convinced to lay off the venom and just
                ignore people you don't think have done their due diligence? Seeing
                your nasty replies is, to me, far worse then someone asking a question
                that's already in the faq.

                Time would be saved on both sides (and you seem awfully concerned with
                this) if you simple didn't reply or spent the bare amount of time
                giving a link to the perl faq.

                --
                Grier
              • acummingsus
                ... (in) an array ] FWIW, if anyone interested: http://perldoc.perl.org/search.html?q=element+in+list
                Message 7 of 11 , Sep 2, 2005
                • 0 Attachment
                  --- In perl-beginner@yahoogroups.com, "James Colannino" <perl-
                  beginner@c...> wrote:
                  > [ snip seeks if exists a match(ing) (element) between a string and
                  (in) an array ]

                  FWIW, if anyone interested:

                  http://perldoc.perl.org/search.html?q=element+in+list

                  http://perldoc.perl.org/search.html?q=find+if+element+in+list

                  Curiously, that 2nd of the two links drops the faq in question from
                  number one on the faq list down to number two on the returned list
                  (scrolled down the page to the faq portion).

                  http://perldoc.perl.org/search.html?q=find+if+match+element+in+list

                  (still at # 2)

                  I wonder why the faq's don't come up on top (if/(since?) they contain
                  the preferred way to do something?

                  Me may at times but may not at other times know what preference order
                  in which to take stuff in.

                  I suppose the answer to that^^ ^^, at this time anyways, is to know
                  the faq's well (since they lead off?) -- do they lead off?

                  If so, a bit of emphasis to that effect at above links would improve
                  for me.

                  But perhaps I've some portion of this wrong.

                  Alan.
                • merlyn@stonehenge.com
                  ... Grier Is there any chance you can be convinced to lay off the venom and just Grier ignore people you don t think have done their due diligence? I ve
                  Message 8 of 11 , Sep 2, 2005
                  • 0 Attachment
                    >>>>> "Grier" == Grier Johnson <grierj@...> writes:

                    Grier> Is there any chance you can be convinced to lay off the venom and just
                    Grier> ignore people you don't think have done their due diligence?

                    I've tried that. It seems to get worse then. People get even more lazy.

                    If I can *scare* someone into "looking into the FAQ first", in the
                    long run, my job is done.

                    A group support mailing list is only effective when the *real*
                    questions are being asked, even when it's a so-called "beginners"
                    list. I've been around the net for a long time, and I've seen it
                    happen time and time again, that when FAQ-style questions are asked on
                    a list, eventually the experts all go away in disgust.

                    THERE'S A POINT TO A FAQ. Why do you think the FAQ was invented, and
                    carefully collected? So that experts wouldn't have to answer the MOST
                    COMMON questions over and over again.

                    Not even looking in the FAQ before asking a mailing list, to me, is
                    the unacceptable behavior here. "You must be this tall" before
                    posting to the net. I'm merely *reacting* to the unacceptable
                    behavior. If you think my *reaction* is unacceptable, please look
                    back to the root cause.

                    Grier> Time would be saved on both sides (and you seem awfully concerned with
                    Grier> this) if you simple didn't reply or spent the bare amount of time
                    Grier> giving a link to the perl faq.

                    For this exchange, yes. For all exchanges overall, no. And I'm going
                    long-term on this.

                    --
                    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!
                  • Shawn Corey
                    ... Please don t respond to the trolls, it only encourages them to post more of the same. I know it s difficult not to reply but they write these things just
                    Message 9 of 11 , Sep 2, 2005
                    • 0 Attachment
                      Grier Johnson wrote:
                      > Is there any chance you can be convinced to lay off the venom and just
                      > ignore people you don't think have done their due diligence? Seeing
                      > your nasty replies is, to me, far worse then someone asking a question
                      > that's already in the faq.
                      >
                      > Time would be saved on both sides (and you seem awfully concerned with
                      > this) if you simple didn't reply or spent the bare amount of time
                      > giving a link to the perl faq.
                      >
                      > --
                      > Grier

                      Please don't respond to the trolls, it only encourages them to post more
                      of the same. I know it's difficult not to reply but they write these
                      things just to make you angry. Ignore them long enough and they'll go away.


                      --- Shawn
                    • merlyn@stonehenge.com
                      ... Shawn Please don t respond to the trolls, it only encourages them to Shawn post more of the same. I know it s difficult not to reply but Shawn they
                      Message 10 of 11 , Sep 2, 2005
                      • 0 Attachment
                        >>>>> "Shawn" == Shawn Corey <shawn.corey@...> writes:

                        Shawn> Please don't respond to the trolls, it only encourages them to
                        Shawn> post more of the same. I know it's difficult not to reply but
                        Shawn> they write these things just to make you angry. Ignore them
                        Shawn> long enough and they'll go away.

                        I presume you're talking about *him* being a troll and not me. It's
                        not my intention to make anyone angry. I do not control anyone's
                        emotions, nor pretend to. In fact, the very phrase "make you angry"
                        implies something that ultimately does not exist.

                        --
                        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!
                      Your message has been successfully submitted and would be delivered to recipients shortly.