Saturday, October 1, 2022

#1 2022-09-18 03:32:33 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2531

Converting text list to numbers list

I wrote following script to be able convert text list to numbers list:

Applescript:


my makeNumericList:{"10.5", "3.14", "5", "1"}

on makeNumericList:stringsList
   set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
   set numericList to (run script ("{" & stringsList & "}"))
   set AppleScript's text item delimiters to ATID
   return numericList
end makeNumericList:

It is very fast and works correctly when current decimal separator of user is ".", but returns wrong list when separator is other.

Any suggestion to make it to work with any current decimal separator for numbers, without using repeat loop?

Last edited by KniazidisR (2022-09-18 03:34:21 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2022-09-18 08:16:40 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5568

Re: Converting text list to numbers list

This might do it if the strings don't contain thousands separators:

Applescript:


my makeNumericList:{"10.5", "3,14", "123 7", "5", "1"}

on makeNumericList:stringsList
   set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "?"}
   set {numberText, AppleScript's text item delimiters} to {stringsList as text, {".", ",", space}}
   set {stringsList, AppleScript's text item delimiters} to {numberText's text items, middle character of (0.0 as text)}
   set {numberText, AppleScript's text item delimiters} to {stringsList as text, "?"}
   set {stringsList, AppleScript's text item delimiters} to {numberText's text items, ","}
   set {numericList, AppleScript's text item delimiters} to {(run script ("{" & stringsList & "}")), ATID}
   return numericList
end makeNumericList:


NG

Offline

 

#3 2022-09-18 08:47:12 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2531

Re: Converting text list to numbers list

@Nigel Garvey,

I confirm that it works exactly as I would like. Thousand separators don't really interest me at the moment. Thanks a lot.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2022-09-18 11:10:18 am

robertfern
Member
Registered: 2011-11-29
Posts: 202

Re: Converting text list to numbers list

Here's my version

Applescript:

my makeNumericList:{"10,5", "3,14", "5", "1"}

on makeNumericList:stringsList
   set {ATID, text item delimiters} to {text item delimiters, "\" as number,\""}
   set numericList to (run script ("{\"" & stringsList & "\" as number}"))
   set text item delimiters to ATID
   return numericList
end makeNumericList:

Last edited by robertfern (2022-09-18 11:14:25 am)

Offline

 

#5 2022-09-18 12:56:36 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2531

Re: Converting text list to numbers list

robertfern wrote:

Here's my version

Applescript:

my makeNumericList:{"10,5", "3,14", "5", "1"}

on makeNumericList:stringsList
   set {ATID, text item delimiters} to {text item delimiters, "\" as number,\""}
   set numericList to (run script ("{\"" & stringsList & "\" as number}"))
   set text item delimiters to ATID
   return numericList
end makeNumericList:



Thanks, but

this handler throws error for the  users whose current decimal separator is "." instead of ","
It should be refined somewhat.

Last edited by KniazidisR (2022-09-18 12:57:13 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2022-09-18 01:20:15 pm

robertfern
Member
Registered: 2011-11-29
Posts: 202

Re: Converting text list to numbers list

I didn’t see that as an issue, since why would I have a list with the other decimal seperator in my list in the first place.
That is why my list contains commas to test the systems with a comma seperator.

But I see your point

Last edited by robertfern (2022-09-18 01:22:08 pm)

Offline

 

#7 2022-09-18 01:30:39 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2531

Re: Converting text list to numbers list

robertfern wrote:

I didn’t see that as an issue, since why would I have a list with the other decimal seperator in my list in the first place.


The numeric text list is returned with different decimal separators by different shell commands or from the webpages. The handler should take this fact into the attention.

Last edited by KniazidisR (2022-09-18 01:31:42 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)