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

Re: [PBML] 0..99 operator

Expand Messages
  • Charles K. Clarkson
    eventualdeath ... Er, it doesn t use the range operator, but: if (int $a = 0 or int $a
    Message 1 of 8 , Oct 30, 2001
    • 0 Attachment
      "eventualdeath" <eventualdeath@...>


      : I would appreciate very much if members could tell me
      : an easier way to write the undermentioned.
      : I could not figure out how to intergrate this
      : operator 0..99 into the system.
      :
      : if ($a==0 or $a==1 or $a==2 .......... all the way up to $a==99)

      Er, it doesn't use the range operator, but:

      if (int $a >= 0 or int $a < 100 ) {

      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.


      Biologists have a word for something that doesn’t change: dead.
    • eventualdeath
      Thanks Charles.Indeed a good programmer must not have a one track mind like me. I must admit that I am still far away. Anyway I ask about the range operator
      Message 2 of 8 , Oct 31, 2001
      • 0 Attachment
        Thanks Charles.

        Indeed a good programmer must not have a one track mind like me. I must
        admit that I am still far away. Anyway I ask about the range operator
        because sometimes we may even want to do things like this :-

        if ($a eq a or $a eq b or $a eq c .... all the way up to $a eq z){ }; it
        would be great if I could use the range operator or an easier way. Please
        advice.

        Thank You!

        ----- Original Message -----
        From: "Charles K. Clarkson" <cclarkson@...>
        To: <perl-beginner@yahoogroups.com>
        Sent: Wednesday, October 31, 2001 2:53 PM
        Subject: Re: [PBML] 0..99 operator


        > "eventualdeath" <eventualdeath@...>
        >
        >
        > : I would appreciate very much if members could tell me
        > : an easier way to write the undermentioned.
        > : I could not figure out how to intergrate this
        > : operator 0..99 into the system.
        > :
        > : if ($a==0 or $a==1 or $a==2 .......... all the way up to $a==99)
        >
        > Er, it doesn't use the range operator, but:
        >
        > if (int $a >= 0 or int $a < 100 ) {
        >
        > HTH,
        > Charles K. Clarkson
        > Clarkson Energy Homes, Inc.
        >
        >
        > Biologists have a word for something that doesn’t change: dead.

        That's the reason for my email handle. However, with the aggrresive studies
        of life science, I have a certain feeling that one day, death could be a
        disease and if it is classified as a disease, then it could be cured.


        _________________________________________________________
        Do You Yahoo!?
        Get your free @... address at http://mail.yahoo.com
      • Charles K. Clarkson
        ... The problem with the approach is it will necessitate 26 comparisons if $a eq z . The benefit may be that it would allow weird things like ( 1.. 13, a ..
        Message 3 of 8 , Oct 31, 2001
        • 0 Attachment
          "eventualdeath" <eventualdeath@...> wrote:

          : Thanks Charles.
          :
          : Indeed a good programmer must not have a one track mind
          : like me. I must admit that I am still far away. Anyway
          : I ask about the range operator because sometimes we may
          : even want to do things like this :-
          :
          : if ($a eq a or $a eq b or .... all the way up to $a eq z){ };
          : it would be great if I could use the range operator or
          : an easier way. Please advice.
          :

          The problem with the approach is it will necessitate
          26 comparisons if $a eq 'z'. The benefit may be that
          it would allow weird things like ( 1.. 13, a .. f, x .. z).
          So let's try a subroutine:


          sub compare {
          # First we pass the value to be compared:
          my $value = shift;

          # Let's pass the range as a reference in
          # case it's a really big range:
          my $range = shift;

          # Let's get a little fancy here and let
          # perl do the matching:
          my %match;

          # We can dereference $range with @$range.
          # We can use a hash slice to fill the hash
          # with an array of 1's:
          @match{@$range} = (1) x @$range;

          # if we have a match return 1 (true)
          return 1 if $match{$value};

          # else return 0 (false)
          return 0;
          }

          __END__

          And to test it:
          #!/usr/bin/perl
          use strict;
          use warnings;
          use diagnostics;

          sub compare;

          my $a = 8;
          print compare $a, [1 .. 9];

          $a = 'qq';
          print "\n\n$a is in range\n\n" if compare $a, ['aa' .. 'zz'];


          I believe Quantum::Superposition also covers this.


          HTH,
          Charles K. Clarkson
          Clarkson Energy Homes, Inc.


          The vast majority of the Earth is underground.
          - R. Zubrin
        • Jenda Krynicky
          From: eventualdeath ... While you could use the .. operator like this : if (grep {$a eq $_} ( a .. z ) ) { ... it s
          Message 4 of 8 , Nov 1, 2001
          • 0 Attachment
            From: "eventualdeath" <eventualdeath@...>
            > Indeed a good programmer must not have a one track mind like me. I
            > must admit that I am still far away. Anyway I ask about the range
            > operator because sometimes we may even want to do things like this :-
            >
            > if ($a eq a or $a eq b or $a eq c .... all the way up to $a eq z){ };
            > it would be great if I could use the range operator or an easier way.
            > Please advice.

            While you could use the .. operator like this :

            if (grep {$a eq $_} ('a' .. 'z') ) { ...

            it's usualy not the best way to do it. In the case with the letters I'd
            use

            if ($a =~ /^[a-z]$/) { ...

            but that's me ... I see regexps everywhere.

            Jenda

            =========== Jenda@... == http://Jenda.Krynicky.cz ==========
            There is a reason for living. There must be. I've seen it somewhere.
            It's just that in the mess on my table ... and in my brain.
            I can't find it.
            --- me
          • eventualdeath
            ... _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
            Message 5 of 8 , Nov 8, 2001
            • 0 Attachment
              ----- Original Message -----
              From: "Jenda Krynicky" <Jenda@...>

              > it's usualy not the best way to do it. In the case with the letters I'd
              > use
              > if ($a =~ /^[a-z]$/) { ...

              I've just completed studying some basics of regexp and now here I am. I've
              confirmed that without the ^ and $ sign, $a is still being tested against
              (a..z). such as this:- if ($a=~/[a-z]/){ print "true" } else {print
              "false"};

              Since we are comparing $a against a..z , why do you put the carret ^
              (signifying beginning ) and the dollar sign $ (last). Is there some safety
              catch.

              Thanks



              _________________________________________________________
              Do You Yahoo!?
              Get your free @... address at http://mail.yahoo.com
            • Frank J. Schmuck
              I think I understand how SendMail works but I d like to retrieve e-mail remotely. Is there a perl module that does this? Thanks Frank [Non-text portions of
              Message 6 of 8 , Nov 9, 2001
              • 0 Attachment
                I think I understand how SendMail works but I'd like to retrieve e-mail
                remotely. Is there a perl module that does this?

                Thanks
                Frank



                [Non-text portions of this message have been removed]
              • Jenda Krynicky
                From: eventualdeath ... This depends on what exactly do you want to match. If you only want to match one letter strings
                Message 7 of 8 , Nov 9, 2001
                • 0 Attachment
                  From: "eventualdeath" <eventualdeath@...>

                  > ----- Original Message -----
                  > From: "Jenda Krynicky" <Jenda@...>
                  >
                  > > it's usualy not the best way to do it. In the case with the letters
                  > > I'd use if ($a =~ /^[a-z]$/) { ...
                  >
                  > I've just completed studying some basics of regexp and now here I am.
                  > I've confirmed that without the ^ and $ sign, $a is still being tested
                  > against (a..z). such as this:- if ($a=~/[a-z]/){ print "true" } else
                  > {print "false"};
                  >
                  > Since we are comparing $a against a..z , why do you put the carret ^
                  > (signifying beginning ) and the dollar sign $ (last). Is there some
                  > safety catch.

                  This depends on what exactly do you want to match.

                  If you only want to match one letter strings containing a lower case
                  letter than /^[a-z]$/ is the right one.

                  If on the other hand you want to match any string that contains a
                  lowercase letter (so that even "12545a6878" would match) then you
                  want /[a-z]/.

                  If you want the lowercase at the beginning you'll use /^[a-z]/ and
                  and if you want it at the end you'll have /[a-z]$/.

                  Now ... if it's guaranteed that the string always only contains just
                  one character, than all those give the same result. If it's not ... they
                  are very different.

                  Jenda

                  =========== Jenda@... == http://Jenda.Krynicky.cz ==========
                  There is a reason for living. There must be. I've seen it somewhere.
                  It's just that in the mess on my table ... and in my brain.
                  I can't find it.
                  --- me
                • Greg
                  ... mail ... Yes. If its a POP3 account, then you need POP3Client. For SMTP you need something else. Greg
                  Message 8 of 8 , Nov 9, 2001
                  • 0 Attachment
                    --- In perl-beginner@y..., "Frank J. Schmuck" <fschmuck@c...> wrote:
                    > I think I understand how SendMail works but I'd like to retrieve e-
                    mail
                    > remotely. Is there a perl module that does this?
                    >
                    > Thanks
                    > Frank

                    Yes. If its a POP3 account, then you need POP3Client.
                    For SMTP you need something else.

                    Greg
                  Your message has been successfully submitted and would be delivered to recipients shortly.