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

Split between letters and numbers

Expand Messages
  • Dukelow, Don
    I have a scalar ($port) that can contain any one of the following hme0, eri0, gfe0, ce0. The number part can be anywhere from 0 and probably won t go any
    Message 1 of 6 , Oct 8, 2007
    • 0 Attachment
      I have a scalar ($port) that can contain any one of the following hme0,
      eri0, gfe0, ce0. The number part can be anywhere from '0' and probably
      won't go any higher then 7, but I don't want to limit it to 7 incase it
      goes to double digits someday. My problem is that I need to put a space
      between the letters and numbers. I've done it with the two lines below
      but there has got to be a better way.

      my @driver = split (/\d/, $port);
      my @instance = split (/$driver[0]/, $port");

      Also I only need the $drive and $instance not arrays. Does anyone have
      a better idea?



      -----
      Don Dukelow
      HP License Management Team
      Hewlett-Packard Company
      Cell: 1-810-728-3388
      e-mail: dukelow@...
      URL: http://www.hp.ca
    • Jeff Pinyan
      ... So $port is a string of lowercase letters followed by a number (one or more digits), and you d like to add a space between the letters and numbers? The
      Message 2 of 6 , Oct 8, 2007
      • 0 Attachment
        On 10/8/07, Dukelow, Don <dukelow@...> wrote:
        > I have a scalar ($port) that can contain any one of the following hme0,
        > eri0, gfe0, ce0. The number part can be anywhere from '0' and probably
        > won't go any higher then 7, but I don't want to limit it to 7 incase it
        > goes to double digits someday. My problem is that I need to put a space
        > between the letters and numbers. I've done it with the two lines below
        > but there has got to be a better way.
        >
        > my @driver = split (/\d/, $port);
        > my @instance = split (/$driver[0]/, $port");

        So $port is a string of lowercase letters followed by a number (one or
        more digits), and you'd like to add a space between the letters and
        numbers? The simplest way to get this done is:

        $port =~ s/([a-z])(\d)/$1 $2/;

        That regex matches a lowercase letter followed by a digit, and saves
        one to $1 and the other to $2. Then those two characters are replaced
        by themselves with a space in between the two of them.

        --
        [Mary said,] "Do whatever he tells you." ~ John 2:5
      • Dukelow, Don
        Gret idea but not quite1 Using the following code. print $port n ; $port =~ s/([a-z])( d)/$1 $2/; print $1 == $2 n ; I get the results, It drops the first
        Message 3 of 6 , Oct 8, 2007
        • 0 Attachment
          Gret idea but not quite1 Using the following code.

          print "$port\n";

          $port =~ s/([a-z])(\d)/$1 $2/;

          print "$1 == $2\n";



          I get the results, It drops the first letter. I don't understand
          either.

          ge0
          e == 0


          -----
          Don Dukelow
          HP UNIX OVO Enrollment
          Hewlett-Packard (Canada) Co.
          e-mail: dukelow@...
          URL: http://www.hp.ca <http://www.hp.ca/>




          ________________________________

          From: perl-beginner@yahoogroups.com
          [mailto:perl-beginner@yahoogroups.com] On Behalf Of Jeff Pinyan
          Sent: Monday, October 08, 2007 1:44 PM
          To: perl-beginner@yahoogroups.com
          Subject: Re: [PBML] Split between letters and numbers



          On 10/8/07, Dukelow, Don <dukelow@... <mailto:dukelow%40hp.com> >
          wrote:
          > I have a scalar ($port) that can contain any one of the following
          hme0,
          > eri0, gfe0, ce0. The number part can be anywhere from '0' and probably
          > won't go any higher then 7, but I don't want to limit it to 7 incase
          it
          > goes to double digits someday. My problem is that I need to put a
          space
          > between the letters and numbers. I've done it with the two lines below
          > but there has got to be a better way.
          >
          > my @driver = split (/\d/, $port);
          > my @instance = split (/$driver[0]/, $port");

          So $port is a string of lowercase letters followed by a number (one or
          more digits), and you'd like to add a space between the letters and
          numbers? The simplest way to get this done is:

          $port =~ s/([a-z])(\d)/$1 $2/;

          That regex matches a lowercase letter followed by a digit, and saves
          one to $1 and the other to $2. Then those two characters are replaced
          by themselves with a space in between the two of them.

          --
          [Mary said,] "Do whatever he tells you." ~ John 2:5





          [Non-text portions of this message have been removed]
        • Jeff Pinyan
          ... You re not using it correctly; don t print $1 or $2 afterwards, print $port. The code I gave changes $port for you. -- [Mary said,] Do whatever he tells
          Message 4 of 6 , Oct 8, 2007
          • 0 Attachment
            On 10/8/07, Dukelow, Don <dukelow@...> wrote:
            > Gret idea but not quite1 Using the following code.
            >
            > print "$port\n";
            >
            > $port =~ s/([a-z])(\d)/$1 $2/;
            >
            > print "$1 == $2\n";
            >
            > I get the results, It drops the first letter. I don't understand
            > either.
            >
            > ge0
            > e == 0

            You're not using it correctly; don't print $1 or $2 afterwards, print
            $port. The code I gave changes $port for you.

            --
            [Mary said,] "Do whatever he tells you." ~ John 2:5
          • Dukelow, Don
            Thank you that worked regular expressions isn t my strong point, but I m wotking on it. ... Don Dukelow HP UNIX OVO Enrollment Hewlett-Packard (Canada) Co.
            Message 5 of 6 , Oct 8, 2007
            • 0 Attachment
              Thank you that worked regular expressions isn't my strong point, but I'm
              wotking on it.


              -----
              Don Dukelow
              HP UNIX OVO Enrollment
              Hewlett-Packard (Canada) Co.
              e-mail: dukelow@...
              URL: http://www.hp.ca <http://www.hp.ca/>




              ________________________________

              From: perl-beginner@yahoogroups.com
              [mailto:perl-beginner@yahoogroups.com] On Behalf Of Jeff Pinyan
              Sent: Monday, October 08, 2007 4:25 PM
              To: perl-beginner@yahoogroups.com
              Subject: Re: [PBML] Split between letters and numbers



              On 10/8/07, Dukelow, Don <dukelow@... <mailto:dukelow%40hp.com> >
              wrote:
              > Gret idea but not quite1 Using the following code.
              >
              > print "$port\n";
              >
              > $port =~ s/([a-z])(\d)/$1 $2/;
              >
              > print "$1 == $2\n";
              >
              > I get the results, It drops the first letter. I don't understand
              > either.
              >
              > ge0
              > e == 0

              You're not using it correctly; don't print $1 or $2 afterwards, print
              $port. The code I gave changes $port for you.

              --
              [Mary said,] "Do whatever he tells you." ~ John 2:5





              [Non-text portions of this message have been removed]
            • Dukelow, Don
              This also works! $port =~ s/( D)( d)/$1 $2/; ... Don Dukelow HP UNIX OVO Enrollment Hewlett-Packard (Canada) Co. e-mail: dukelow@hp.com URL: http://www.hp.ca
              Message 6 of 6 , Oct 8, 2007
              • 0 Attachment
                This also works!
                $port =~ s/(\D)(\d)/$1 $2/;


                -----
                Don Dukelow
                HP UNIX OVO Enrollment
                Hewlett-Packard (Canada) Co.
                e-mail: dukelow@...
                URL: http://www.hp.ca <http://www.hp.ca/>




                ________________________________

                From: perl-beginner@yahoogroups.com
                [mailto:perl-beginner@yahoogroups.com] On Behalf Of Jeff Pinyan
                Sent: Monday, October 08, 2007 4:25 PM
                To: perl-beginner@yahoogroups.com
                Subject: Re: [PBML] Split between letters and numbers



                On 10/8/07, Dukelow, Don <dukelow@... <mailto:dukelow%40hp.com> >
                wrote:
                > Gret idea but not quite1 Using the following code.
                >
                > print "$port\n";
                >
                > $port =~ s/([a-z])(\d)/$1 $2/;
                >
                > print "$1 == $2\n";
                >
                > I get the results, It drops the first letter. I don't understand
                > either.
                >
                > ge0
                > e == 0

                You're not using it correctly; don't print $1 or $2 afterwards, print
                $port. The code I gave changes $port for you.

                --
                [Mary said,] "Do whatever he tells you." ~ John 2:5





                [Non-text portions of this message have been removed]
              Your message has been successfully submitted and would be delivered to recipients shortly.