Previous 1 2 3 Next 30 Replies Latest reply: Jul 25, 2014 10:43 PM by MichaelRow12
MichaelRow12 Level 1 Level 1

I have some strings which contain number and other characters, such as "1 920 pixels". What I want is to get 1920 as integer.

Thanks in advance.

Solved by Phil Stokes on Jul 22, 2014 8:44 AM Solved
Mark said: Well, you still have to turn it into an integer.  Good point. Had forgotten that in all the fun... This should do it, I think: set s to quoted form of "He's got 3 cats and 1,920 dogs and $20,000"do shell script "sed s/[a-zA-Z\\']//g <<< " & sset dx to the resultset numlist to {}repeat with i from 1 to count of words in dx  set this_item to word i of dx  try  set this_item to this_item as number  set the end of numlist to this_item  end tryend repeatnumlist
Reply by Phil Stokes on Jul 21, 2014 2:30 AM Helpful
That's right because you have to extract the part that you want as a number as a substring first. e.g., set s to "hello 1984"set substringIndex to offset of "1984" in sset substring to text substringIndex thru -1 of s as number  and going the other way: set p to "1,920 pixels"set substringIndex to offset of "0" in pset substring to text 1 thru (substringIndex) of p as number
Reply by Mark Jalbert on Jul 22, 2014 4:39 AM Helpful
Hi VikingOSX, It's not working correctly with the standard grep in OSX 10.6.Example:..100,000.27.99999 50,137.15..I'll play with it when I can find some time. This should be moved to its own thread. @ Phil Stokes      Phil Stokes wrote: ...but until the OP is clearer about what exactly he wants there's no point in covering ever possible case. I though that this had gone off topic because it didn't fit Michael's requirement. Anyway, from the shell's perspective this would meet his requirement as stated. sed 's/[^0-9]//g' <<<"1 920 pixels" Well, you still have to turn it into an integer.

All replies

  • Frank Caggiano Level 7 Level 7

    Program  Language?

  • Phil Stokes Level 2 Level 2

    set x to "1920"

    log class of x

    set y to x as number

    class of y

     

    Replies:

    (*text*)

    Result:

    integer

  • MrHoffman Level 6 Level 6

    As Frank Caggiano writes, what is your preferred programming language for this implementation?

     

    Also and somewhat more subtly, what is the string encoding?  If it's ASCII, then most available calls will work.  If you're working with Unicode or UTF-8, then you'll probably want to use different routines to parse and retrieve the data.

     

    strtok() and then atoi() in C are primitive, but would work.  Using Objective C, NSString has methods to parse and retrieve data, and to return an integer value from a string.   Python and most other languages also have conversions.  Common finite state parsers such as Ragel, Lex/Yacc or Flex/Bison can usually return integer values, as well — using a parser can sometimes be easier than hand-coding the parsing, when dealing with big wads of text data.

  • VikingOSX Level 6 Level 6
    expertise.macosx
    Mac OS X

    egrep -o "(\d{1,3},?\d{1,3},?\d{3}|\d+)" <<< "See spot 1920 run 10,000 circles around Jane 999 who has fainted 1 time."

     

    Output

     

    1920

    10,000

    999

    1

  • MichaelRow12 Level 1 Level 1

    Forgot to mention. Sorroy for that.

    I want to get Integer from string in Apple Script.

  • MichaelRow12 Level 1 Level 1

    It works well when the a string has numbers only, but in other cases, it may crash.

  • Phil Stokes Level 2 Level 2

    That's right because you have to extract the part that you want as a number as a substring first.

     

    e.g.,

     

    set s to "hello 1984"

    set substringIndex to offset of "1984" in s

    set substring to text substringIndex thru -1 of s as number

     

     

    and going the other way:

     

    set p to "1,920 pixels"

    set substringIndex to offset of "0" in p

    set substring to text 1 thru (substringIndex) of p as number


  • MichaelRow12 Level 1 Level 1

    That's my mistakes. Post this thread in a hurry and forget to describe in detail.

    As for the text encoding, it may be UTF-8 by default In Apple Script. One of the methods  is that: convert the encoding to ASCII and then compare each character with ASCII code, but it's a little inconvenient. I want to know a faster way to get number from string in Apple Script when "as integer" doesn't work (just like: "1 920 pixels" as integer)

  • Phil Stokes Level 2 Level 2

    OK, here's a handler that should do it for you.

     

    --use this line to specify the string and the number

    returnNumber("the number 1,920 can be at the start, int middle or the end", 1920)

     

     

    --place this handler at the beginning of your script

    on returnNumber(myString, aNumber)

      set aNumber to aNumber as text

      set myString to myString as text

      set substring to ""

      set removeLeading to offset of (text 1 of aNumber) in myString

      if removeLeading is greater than 1 then

      set substring to text (removeLeading) thru -1 of myString

      else

      set substring to myString

      end if

      set removeTrailing to offset of (text -1 of aNumber) in substring

      set substring to text 1 thru removeTrailing of substring as number

     

      return substring

     

    end returnNumber

  • Phil Stokes Level 2 Level 2

    Ah, now there's a flaw in that handler, which is that repeated numerals will break it (e.g, "55" will return '5').

     

    This modified handler should solve that problem, but note that you also must specify the number exactly as it is in the string and wrapped in quotes, ie "1,920" not 1920

     

    returnNumber("there are 1,920 apples in my basket", "1,920")

     

    on returnNumber(myString, aNumber)

      set aNumber to aNumber as text

      set numberCount to (count of aNumber)

      set myString to myString as text

      set substring to ""

      set removeLeading to offset of (text 1 of aNumber) in myString

      if removeLeading is greater than 1 then

      set substring to text (removeLeading) thru -1 of myString

      else

      set substring to myString

      end if

      set substring to text 1 thru numberCount of substring as number

     

      return substring

     

    end returnNumber

  • Phil Stokes Level 2 Level 2

    I like this. It 's more robust than messing around with text offsets.

     

     

    I put it into an AppleScript for the OP:

     

    set formula to "egrep -o \"(\\d{1,3},?\\d{1,3},?\\d{3}|\\d+)\" <<< \""

     

    set s to quoted form of "your string with 1920 or 55 or any other number in it"

     

    set term to "\""

    do shell script formula & s & term


     

     

    However, note one minor weakness which my revised handler above doesn't have: if the number is comma-separated like "1,920" it'll return

     

    1

    920

  • VikingOSX Level 6 Level 6
    expertise.macosx
    Mac OS X

    Phil,

     

    Another day, another RE. The following will digest any optionally punctuated integer and/or decimal less than a trillion, and handle decimal fractions (e.g. .000456) too. I made certain it handles 1,920 correctly.

     

    egrep -o "(\d+[.,]?\d+[.,]?\d+[.,]?\d+[.,]?\d?\d?|\d+[.,]?\d+|[.,]\d+)”

  • Phil Stokes Level 2 Level 2

    Cool, though you're not going to like the output of this version (Sorry!).

     

    set formula to "egrep -o \"(\\d+[.,]?\\d+[.,]?\\d+[.,]?\\d+[.,]?\\d?\\d?|\\d+[.,]?\\d+|[.,]\\d+)\" <<< \""

     

    set s to quoted form of "any number you like, (e.g., 1,920) but don't put a $ sign in front of it like $1,920 ;)"

     

    set term to "\""

    do shell script formula & s & term


  • VikingOSX Level 6 Level 6
    expertise.macosx
    Mac OS X

    You are soooo right. Fixed. Still won’t handle a space between currency symbol and number. Long enough as it is.

     

    egrep -o "([$]?\d+[.,]?[$]?\d+[.,]?[$]?\d+[.,]?[$]?\d+[.,]?\d?\d?|[$]?\d+[.,]?[$]?\d+|[$ ]?[.,]\d+)"

     

    egrep -o "([$]?\\d+[.,]?[$]?\\d+[.,]?[$]?\\d+[.,]?[$]?\\d+[.,]?\\d?\\d?|[$]?\\d+[.,]?[$] ?\\d+|[$]?[.,]\\d+)"

Previous 1 2 3 Next