Delete from a variable

Hi Folks,

how can I delete a string from a string?

f.ex:

the variable “a” is: “My Name is Stefan, and I am a total applescript noob”

the variable “b” should be elliminate “noob”

What can I do, that only “My Name is Stefan, and I am a total applescript” is present in the variable “a”

Thanks for your help,

Stefan

Hi

Does this work for you!!

set a to "My Name is Stefan, and I am a total applescript noob"
set b to "noob"
set noobgone to ""
set OldDelims to AppleScript's AppleScript's text item delimiters
set AppleScript's AppleScript's text item delimiters to "noob"
set newA to text items of a
set AppleScript's AppleScript's text item delimiters to ""
set newA to newA as text
set AppleScript's AppleScript's text item delimiters to OldDelims
return newA

I’d suggest something similar to what pidge1 posted:

set a to "My Name is Stefan, and I am a total applescript noob"
set a to simpleReplace("noob", "", a)

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

Hi Pidge, Hi Bruce,

thanks a lot - works nearly perfect…

Why nearly?

I read from a file - there are 2000 lines - with the text delimiter I identify one
line and delete it (from the variable) - with the code you have built for me!

When I read the variable again I see that there is an empty line where I
previous deleted the “b” - Is it possible to not only delete but also make
something like a “delete line”?

Thanks for your help,

Stefan

Just add a newline to the end of the search term:

set a to "My Name is Stefan, and I am an applescript noob
master after going on the forums."
set a to simpleReplace("noob
", "", a)

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

The problem with this is that it won’t replace occurances of the word when it doesn’t have a newline after it. Instead, you may want to just add a second pass where you search for two newlines in a row and replace them with a single one. This would eliminate any empty lines.

Hi Experienced Noob…

thanks a lot for your help - It is working fine - but not exactly what should happen…

I will show you the results:

first the code:


set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"

set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"

set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to (read theFile)
display dialog del_list

set a to del_list
set a to simpleReplace(delete_adressbook, "", a)

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

display dialog del_list looks like this:

“Lehrner;Stefan;+4367682007038;
Lehrner;Bianca;+436505700550;
Lehrner;Laetitia;+436505800550;
Egon;Hugo;+543332233;
Mathaisl;Bernhard;+4367682007179;
Osterholt;Roland;+4917199999998;
von Irgendwo;Irgendwer;+491111111;
Klaus;Maria;+436666666;
Kraller;Leo;+4367682006720;
Trnka;Erich;+4367682005555;
Mayer;Peter;+436555000555;
Wegscheider;Guido;+4367682007418;”

return subject looks like this:

"Lehrner;Stefan;+4367682007038;
Lehrner;Bianca;+436505700550;

Egon;Hugo;+543332233;
Mathaisl;Bernhard;+4367682007179;
Osterholt;Roland;+4917199999998;
von Irgendwo;Irgendwer;+491111111;
Klaus;Maria;+436666666;
Kraller;Leo;+4367682006720;
Trnka;Erich;+4367682005555;
Mayer;Peter;+436555000555;
Wegscheider;Guido;+4367682007418;"

Laetita is gone - so far so good - but how can I delete this empty line?

Thanks for your help,

Stefan

May I can use:

set ctrl to ASCII character 127

Thanks for your help…

Stefan

Hi Stefan,

the problem is, you replace the line, but not the line break.
I would do it a different way

set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"

set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"

set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
repeat with i from 1 to count del_list
	if item i of del_list is delete_adressbook then
		set del_list to delete_list_item(del_list, i)
		exit repeat
	end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list

on delete_list_item(theList, theItem)
	if theItem is 1 then
		return rest of theList
	else if theItem is (count theList) then
		return items 1 thru -2 of theList
	else
		return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
	end if
end delete_list_item

del_list is a variable

Hi Stefan,

I have to apologize for my stupidness…

The next goal will be putting your code in a xcode application…

The best solution will be linking the applescript file into the application…

Best Regards,

Stefan

Considering that you’re going to process approx. 2000 lines,
I recommend a binary search instead of parsing the list serially

set delete_nachname to "Lehrner"
set delete_vorname to "Laetitia"
set delete_telefon to "+436505800550"

set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"

set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
set foundIndex to BinarySearch(del_list, delete_adressbook)
if foundIndex is not false then set del_list to delete_list_item(del_list, foundIndex)

set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list

on delete_list_item(theList, theItem)
	if theItem is 1 then
		return rest of theList
	else if theItem is (count theList) then
		return items 1 thru -2 of theList
	else
		return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
	end if
end delete_list_item

on BinarySearch(theList, theValue)
	local low, mid, high
	script o
		property l : theList
	end script
	
	set low to 1
	set high to (count theList)
	repeat while (low ≤ high)
		set mid to (low + high) div 2
		if ({theValue} is in items low thru mid of o's l) then
			set high to mid - 1
		else
			set low to mid + 1
		end if
	end repeat
	
	if (item low of o's l is theValue) then return low
	return false
end BinarySearch

Hi Stefan,

using the first example - I have written this - do you think that my syntax is correct?

(Sorry, I have no modem here at the moment, so I am not able to test)


if name of theObject is "bt_delete_adressbook" then
		
		set delete_nachname to contents of text field "nachname" of drawer "Inbox" of window "main"
		set delete_vorname to contents of text field "vorname" of drawer "Inbox" of window "main"
		set delete_telefon to contents of text field "telefon" of drawer "Inbox" of window "main"
		
		set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
		
		set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
		set del_list to load script file script_path
		tell delete_list_item
			set del_list to delete_list_item(theList, theItem)
		end tell
		
		set write_adressbook to del_list
		-- hier schreiben wir ins file - alles ausser dem gelöschten namen
		
		set fileToCheck_adressbook to ((path to desktop as Unicode text) & "adressbook.txt")
		
		set eof of fileToCheck_adressbook to 0
		set open_adressbook to open for access fileToCheck_adressbook with write permission
		
		write (write_adressbook as string) to open_adressbook starting at eof
		close access open_adressbook
		-- hier wird wieder gelesen
		
		delay 1
		--hier lesen wir wieder aus dem file: 
		
		set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
		set num_list to paragraphs of (read theFile)
		set myList_ab to {}
		set {tid, text item delimiters} to {text item delimiters, ";"}
		repeat with num in num_list
			set end of myList_ab to text items 1 thru 3 of num
		end repeat
		set text item delimiters to tid
		set content of table view "adressbook" of scroll view "adressbook" of drawer "Inbox" of window "main" to myList_ab
		
		
	end if

delete_adressbook.scpt looks like this:



set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"

set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
repeat with i from 1 to count del_list
	if item i of del_list is delete_adressbook then
		set del_list to delete_list_item(del_list, i)
		exit repeat
	end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list

on delete_list_item(theList, theItem)
	if theItem is 1 then
		set q to rest of theList as text
	else if theItem is (count theList) then
		set q to items 1 thru -2 of theList as text
	else
		set q to (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList) as text
	end if
end delete_list_item

What are the advantages of the binary search comparing to the first one?

Thanks for your info,

Stefan

I don’t know, because I don’t have your environment, so I can’t test it either.
Run it and look, what’s happening. :wink:

SPEED!
If you have a list of 1000 items, it can take (worst case) 1000 loops to find the item (serial search).
With binary search it takes at most 10 loops to find it (2^10 = 1024)