Topic
  • 16 replies
  • Latest Post - ‏2013-03-07T19:05:31Z by llandale
vinaykumarshankar
vinaykumarshankar
1 Post

Pinned topic Extracting Integers from a string

‏2012-10-05T05:48:55Z |
Hi I am new to DXL. Can we possible to extact the integres from the string..
For example: Consider a string:
str = "0001,0002,003,0004.."
Can i extract the integer values 0001, 0002,0003.... to the variable of type int.
i.e. variabls a,b,c are type integer.
can we assign a = 0001, b = 0002 c = 0003 values extracted from the above string.
Updated on 2013-03-07T19:05:31Z at 2013-03-07T19:05:31Z by llandale
  • Mathias Mamsch
    Mathias Mamsch
    1983 Posts

    Re: Extracting Integers from a string

    ‏2012-10-05T21:37:13Z  

    Easy. With a regexp you can find all numbers in a string:
     

    Regexp r = regexp "[0-9]+"
     
    int pos = 0 
     
    string s = "0123, 3456, 12"
     
    // create a buffer so we can iterate over all matches efficiently
    Buffer buf = create(); buf = s 
     
    while (search(r, buf, pos)) {
       int nr = intOf realOf s[pos+start 0:pos+end 0]
       print nr // do whatever you like with the nr
       pos += 1 + end 0 
    }
     
    delete buf
    

     


    Maybe that helps, regards, Mathias

     

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Updated on 2014-01-06T21:52:03Z at 2014-01-06T21:52:03Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-04T09:34:55Z  

    Easy. With a regexp you can find all numbers in a string:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">Regexp r = regexp "[0-9]+" int pos = 0 string s = "0123, 3456, 12" // create a buffer so we can iterate over all matches efficiently Buffer buf = create(); buf = s while (search(r, buf, pos)) { int nr = intOf realOf s[pos+start 0:pos+end 0] print nr // do whatever you like with the nr pos += 1 + end 0 } delete buf </pre>

     


    Maybe that helps, regards, Mathias

     

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Hello Mathias,

    Thanks for your post reg how to extract the numeric data to strings.

    I have a string like
    s= "N11.03N11.02N10.00N11.00N12.00"

    I want to extract the string like the o/p below

    1103
    1102
    1000
    1100
    1200

    Each integer needs to stored and it needs to be compared with other string/int
  • Mathias Mamsch
    Mathias Mamsch
    1983 Posts

    Re: Extracting Integers from a string

    ‏2013-02-04T12:46:26Z  
    Hello Mathias,

    Thanks for your post reg how to extract the numeric data to strings.

    I have a string like
    s= "N11.03N11.02N10.00N11.00N12.00"

    I want to extract the string like the o/p below

    1103
    1102
    1000
    1100
    1200

    Each integer needs to stored and it needs to be compared with other string/int

    So which part of it is the problem? For the extraction of the numbers, you can use the same code as above, just a different regular expression. Then inside the search loop you will convert to Integer and put the numbers to a Skip list.

    int val = intOf ( realOf (strYouIntegerString) )
    put (yourSkip, val, val)
    

     


    Afterwards you can do what you want with those numbers. So where do you need help? Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2014-01-06T21:52:38Z at 2014-01-06T21:52:38Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-04T13:56:19Z  

    So which part of it is the problem? For the extraction of the numbers, you can use the same code as above, just a different regular expression. Then inside the search loop you will convert to Integer and put the numbers to a Skip list.

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">int val = intOf ( realOf (strYouIntegerString) ) put (yourSkip, val, val) </pre>

     


    Afterwards you can do what you want with those numbers. So where do you need help? Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Danke Mathias.
    Can you give me the Regular expression syntax?

    I have used the similar syntax but it didnt work.

    If you dont mind, can you provide me the Regular expression, and how to club the numeric data and then compare by example?
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-04T13:59:24Z  
    Danke Mathias.
    Can you give me the Regular expression syntax?

    I have used the similar syntax but it didnt work.

    If you dont mind, can you provide me the Regular expression, and how to club the numeric data and then compare by example?
    I have used the regular expression
    Regexp r = regexp "0-9" to extract the numbers.
    But each number is printed in one line, but I need to club the int like 1103 or 1102 from N11.03.

    how to put the numbers in one string? Sorry I am very new to DXL and also coding.
  • Mathias Mamsch
    Mathias Mamsch
    1983 Posts

    Re: Extracting Integers from a string

    ‏2013-02-04T20:22:23Z  
    I have used the regular expression
    Regexp r = regexp "0-9" to extract the numbers.
    But each number is printed in one line, but I need to club the int like 1103 or 1102 from N11.03.

    how to put the numbers in one string? Sorry I am very new to DXL and also coding.

    Your regular expression would be something like:
     

    //                    match 1     match 2
    //                  N (digits) . (digits)
    Regexp re = regexp "N([0-9]+)\\.([0-9]+)
     
    // ...
     
    // concatenate match 1 and match 2
    string sMyNumberString = s[pos+start 1:pos+end 1] s[pos+start 2:pos+end 2]
    int myNumber = intOf realOf sMyNumberString
    

     


    Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Updated on 2014-01-06T21:53:08Z at 2014-01-06T21:53:08Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-05T11:11:29Z  

    Your regular expression would be something like:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">// match 1 match 2 // N (digits) . (digits) Regexp re = regexp "N([0-9]+)\\.([0-9]+) // ... // concatenate match 1 and match 2 string sMyNumberString = s[pos+start 1:pos+end 1] s[pos+start 2:pos+end 2] int myNumber = intOf realOf sMyNumberString </pre>

     


    Regards, Mathias

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

    Guten Tag Mathias.

    Your input really worked. Thanks a lot.
    First while loop is taking and that only for one Object in the module.
    Second while is not taking at all.

    My intention is to extract the Numeric data from attribute Release and Tar.

    Then I wanted to compare the numeric data from those attributes if it is <= 1103 and then set the value implemented.

    I tried by removing the while loop and made it with if condition.
    Then it is taking only first numeric data from the mentioned attributes.

    Please help me how to extract all the data for a attribute of Object and then compare.

    Danke SChon

    Attachments

  • llandale
    llandale
    2985 Posts

    Re: Extracting Integers from a string

    ‏2013-02-05T15:58:15Z  
    Guten Tag Mathias.

    Your input really worked. Thanks a lot.
    First while loop is taking and that only for one Object in the module.
    Second while is not taking at all.

    My intention is to extract the Numeric data from attribute Release and Tar.

    Then I wanted to compare the numeric data from those attributes if it is <= 1103 and then set the value implemented.

    I tried by removing the while loop and made it with if condition.
    Then it is taking only first numeric data from the mentioned attributes.

    Please help me how to extract all the data for a attribute of Object and then compare.

    Danke SChon
    pos=0 before BOTH while loops.
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-06T06:37:35Z  
    • llandale
    • ‏2013-02-05T15:58:15Z
    pos=0 before BOTH while loops.
    I could extract the All the data.

    But how to remove the carraige return ( newline character from the extracted string) and keep the data to array as integer?

    Currently the o/p is like below:

    1103
    1102
    1201
    1000

    I would like to have the above data stored in array like : { 1103,1102,1201,1000}
  • Mathias Mamsch
    Mathias Mamsch
    1983 Posts

    Re: Extracting Integers from a string

    ‏2013-02-07T09:08:20Z  
    I could extract the All the data.

    But how to remove the carraige return ( newline character from the extracted string) and keep the data to array as integer?

    Currently the o/p is like below:

    1103
    1102
    1201
    1000

    I would like to have the above data stored in array like : { 1103,1102,1201,1000}

    From the above post you already see how to convert the number strings to integers using intOf realOf. Now you can just store them in an array like this:
     

    Array arNumbersFound = create(10,2) // array will atomatically resize, so do not worry about the overall size
    int iFound = 0
     
    // ...
     
     
    // in your search loop: 
    int val = intOf realOf myStringValue
    put (arNumbersFound, val, iFound, 0 /* = first column */) 
    iFound++
     
     
    // Later to iterate over the array: 
    int i
    print "Values: " 
     
    for (i = 0; i < iFound; i++)  {
      int value = (get(arNumbersFound, i, 0 /* = first column */) 
      if (i != 0) print ", "  // print or do whatever you like with the numbers
      print value "" 
    }
    

     


    Regards, Mathias

     

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Updated on 2014-01-06T21:53:50Z at 2014-01-06T21:53:50Z by iron-man
  • Mathias Mamsch
    Mathias Mamsch
    1983 Posts

    Re: Extracting Integers from a string

    ‏2013-02-07T09:08:53Z  

    From the above post you already see how to convert the number strings to integers using intOf realOf. Now you can just store them in an array like this:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">Array arNumbersFound = create(10,2) // array will atomatically resize, so do not worry about the overall size int iFound = 0 // ... // in your search loop: int val = intOf realOf myStringValue put (arNumbersFound, val, iFound, 0 /* = first column */) iFound++ // Later to iterate over the array: int i print "Values: " for (i = 0; i < iFound; i++) { int value = (get(arNumbersFound, i, 0 /* = first column */) if (i != 0) print ", " // print or do whatever you like with the numbers print value "" } </pre>

     


    Regards, Mathias

     

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Whoops it should be:
     

    int value = (get(arNumbersFound, i, 0 /* = first column */) int
    

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Updated on 2014-01-06T21:54:21Z at 2014-01-06T21:54:21Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-07T10:07:49Z  

    Whoops it should be:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">int value = (get(arNumbersFound, i, 0 /* = first column */) int </pre>

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    vielen danke Herr Mathias.
    Ihre Eingabe sind sehr hilfreich.
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-12T10:54:31Z  

    Whoops it should be:
     

    <pre class="javascript dw" data-editor-lang="js" data-pbcklang="javascript" dir="ltr">int value = (get(arNumbersFound, i, 0 /* = first column */) int </pre>

     

     


    Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

     

     

    Mathias, I have tried executing the script with your inputs.
    Script is running for long and its stopping after sometime.

    Even if I see the o/p, the extracted values are getting printed against each Object ID.

    Attached you can find the DXL script.
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-02-26T06:25:08Z  
    Mathias, I have tried executing the script with your inputs.
    Script is running for long and its stopping after sometime.

    Even if I see the o/p, the extracted values are getting printed against each Object ID.

    Attached you can find the DXL script.
    With thte DXL script i have posted earlier, I could extract the numeric data, but the extracted numeric data is being listed for each and every object in the o/p panel.

    Would someone help me to resolve the issue so that extracted data is stored in the array for that partcular Object ID, so that I can compare the data and set the other attribute.
  • SystemAdmin
    SystemAdmin
    3180 Posts

    Re: Extracting Integers from a string

    ‏2013-03-06T10:01:20Z  
    With thte DXL script i have posted earlier, I could extract the numeric data, but the extracted numeric data is being listed for each and every object in the o/p panel.

    Would someone help me to resolve the issue so that extracted data is stored in the array for that partcular Object ID, so that I can compare the data and set the other attribute.
    Would anyone help me to resolve the error?

    DOORS is getting hanged if I run the DXL script attached to the post
  • llandale
    llandale
    2985 Posts

    Re: Extracting Integers from a string

    ‏2013-03-07T19:05:31Z  
    Would anyone help me to resolve the error?

    DOORS is getting hanged if I run the DXL script attached to the post
    I'm lazy and forgetful and not re-reading the thread, just looking at this code.
    • use "regexp2" not "regexp"
    • I think you need to "setempty" both Buffers at the top of the loop, in case your atts have null characters.
    • I notice you create the Arrays with 2 rows, but only use row #0.
    • "print value2" and the previous print need to move up into the loop.

    I notice your Arrays contain every found Reference for every object, specifically including duplicates. I notice 2 print statements per reference. print statements take expontially more time each time you print (the 1000th print is much slower than the 1st), and this may be your problem if you have 1000 or more objects.

    You could have a bufResults and change "print" to "bufResults +=", then do a single massive "print tempStringOf(bufResults)" at the bottom.

    -Louie

    Use an editor that lets you see tabs and spaces.