Monday, January 23, 2017

#1 2016-12-30 10:41:00 am

Noovicio
Member
Registered: 2016-10-15
Posts: 11

ignore spaces

Hi guys
I have a minor issue (I think is minor) with a code I'm writing
I have a web form that users fill and send to us, part of the code i'm writing deals with 3 text boxes in the web form which has numbers typed by the user, the numbers could be 1, 2 , 3 etc. The problem I have is that sometimes the user,
do things that break my precious code:
1/- instead of leaving the field alone if they want nothing, they write 0
2/- or [space], [space],[space]
3/- or they write [space], [space],[space], 3

Example:
Apples: [spaces] 
Oranges: 4
Kiwis: 2

if apples > "" then
    set applesAmount to "AppX" & apples & "_"
end if
I'd like to have "AppX_" as a result if apples=0 but i'm getting "AppX   _"

If I convert the number into an integer it takes away all the spaces in case 1/ and 3/ but gives me an error in 2/
I've tried to use ignore white spaces but they are still showing. How can I solve this? I would like to avoid extra code to remove the spaces if possible.
Let me know if this is not clear, I'm trying not to confuse the thing too much....
Thanks for your help.

Offline

 

#2 2016-12-30 11:01:59 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2743

Re: ignore spaces

If I understand well, you may try :

Applescript:

set apples to " 2"
my decodeApples(apples) --> "AppX2_"

set apples to space & space & space # I use this syntax because the message editor replace groups of spaces in quotes by a single one
my decodeApples(apples) --> "AppX_"

on decodeApples(theData)
   try
       theData as integer
       set applesAmount to "AppX" & result & "_"
   on error
       set applesAmount to "AppX_"
   end try
   return applesAmount
end decodeApples

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 30 d├ęcembre 2016 18:00:10

Offline

 

#3 2016-12-30 03:01:22 pm

Noovicio
Member
Registered: 2016-10-15
Posts: 11

Re: ignore spaces

Thanks again Yvan!
One more thing; your code above works perfect, I'm just wondering how I can make this handler to work with the 3 instances needed, I need the "BCx" to change also to LHx and CSx, I could repeat it 3 times but I'm sure there is a more elegant solution:


Applescript:

my decodeAmounts(bcSets)
my decodeAmounts(lhSets)
my decodeAmounts(csSets)

on decodeAmounts(theData)
   try
       theData as integer
       set statAmount to "BCx" & result & "_"
       if theData < 1 then
           set statAmount to ""
       end if
   on error
       set statAmount to "BCx_"
   end try
   return statAmount
end decodeAmounts

Last edited by Noovicio (2016-12-30 03:02:46 pm)

Offline

 

#4 2016-12-30 03:07:42 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4160

Re: ignore spaces

Here are a couple more suggestions. Both only address the problems described:

Applescript:

set apples to " 0 "
set applesAmount to "AppX" & correctEntry(apples) & "_"

on correctEntry(thisEntry)
   set astid to AppleScript's text item delimiters
   set AppleScript's text item delimiters to space
   set thisEntry to thisEntry's text items
   set AppleScript's text item delimiters to ""
   set thisEntry to thisEntry as text
   set AppleScript's text item delimiters to astid
   if (thisEntry > "") then
       set thisEntry to thisEntry as integer as text
       if (thisEntry is "0") then set thisEntry to ""
   end if
   return thisEntry
end correctEntry

Applescript:

use AppleScript version "2.4" -- Yosemite or later needed to run the code directly.
use framework "Foundation"

set apples to " 0 "
set applesAmount to "AppX" & correctEntry(apples) & "_"

on correctEntry(thisEntry)
   set thisEntry to current application's class "NSString"'s stringWithString:(thisEntry)
   set thisEntry to thisEntry's stringByReplacingOccurrencesOfString:(" +|\\b0+") withString:("") options:(current application's NSRegularExpressionSearch) range:({location:0, |length|:thisEntry's |length|()})
   return thisEntry as text
end correctEntry


NG

Offline

 

#5 2016-12-31 04:53:57 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2743

Re: ignore spaces

Applescript:


set apString to my decodeAmounts(apSets, "APx")
set bcString to my decodeAmounts(bcSets, "BCx")
set csString to my decodeAmounts(csSets, "CSx")
set lhString to my decodeAmounts(lhSets, "LHx")

set bcString to "AP" & my grabAmount(apSets)
set bcString to "BC" & my grabAmount(bcSets)
set csString to "CS" & my grabAmount(csSets)
set lhString to "LH" & my grabAmount(lhSets)

# My preferred version. With it, the handler may be used for other prefixes and/or suffixes
set bcString to "APx" & my getAmount(apSets) & "_"
set bcString to "BCx" & my getAmount(bcSets) & "_"
set csString to "CSx" & my getAmount(csSets) & "_"
set lhString to "LHx" & my getAmount(lhSets) & "_"

on decodeAmounts(theData, thePrefix)
   try
       theData as integer
       set statAmount to thePrefix & result & "_"
       if theData < 1 then
           set statAmount to ""
       end if
   on error
       set statAmount to thePrefix & "_"
   end try
   return statAmount
end decodeAmounts

on grabAmount(theData)
   try
       theData as integer
       if theData < 1 then
           set theData to ""
       end if
       set statAmount to "x" & theData & "_"
   on error
       set statAmount to "x_"
   end try
   return statAmount
end grabAmount

on getAmount(theData)
   try
       theData as integer
       if theData < 1 then
           set theData to ""
       end if
   on error
       set theData to "_"
   end try
   return theData
end getAmount

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 31 d├ęcembre 2016 11:53:43

Last edited by Yvan Koenig (2016-12-31 07:43:02 am)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.042 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)