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

Re: [PBML] Perl and regular expressions?

Expand Messages
  • merlyn@stonehenge.com
    ... Edward under the assumption that filename is all characters (no numbers) Edward $f= abcd123112299 ; Edward $f=~s/([ a]*)([0-9][0-9][0-9])(.*)/$1$3$2/;
    Message 1 of 12 , Mar 31, 2011
    • 0 Attachment
      >>>>> "Edward" == Edward Willekens <edwardwillekens@...> writes:

      Edward> under the assumption that filename is all characters (no numbers)
      Edward> $f="abcd123112299";
      Edward> $f=~s/([\a]*)([0-9][0-9][0-9])(.*)/$1$3$2/;
      Edward> print $f."\n";

      and /\d/ is the same as /[0-9]/, and a lot easier to type.

      --
      Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
      <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
      Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
      See http://methodsandmessages.posterous.com/ for Smalltalk discussion
    • Shlomi Fish
      Hi Randal, Edward, and all. This message is in UTF-8. ... Well, just to be a bit pedantic: d matches any digit character in Unicode. As a result: [IRC_LOG]
      Message 2 of 12 , Apr 1, 2011
      • 0 Attachment
        Hi Randal, Edward, and all.

        This message is in UTF-8.

        On Thursday 31 Mar 2011 15:43:42 merlyn@... wrote:
        > >>>>> "Edward" == Edward Willekens <edwardwillekens@...> writes:
        > Edward> under the assumption that filename is all characters (no numbers)
        > Edward> $f="abcd123112299";
        > Edward> $f=~s/([\a]*)([0-9][0-9][0-9])(.*)/$1$3$2/;
        > Edward> print $f."\n";
        >
        > and /\d/ is the same as /[0-9]/, and a lot easier to type.

        Well, just to be a bit pedantic: \d matches any digit character in Unicode. As
        a result:

        [IRC_LOG]
        Mar 25 19:42:18 <rindolf> perlbot: eval: ["٣" =~ /\d/ ? "Match" :
        "Nomatch"]
        Mar 25 19:42:21 <perlbot> rindolf: ["Match"]
        Mar 25 19:42:24 <rindolf> Yay!
        [/IRC_LOG]

        "٣" is the digit "3" in
        http://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system which is used
        in Written Arabic, among other contexts. Naturally, there are also probably
        calculator digits in Unicode, and various other variations of digits which \d
        will happily match.

        So [0-9] is sometimes a safer choice, if you wish to avoid ASCIIification
        and/or cultural imperialism.

        Give me ASCII or give me deaþ!

        Regards,

        Shlomi Fish

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        Best Introductory Programming Language - http://shlom.in/intro-lang

        Doing linear scans over an associative array is like trying to club someone to
        death with a loaded Uzi. -- Larry Wall

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      • Tim Lewis
        This may be a simple question, but I have searched for this answer. Suppose I have a multi-di array. I can make it sort by any one element. But what I need
        Message 3 of 12 , Apr 7, 2011
        • 0 Attachment
          This may be a simple question, but I have searched for this answer.
          Suppose I have a multi-di array. I can make it sort by any one element. But what I need to sort by two? In my simple example below, what if I wanted to sort by the animal name, and then by number of legs? I am grateful for any help or tips on this.

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

          my @critters; #multi-di with animal name and number of legs
          $critters[0][0] = "dog";
          $critters[0][1] = "4";
          $critters[1][0] = "cat";
          $critters[1][1] = "4";
          $critters[2][0] = "monkey";
          $critters[2][1] = "2";

          #I know I can sort by any one field with
          @critters = sort { $a->[0] cmp $b->[0] } @critters;
          my $critterCount= @critters;
          for (my $count=0;$count<$critterCount;$count++) {
          print "The critter $critters[$count][0] has $critters[$count][1] legs\n";
          }
        • merlyn@stonehenge.com
          ... Tim Suppose I have a multi-di array. I can make it sort by any one Tim element. But what I need to sort by two? In my simple example Tim below, what
          Message 4 of 12 , Apr 7, 2011
          • 0 Attachment
            >>>>> "Tim" == Tim Lewis <twlewis@...> writes:

            Tim> Suppose I have a multi-di array. I can make it sort by any one
            Tim> element. But what I need to sort by two? In my simple example
            Tim> below, what if I wanted to sort by the animal name, and then by
            Tim> number of legs? I am grateful for any help or tips on this.

            Since that can be interpreted two ways, I'm going to pretend you
            actually said "with the major sort by animal name, using the number of
            legs to break ties as a secondary sort".

            Tim> my @critters; #multi-di with animal name and number of legs
            Tim> $critters[0][0] = "dog";
            Tim> $critters[0][1] = "4";
            Tim> $critters[1][0] = "cat";
            Tim> $critters[1][1] = "4";
            Tim> $critters[2][0] = "monkey";
            Tim> $critters[2][1] = "2";

            my @sorted = sort {
            $a->[0] cmp $b->[0]
            or $a->[1] <=> $b->[1];
            } @critters;

            This is covered in the Alpaca book.

            And now, for the traditional "don't post here" comment:

            **************************************************
            *
            * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
            *
            * The official one is at beginners@..., with instructions on how to
            * join at http://lists.perl.org/list/beginners.html.
            *
            * You'll get far better response there, not to say you won't get *any* here.
            *
            **************************************************


            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
            See http://methodsandmessages.posterous.com/ for Smalltalk discussion
          • Tim Lewis
            Thanks Randall. I was close. I was using cmp instead of for the number portion. Thanks again. I did not know about the other listserver, and found this
            Message 5 of 12 , Apr 7, 2011
            • 0 Attachment
              Thanks Randall. I was close. I was using "cmp" instead of <=> for the number portion.

              Thanks again.
              I did not know about the other listserver, and found this one on Yahoo. I will check out the other one.


              Tim

              ---- merlyn@... wrote:
              > >>>>> "Tim" == Tim Lewis <twlewis@...> writes:
              >
              > Tim> Suppose I have a multi-di array. I can make it sort by any one
              > Tim> element. But what I need to sort by two? In my simple example
              > Tim> below, what if I wanted to sort by the animal name, and then by
              > Tim> number of legs? I am grateful for any help or tips on this.
              >
              > Since that can be interpreted two ways, I'm going to pretend you
              > actually said "with the major sort by animal name, using the number of
              > legs to break ties as a secondary sort".
              >
              > Tim> my @critters; #multi-di with animal name and number of legs
              > Tim> $critters[0][0] = "dog";
              > Tim> $critters[0][1] = "4";
              > Tim> $critters[1][0] = "cat";
              > Tim> $critters[1][1] = "4";
              > Tim> $critters[2][0] = "monkey";
              > Tim> $critters[2][1] = "2";
              >
              > my @sorted = sort {
              > $a->[0] cmp $b->[0]
              > or $a->[1] <=> $b->[1];
              > } @critters;
              >
              > This is covered in the Alpaca book.
              >
              > And now, for the traditional "don't post here" comment:
              >
              > **************************************************
              > *
              > * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
              > *
              > * The official one is at beginners@..., with instructions on how to
              > * join at http://lists.perl.org/list/beginners.html.
              > *
              > * You'll get far better response there, not to say you won't get *any* here.
              > *
              > **************************************************
              >
              >
              > --
              > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
              > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
              > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
              > See http://methodsandmessages.posterous.com/ for Smalltalk discussion
            Your message has been successfully submitted and would be delivered to recipients shortly.