Finding (and deleting) the last comma in a list
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 11
- Joined: Sat Oct 14, 2006 2:08 pm
Finding (and deleting) the last comma in a list
It has been a long night and still not a simple solution or solution at all.
I am taking number data from the lines of a field and putting these numbers (format of xx.xx) into a local tCost. The list of numbers looks like tCost = "10.00,12.06,". I am trying to use the expression: sum tCost to sum the contents of tCost. This is throwing an error and I think that it is do to the trailing final comma in the list.
However, I can't figure out how to find and delete that final comma prior to calling the sum handler.
I am prepared for the answer to this to be fairly simple I am just absolutely blind to finding it and have been for the past two days. Sorry for the newbie question.
Help?
Mark MacKenzie
I am taking number data from the lines of a field and putting these numbers (format of xx.xx) into a local tCost. The list of numbers looks like tCost = "10.00,12.06,". I am trying to use the expression: sum tCost to sum the contents of tCost. This is throwing an error and I think that it is do to the trailing final comma in the list.
However, I can't figure out how to find and delete that final comma prior to calling the sum handler.
I am prepared for the answer to this to be fairly simple I am just absolutely blind to finding it and have been for the past two days. Sorry for the newbie question.
Help?
Mark MacKenzie
Mark MacKenzie
Art Conservator
Alcalde, NM
Art Conservator
Alcalde, NM
Hi Mark,
a trailing comma does NOT throw an error when "sum"ming up!
Try in the messagebox:
put sum(10,22,)
-> 32
So it must be something else that trhows an error in your handler.
Anyway:
Removing a trailing character (comma or whatever)
Hope that helps.
Best
Klaus
a trailing comma does NOT throw an error when "sum"ming up!
Try in the messagebox:
put sum(10,22,)
-> 32
So it must be something else that trhows an error in your handler.
Anyway:
Removing a trailing character (comma or whatever)
Code: Select all
if char -1 of WhatEver = "," then
delete char -1 of WhatEver
end if
Best
Klaus
Last edited by Klaus on Wed May 14, 2008 7:21 pm, edited 1 time in total.
Whenever you are building a list of some sort and are appending the delimiter character yourself, always remember to delete that last one.
And when the list is coming from elsewhere and you want to double-check for a last delimiter, as Klaus said, test for it first.
When working with lines, here's a trick to remove any number of blank lines from front or back of the list:
That will also remove any whitespace characters from the beginning and end of the list. So if lines with just whitespace are important, or whitespace at the beginning or end of other lines needs to be preserved, don't use this shortcut.
You can also use that shortcut for word lists or really any list that uses a whitespace character as a deliminator (space, tab, return, etc).
Code: Select all
repeat with a = 1 to 10
put a &"," after myList
end repeat
delete last char of myList
Code: Select all
if the last char of myList is "," then delete last char of mylist
Code: Select all
put word 1 to (the number of words in myList) of myList into myList
You can also use that shortcut for word lists or really any list that uses a whitespace character as a deliminator (space, tab, return, etc).
~ Nonsanity
~ Chris Innanen
~ Chris Innanen
-
- Posts: 11
- Joined: Sat Oct 14, 2006 2:08 pm
Thank you Klaus and Chris.
I see where I went wrong (several times).
I eventually adopted a different way of getting the sum of the list rather than using the sum function.
There must be something I haven't found yet that makes the sum function throw an error the way I am trying to use it.
Getting the last char of the list is so simple I now feel somewhat foolish in not seeing it myself. Goes to show how powerful and really understandably simple Transcript really is.
Thank you both.
Mark
I see where I went wrong (several times).
I eventually adopted a different way of getting the sum of the list rather than using the sum function.
There must be something I haven't found yet that makes the sum function throw an error the way I am trying to use it.
Getting the last char of the list is so simple I now feel somewhat foolish in not seeing it myself. Goes to show how powerful and really understandably simple Transcript really is.
Thank you both.
Mark
Mark MacKenzie
Art Conservator
Alcalde, NM
Art Conservator
Alcalde, NM
-
- Posts: 11
- Joined: Sat Oct 14, 2006 2:08 pm
Hi Klaus.
I feel an attack of the stupids coming on. Yes, you are perfectly correct. I have been away from Revolution for long enough to forget some of the basics.
My original posting was correct and you have pointed out the major error of my coding.
Thank you
Mark
I feel an attack of the stupids coming on. Yes, you are perfectly correct. I have been away from Revolution for long enough to forget some of the basics.
My original posting was correct and you have pointed out the major error of my coding.
Thank you
Mark
Mark MacKenzie
Art Conservator
Alcalde, NM
Art Conservator
Alcalde, NM