Re: Replace_chars Subroutine just doesn't work for me
Re: Replace_chars Subroutine just doesn't work for me
- Subject: Re: Replace_chars Subroutine just doesn't work for me
- From: kai <email@hidden>
- Date: Sat, 25 Oct 2003 02:56:55 +0100
on Fri, 24 Oct 2003 18:12:01 -0400, Dan Doughtie wrote:
>
set MyText to content of x as string
>
>
>
my replace_chars(MyText, "5", "g")
>
my replace_chars(MyText, "m", "'")
>
my replace_chars(MyText, "l", "\"")
>
my replace_chars(MyText, "n", "\"")
>
my replace_chars(MyText, "V", " ")
>
my replace_chars(MyText, ",\"", "2")
>
my replace_chars(MyText, ".\"", ".2") --
>
my replace_chars(MyText, "3 ", "2 ")
>
my replace_chars(MyText, "\"", "3")
>
my replace_chars(MyText, ".
>
", ".
>
")
>
>
>
on replace_chars(MyText, search_string, replacement_string)
>
set AppleScript's text item delimiters to the search_string
>
set the item_list to every text item of MyText
>
set AppleScript's text item delimiters to the replacement_string
>
set MyText to the item_list as string
>
set AppleScript's text item delimiters to ""
>
return MyText
>
end replace_chars
I'm afraid you might have just stumbled across a misconception about how
subroutines actually work in AS, Dan.
The variable 'MyText' inside the subroutine isn't the same as the variable
'MyText' in the main part of the script. Variables inside a subroutine are
generally considered as local - unless they're explicitly declared as
global.
Besides one or two other abilities, even the so-called 'top-level' of a
script can be considered essentially another handler: the script's run
handler. (Indeed, it's not uncommon for top-level commands to be enclosed in
an 'on run' block - which can sometimes help to make things a bit clearer.)
So the trouble here is that you're:
1) calling the 'replace_chars' subroutine from the script's top level
2) also passing the value of (the top-level) 'MyText' to the subroutine
2) modifying the local variable 'MyText' within the subroutine
3) returning the subroutine's resulting value to the the script's top level
4) ...but, unfortunately, not actually changing anything with the result
There are a number of ways to remedy this - although, in one way or another,
they all involve modifying the value of the variable 'MyText' at the
script's top level.
One technique is to declare the variable as a global, so that it will be
accessible to all handlers. However, there's then no need to pass it's value
to the subroutine - or to return the resulting value from there:
-------------
global MyText
>
set MyText to content of x as string
replace_chars("5", "g")
replace_chars("m", "'")
replace_chars("l", "\"")
-- etc...
MyText -- show the result in the result window
on replace_chars(search_string, replacement_string)
>
set AppleScript's text item delimiters to the search_string
>
set the item_list to every text item of MyText
>
set AppleScript's text item delimiters to the replacement_string
>
set MyText to the item_list as string
>
set AppleScript's text item delimiters to ""
-- return MyText (commented out: not required in this situation)
>
end replace_chars
-------------
Bear in mind, though, that globals can also create their own kind of
confusion - especially when there are lots of them.
Another method is to set the value of 'MyText' at the top level of the
script. So, modifying your original script, you could just change:
-------------
>
my replace_chars(MyText, "5", "g")
>
my replace_chars(MyText, "m", "'")
>
my replace_chars(MyText, "l", "\"")
>
etc...
-------------
to:
-------------
set MyText to replace_chars(MyText, "5", "g")
set MyText to replace_chars(MyText, "m", "'")
set MyText to replace_chars(MyText, "l", "\"")
etc...
MyText -- show the result in the result window
-------------
However, instead of listing all those commands, I'd be tempted to consider a
value list and repeat loop - something like this:
-------------
>
on replace_chars(MyText, search_string, replacement_string)
>
set AppleScript's text item delimiters to the search_string
>
set the item_list to every text item of MyText
>
set AppleScript's text item delimiters to the replacement_string
return item_list as string -- new line (replaces 3 previous ones)
>
end replace_chars
>
set MyText to content of x as string
set repLaceList to {{"5", "g"}, {"m", "'"}, {"l", "\""}} -- etc...
repeat with r in repLaceList
set MyText to replace_chars(MyText, r's item 1, r's item 2)
end repeat
set AppleScript's text item delimiters to "" -- moved from subroutine
MyText -- show the result in the result window
-------------
(You'll see that I moved the final reset of the text item delimiters from
the subroutine to the top level - since it's required only once.)
One final thought...
Because the use of a list should condense things considerably, you might
even consider dispensing with a separate handler altogether:
-------------
>
set MyText to content of x as string
set repLaceList to {{"5", "g"}, {"m", "'"}, {"l", "\""}} -- etc...
repeat with r in repLaceList
set text item delimiters to r's item 1
set item_list to MyText's text items
set text item delimiters to r's item 2
set MyText to item_list as string
end repeat
set text item delimiters to {""}
MyText
-------------
---
kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.