Page 1 of 1

myReplace function for accentued chars [Solved]

Posted: Mon Jun 02, 2014 1:01 pm
by atout66
Hi to all,
I want to replace chars in a string by special chars for french language (and others).
I already know the internal function Replace like:

Code: Select all

replace "À" with "À" in laChaine -- Alt + 0192
but I've noticed that this function is not stabble and don't do the job always very well.
So I decided to build mine; here is what I do:

Code: Select all

local tArray -- We keep each char of <laChaine> into that array
     repeat with xxx = 1 to the number of chars of laChaine -- <laChaine> is a string which can be long
          put char xxx of laChaine into tArray[xxx]          
     end repeat
     repeat for each line xxx in the keys of tArray
          put tArray[xxx] into laLettre -- <laLettre> is a char of the string <laChaine> 
          switch laLettre
               case "à"
                    put "Ã" into tArray[xxx]
                    break
               case "À" // Alt + 0192
                    put "À" into tArray[xxx] 
                    break
Etc.
Until here, everything is OK. The problem is when I want to reconstruct the string <laChaine> like below:

Code: Select all

 repeat for each line xxx in the keys of tArray
          put tArray[xxx] into laLettre
          put laLettre after leTexte          
     end repeat
What I get is a strange order in the string.
For example if the string is "Nom commun", with the reverse process, I get "Nom commnu" :?:
In the debugger, I can notice that the repeat loop read the key "Nom commun" of tArray like 1,2,3,4,5,6,7,8,10,9 :!:
If they are some <cr> in the string, like <line1><line2><line3>, I get <line1><line3><line2> :?:
Any idea what's wrong in this process ?

Thanks in advance, Jean-Paul.

Re: myReplace function for accentued chars

Posted: Mon Jun 02, 2014 2:00 pm
by atout66
After some reading in the dictionary, specially the Note part, here is how I've done to avoid the strange order returned by the repeat loop:

Code: Select all

 put 0 into leCmp
     repeat for each line xxx in the keys of tArray
          add 1 to leCmp
          put tArray[leCmp] into laLettre
          put laLettre after leTexte          
     end repeat
As explained in the dictionary, LC doen't index the keys like other language.
That's the reason why I've add the integer var <leCmp>

Re: myReplace function for accentued chars

Posted: Mon Jun 02, 2014 2:01 pm
by Klaus
Bonjour Jean-Paul,

as you have noticed, the keys of an array are not in teh "correct" order!
Therfor you need to do something like this:

Code: Select all

...
local tArray -- We keep each char of <laChaine> into that array
     repeat with xxx = 1 to the number of chars of laChaine -- <laChaine> is a string which can be long
          put char xxx of laChaine into tArray[xxx]          
     end repeat

    ## Trick starts here: :-)
    put the keys of tArray into tKeys

    ## Now FORCE the correct order:
    sort lines of tKeys numeric

   ## Now continue with the correct order:
    repeat for each line xxx in tKeys
...
Best

Klaus

Re: myReplace function for accentued chars

Posted: Mon Jun 02, 2014 2:10 pm
by atout66
Hi Klaus,

Thanks for your trick but it is not the right one :wink:
In fact if the keys of the array are:
1,2,3,4,5,6,7,8,9,10, and you sort them by numeric order I get
1,2,3,4,5,6,7,8,10,9 ALWAYS! (don't ask me why, I already got crasy with it :shock: )
If I sort them just like :
sort lines of tKeys
I get 1,10,2,3,4,5,6,7,8,9
That's why I had to add this */%$* var <leCmp> 8)

Best regards, Jean-Paul.

Re: myReplace function for accentued chars [Solved]

Posted: Mon Jun 02, 2014 2:19 pm
by Klaus
Hmmm, just made a little test with this script:

Code: Select all

on mouseUp
   repeat with i = 1 to 10
      put "xxx" into tArray[i]
   end repeat
   put the keys of tArray into tKeys1
   put tKeys1 into tKeys2
   sort tKeys2 numeric
   put tKeys1 & CR & CR & tKeys2
end mouseUp
and here is the CORRECT and EXSPECTED poutput:
1
2
3
4
5
6
7
8
10
9

1
2
3
4
5
6
7
8
9
10

Sorting always works as exspected, so please show your data and scripts! :D

Re: myReplace function for accentued chars [Solved]

Posted: Mon Jun 02, 2014 3:00 pm
by atout66
arght, you're right (once again) :wink:
I wrote: sort tKeys2 by numeric
intead of : sort tKeys2 numeric

Thanks for your help, Jean-Paul.