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

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.

  • Frank Caggiano Level 7 Level 7 (25,695 points)

    Program  Language?

  • Phil Stokes Level 2 Level 2 (360 points)

    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 (12,980 points)

    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 (8,900 points)

    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 (0 points)

    Forgot to mention. Sorroy for that.

    I want to get Integer from string in Apple Script.

  • MichaelRow12 Level 1 Level 1 (0 points)

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

  • Phil Stokes Level 2 Level 2 (360 points)

    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 (0 points)

    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 (360 points)

    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 (360 points)

    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 (360 points)

    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 (8,900 points)

    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 (360 points)

    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 (8,900 points)

    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