[SOLVED] Something is not right with my data

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: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

[SOLVED] Something is not right with my data

Post by karmacomposer » Tue Aug 04, 2020 5:56 pm

I am absolutely perplexed.

I am iterating through a series of data and putting them into variables

I am then testing these string variables and trying to have a hit counter count them up:

Let's say ccMonthDecrypted does = "January" and I have actually "put" this result into a field just to ensure I am not whacked out of my mind.

So the following code should get a HIT when a particular month is equal to ccMonthDecrypted (the decrypted results of this variable are indeed January through December and all are spelled 100% and they all start with a capital letter, etc. Nothing is wrong with the encrypt/decrypt and nothing is weird about the data itself.)

All variables have been declared with a local variable (local varJanCount) and all variables contain a 0 (put 0 into varJanCount)

YET....

Code: Select all

if ccMonthDecrypted = "January" then
			add 1 to varJanCount
		 else
			if ccMonthDecrypted = "February" then
			add 1 to varFebCount
		 else
			if ccMonthDecrypted = "March" then
			add 1 to varMarCount
		 else
			if ccMonthDecrypted = "April" then
			add 1 to varAprCount
		 else
			if ccMonthDecrypted = "May" then
			add 1 to varMayCount
		 else
			if ccMonthDecrypted = "June" then
			add 1 to varJunCount
		 else
			if ccMonthDecrypted = "July" then
			add 1 to varJulCount
		 else
			if ccMonthDecrypted = "August" then
			add 1 to varAugCount
		 else
			if ccMonthDecrypted = "September" then
			add 1 to varSepCount
		 else
			if ccMonthDecrypted = "October" then
			add 1 to varOctCount
		 else
			if ccMonthDecrypted = "November" then
			add 1 to varNovCount
		 else
			if ccMonthDecrypted = "December" then
			add 1 to varDecCount
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
		 end if
Results in, well, no results.

Code: Select all

put "January" & tab & varJanCount & cr &\
		 "February" & tab & varFebCount & cr &\
		 "March" & tab & varMarCount & cr &\
		 "April" & tab & varAprCount & cr &\
		 "May" & tab & varMayCount & cr &\
		 "June" & tab & varJunCount & cr &\
		 "July" & tab & varJulCount & cr &\
		 "August" & tab & varAugCount & cr &\
		 "September" & tab & varSepCount & cr &\
		 "October" & tab & varOctCount & cr &\
		 "November" & tab & varNovCount & cr &\
		 "December" & tab & varDecCount into line 2 of field "tblAcctsMonth"
In the table from the above code, the 1st column correctly shows all months. The second column is all 0's since I am not
getting any hits.

If read in english, it looks like this:

if "January" = "January" then
add 1 to varJanCount
end if

That should work when I hit a January.

It's not.

It makes no sense.

What am I doing wrong?

Mike
Last edited by karmacomposer on Wed Aug 05, 2020 4:32 am, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7321
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Something is not right with Livecode Strings

Post by dunbarx » Tue Aug 04, 2020 6:13 pm

Hi.

First, are you sure that the case doesn't matter? If it does, "january" <> "January".

Second, I took your code and to run its easily in a button added:

Code: Select all

on mouseUp
   put "January" into ccMonthDecrypted --ADDED
   if ccMonthDecrypted = "January" then
      add 1 to varJanCount
   else
      if ccMonthDecrypted = "February" then
      --etc
I got a "1" in varJanCount.

Third. Do this instead:

Code: Select all

on mouseUp
   put "January" into ccMonthDecrypted
   switch ccMonthDecrypted
      case "January"
         add 1 to varJanCount
         break
      case "February"
         add 1 to varFebCount
         break
         --- etc
   end switch
end mouseUp
SOOO much easier to read and manage.

Craig

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: Something is not right with Livecode Strings

Post by karmacomposer » Tue Aug 04, 2020 6:16 pm

Yes, case is correct.

I can try switch, but will it work over the if/then = statement?

Mike

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8322
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Something is not right with Livecode Strings

Post by FourthWorld » Tue Aug 04, 2020 6:16 pm

Can we see the part where ccMonthDecrypted is assigned?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: Something is not right with Livecode Strings

Post by karmacomposer » Tue Aug 04, 2020 6:22 pm

Did not work. Resulted in all '0's

It's like the variable is being reset or emptied. I just don't get it.

Mike

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8322
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Something is not right with Livecode Strings

Post by FourthWorld » Tue Aug 04, 2020 6:25 pm

I wonder if sharing the part where ccMonthDecrypted is assigned might help us help you...
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

mrcoollion
Posts: 597
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Something is not right with Livecode Strings

Post by mrcoollion » Tue Aug 04, 2020 7:07 pm

Another way to get the month number

Code: Select all

   put "July" into ccMonthDecrypted 
   put "January,February,March,April,May,June,July,August,September,October,November,December" into tMonthsList
   put itemoffset(ccMonthDecrypted,tMonthsList,1)+1 into tResult

SparkOut
Posts: 2360
Joined: Sun Sep 23, 2007 4:58 pm

Re: Something is not right with Livecode Strings

Post by SparkOut » Tue Aug 04, 2020 7:22 pm

As Richard says, it looks like we need to see where the assignment of ccMonthDecrypted is made.

Personally I wouldn't use an if/then or switch structure for this, it's crying out for an array.
One line:

Code: Select all

add 1 to varMonthCount[(ccMonthDecrypted)]
The added advantage of this is that you can see exactly what the ccMonthDecrypted values contain as they will be the keys of the array. If there is any anomaly such that ccMonthDecrypted doesn't contain one of the month names, or something else weird is happening, it will be readily apparent from a quick look at the keys.

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: Something is not right with Livecode Strings

Post by karmacomposer » Tue Aug 04, 2020 7:35 pm

SparkOut wrote:
Tue Aug 04, 2020 7:22 pm
As Richard says, it looks like we need to see where the assignment of ccMonthDecrypted is made.

Personally I wouldn't use an if/then or switch structure for this, it's crying out for an array.
One line:

Code: Select all

add 1 to varMonthCount[(ccMonthDecrypted)]
The added advantage of this is that you can see exactly what the ccMonthDecrypted values contain as they will be the keys of the array. If there is any anomaly such that ccMonthDecrypted doesn't contain one of the month names, or something else weird is happening, it will be readily apparent from a quick look at the keys.
SparkOut,

That seems to work a bit. However, trying to put it in a table after the repeat loop does not work.

How would I put the month and the count, separated by & tab & into a table field (say field "tblMonths")?

Do I need to use the combine with command?

How would I sort it so that it reads January - December in order but with the correct counts?

One last thing - I need to also put just the values into a string for chartMaker:

put "Values=Accounts," then the numbers separated by & "," & - but in order of January - December

Thanks for the help - this one may actually work.

Mike

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7321
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Something is not right with Livecode Strings

Post by dunbarx » Tue Aug 04, 2020 7:55 pm

Yes, you have to combine an array variable in order to "get it back" into Livecode.

Try this as an experiment in a button. Only the first four months are listed, "alphabetizing" them, so you would have to assign a value to all 12:

Code: Select all

on mouseUp
   repeat 3
      add random(2) mod 2 to myArray["3 Jan"]
      add random(2) mod 2 to myArray["2 Feb"]
      add random(2) mod 2 to myArray["4 Mar"]
      add random(2) mod 2 to myArray["1 Apr"]
   end repeat
   combine myArray with return and comma
   sort myArray by the first word of each
   breakpoint
end mouseUp
Step through the handler. You can see in the debugger a random number of "hits" in each month, and the months are ordered properly. This is just a start...

Craig
Last edited by dunbarx on Tue Aug 04, 2020 7:56 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8322
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Something is not right with Livecode Strings

Post by FourthWorld » Tue Aug 04, 2020 7:56 pm

karmacomposer wrote:
Tue Aug 04, 2020 7:35 pm
How would I put the month and the count, separated by & tab & into a table field (say field "tblMonths")?

Do I need to use the combine with command?
Yep. This should do it:

Code: Select all

combine varMonthCount with cr and tab
...where the first delim used (CR) determines the separator between key-value pairs, and the second delim (tab) is what will go between each key and its value.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

SparkOut
Posts: 2360
Joined: Sun Sep 23, 2007 4:58 pm

Re: Something is not right with Livecode Strings

Post by SparkOut » Tue Aug 04, 2020 7:58 pm

The combine command will work to make it readable, but it won't sort the months into month order.
Pick whatever you need from here

Code: Select all

 -- double-check the results with a visual check that there are only 12 valid month name keys in the array
   put the keys of varMonthCount --or answer the keys of varMonthCount
   
   repeat for each line tMonth in the keys of varMonthCount
      put tMonth & tab & varMonthCount[(tMonth)] & cr after tMonthCountList
   end repeat
   put tMonthCountList  --another visual check of the full data
   -- you will see an array is not an ordered list, but a hash table that is not sorted
   
   put empty into tMonthCountList --just to clear it of the other visual check data
   --we can construct a list of the month names to ensure the correct sort
   --or we can be lazy and use the built-in monthNames function
   repeat for each line tMonth in the monthNames
      put tMonth & tab & varMonthCount[(tMonth)] & cr after tMonthCountList
   end repeat
   put tMonthCountList into field "tblMonths" 
   --another visual check of the full data, where the keys are automatically sorted
   --if you have a non-English system, you might find it easier to just start with a list you made by populating 
   --a variable with the month names you want in the correct order
   
   put "Values=Accounts," into tChartmakerData
   repeat for each line tMonth in the monthNames
      put varMonthCount[(tMonth)] & "," after tChartmakerData
   end repeat
   delete the last char of tChartmakerData --drop the trailing comma
   put tChartmakerData

karmacomposer
Posts: 355
Joined: Wed Apr 27, 2011 2:12 pm

Re: Something is not right with Livecode Strings

Post by karmacomposer » Tue Aug 04, 2020 8:10 pm

I appreciate the help everyone. The other dilemma is that this solution lists each month found and then a number so there are multiple months with different numbers. Not sure how to read it.

Ie:

October 1
January 3

...

October 2
January 1

Point is, there are 403 records and this will give me 403 results. I need the 12 months with accurate hit counts per month

This is for a dashboard and this table and graph are for Accounts Per Month

Once I have one working solution I can extrapolate for the rest.

Mike

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7321
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Something is not right with Livecode Strings

Post by dunbarx » Tue Aug 04, 2020 8:56 pm

Not sure what you mean. Are both days and months to be recorded? In multiple years?

In any case, all you need do is assign unique values to those entries. LC has powerful sorting and parsing capabilities, and you can retrieve anything you need.

So for:
October 1
January 3

...

October 2
January 1
What do you actually need?

Craig

SparkOut
Posts: 2360
Joined: Sun Sep 23, 2007 4:58 pm

Re: Something is not right with Livecode Strings

Post by SparkOut » Tue Aug 04, 2020 9:04 pm

So how are you retrieving the records?

If you do the array population with the single line I showed, then each new value will only create a new key if there was not one with that name already.

Unless you have some other decrypted results that are not the same as the other month names, then you can only have 12 records.

Where are you getting the other values?

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”