Finding (and deleting) the last comma in a list

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
WayPastInk
Posts: 11
Joined: Sat Oct 14, 2006 2:08 pm

Finding (and deleting) the last comma in a list

Post by WayPastInk » Wed May 14, 2008 1:35 pm

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
Mark MacKenzie
Art Conservator
Alcalde, NM

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Wed May 14, 2008 2:15 pm

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)

Code: Select all

if char -1 of WhatEver = "," then
  delete char -1 of WhatEver
end if
Hope that helps.


Best

Klaus
Last edited by Klaus on Wed May 14, 2008 7:21 pm, edited 1 time in total.

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Wed May 14, 2008 6:46 pm

Whenever you are building a list of some sort and are appending the delimiter character yourself, always remember to delete that last one.

Code: Select all

repeat with a = 1 to 10
	put a &"," after myList
end repeat
delete last char of myList
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.

Code: Select all

if the last char of myList is "," then delete last char of mylist
When working with lines, here's a trick to remove any number of blank lines from front or back of the list:

Code: Select all

put word 1 to (the number of words in myList) of myList into myList
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).
~ Nonsanity
~ Chris Innanen

WayPastInk
Posts: 11
Joined: Sat Oct 14, 2006 2:08 pm

Post by WayPastInk » Thu May 15, 2008 1:33 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
Mark MacKenzie
Art Conservator
Alcalde, NM

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Thu May 15, 2008 2:13 pm

Hi Mark,

after reading your first posting again, it looks like your are just not using the function-syntax correctly:
sum tCost
## This is the correct syntax for a HANDLER!

It should be:
put sum(tCost) intop total_Cost
## or whatever...

But maybe that was just a typo in your posting.


Best

Klaus

WayPastInk
Posts: 11
Joined: Sat Oct 14, 2006 2:08 pm

Post by WayPastInk » Fri May 16, 2008 12:55 am

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
Mark MacKenzie
Art Conservator
Alcalde, NM

Post Reply