extract numbers and text from a long string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kam5438mak
    New Member
    • Oct 2013
    • 2

    extract numbers and text from a long string

    I have a long string from which I would like to separate words and the next numbers and store them in arrays if possible

    String = aa12ab123ac34ad 23ae321ca21cb23 4za123zb12zd12

    text usually is 2 characters long but the digits vary in length 2-4 digits
    I would like to store them as
    alpha[1]=aa
    number[1]=12
    alpha[2]=ab
    number[2]=123
    .
    .
    .
    alpha[n]=zd
    number[n]=12
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    We would like to help you troubleshoot your code, but you first need to show us that code and what it outputs and how that output differs from what you expected.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Consider using regular expressions to retrieve letter and number groupings.

      -Frinny

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        And consider using the push function to add the groups to the arrays.

        Comment

        • kam5438mak
          New Member
          • Oct 2013
          • 2

          #5
          Had fun Figuring it out myself. I just changed the logic a bit to use 2 arrays but skipping the i values in the for loop.

          Code:
           my $data = 'aa12ab123ac34ad23ae321ca21cb234za123zb12zd12'
            my @values1 = split(/(\d+)/, $data);
            my @values2 = split(/(\d+)/, $data);
          
          for (my $i=0; $i < @values1; $i++) {
            if ($values1[$i] eq "aa"){
             if ($values2[$i+1] == 0){
                print "Yay\n";
                 }
              }
           }
          Not the best way i know, but it does the job. I wanted separate arrays for string and number earlier.

          I might ask questions on tk or tkx as I intend to display this parser's result on gui. Thanks.

          If you guys can think of cleaner/simpler method, please let me know. Kindly try to give a code snippet, if possible. Else point to a topic :)
          Last edited by Rabbit; Oct 25 '13, 05:02 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            It's not quite what we recommended but it solves the problem. There's always more than one solution to the problem :)

            -Frinny

            Comment

            • sathishkumar se
              New Member
              • Dec 2011
              • 10

              #7
              use the following code
              Code:
              my $data = 'aa12ab123ac34ad23ae321ca21cb234za123zb12zd12';
              while($data =~ m/([a-z]+)(\d+)/igs)
              {
              	print "alpha  :: $1\n";
              	print "Number :: $2\n";
              }

              Comment

              Working...