Finding matches in a string...

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
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Finding matches in a string...

Post by bjb007 » Fri Feb 15, 2008 6:22 am

I have a string

2,14,1,5,19....etc.

I want to count the number of occurences of 1
but looping through the string also counts 14 and 19
etc. as being '1'. Same for 2,3, etc. as there will
be 21,22....31,32... in the string.

How can I prevent 14 etc. being counted as 1, 24 as 2 etc.?

Possible fixes all seem convoluted.

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Contact:

Post by malte » Fri Feb 15, 2008 6:45 am

A script would be helpful here. :)

You can take a look at matchtext an wholematches in the dictionary. However, there may be different solutions, that would require to see your scripts.

All the best,

Malte

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Finding matches in a string...

Post by bjb007 » Fri Feb 15, 2008 7:45 am

OK malte...

----------------------------------------------------------
put url ("file:" & "numbers.txt") into myData
put myData & "1," into myData
put item -36 to -1 of myData into theString
put myData into url ("file:" & "numbers.txt")

put 0 into theCount
repeat with i = 1 to 36
if item i of theString = item 1 of theString then
add 1 to theCount
end if
end repeat
-----------------------------------------------------------

item 1 is 1
Always true for the first comparison since 1 has just
been added to myData but do it to get theCount.

BTW I do format with indents but submit removes it.

P.S. Why do I keep typing theCount = 0?
Because it's the standard way of putting a value into
a variable - except in Rev!

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri Feb 15, 2008 6:04 pm

The code, as you posted it, seems to do what you want, and I can use it to count items. Maybe that is not actually what you want? Of course you have to make sure that the file exists, and that it's contents always has a comma at the end.
As an alternative, I suggest you try this code, it's easier to use and faster:

Code: Select all

  put url ("file:" & "numbers.txt") into myData 
  put  "1," after myData 
  put item -36 to -1 of myData into theString 
  put myData into url ("file:" & "numbers.txt") 

  put item 1 of theString into theCompare
  repeat for each item theItem in theString 
    if theCompare = theItem then 
      add 1 to theCount 
    end if 
  end repeat 
To keep intendation intact when posting to the forum, you need to use the code button in the editor, or write the code tags, as specified in the BBCode help page.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Finding matches in a string...

Post by bjb007 » Fri Feb 15, 2008 7:11 pm

Thanks BvG - that did the trick.

I got it to work by hard-coding the number to compare
to but wanted generic code so that I could use the
different numbers "on-the-fly".

I had to mod this line...

put item 1 of theString into theCompare

to

put item -1 of theString into theCompare

as I wanted the last number in the string.

BTW I was pleased to find the "step" option in the
"repeat" docs i.e.

repeat for i = 1 to 36 step 4
would only check 1,5,9...etc.

Post Reply