Page Index Toggle Pages: 1 [2]  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Counting fields with Sesame (Read 4294 times)
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Counting fields with Sesame
Reply #15 - May 18th, 2004 at 5:39pm
Print Post Print Post  
Blightman,

I got your appplication. Of the 925 records, only 48 have a violation code, so I assume that those are the only ones we're interested in.

Which way do you want this?
Option 1: State is Primary
MA
  CIN    5
  CKL    2
  DIL    3
Total for MA: 10

VA
  CIN    4
  CKL    1
  DIL     1
Total for VA: 6

TOTAL: 16
------------------------

Or Option 2: Violation Code is primary

CIN
  MA    5
  VA    4
Total CIN: 9

CKL
  MA    2
  VA    1
Total CKL: 3

DIL
  MA    3
  VA    1
Total DIL: 4

TOTAL: 16
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Blightman
Member
*
Offline


No personal text

Posts: 17
Location: D.C. person living in Utah
Joined: May 12th, 2004
Re: Counting fields with Sesame
Reply #16 - May 18th, 2004 at 8:24pm
Print Post Print Post  
Hammer,

It is safe to assume that what I sent you are the only records in question.  Thousands more will be imported later.

Option #2 is what I want to do.  What other questions do you have?

By the way: how do you quote from a previous post?  That would make my quotes more presentable.
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Counting fields with Sesame
Reply #17 - May 18th, 2004 at 9:54pm
Print Post Print Post  
In reply to your question about making Quotes on the forum:

I have provided a reply in a thread with its own Subject named "Forum Quoting" at
http://www.lantica.com/Forum2/cgi-bin/yabb/YaBB.pl?board=gen_disc;action=post2
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Counting fields with Sesame
Reply #18 - May 19th, 2004 at 2:43pm
Print Post Print Post  
Blightman,

Attached is a Mass Update showing an example of doing this. Retrieve your records, sorted by violation code. Run this Mass Update.

Note that about 80 percent of this code is only necessary because the State is buried in the City value and must be parsed out. Because of this, we have to do a bunch of bookkeeping to track the occurences, rather than just being able to sort by State.

This can also be adjusted so that you click a command button, or so that the output goes to a printer.

The Mass Update will produce something like this in the WriteLn window:
Code
Select All
Violation: CIN
		3
HI		2
Total Violations for CIN: 5
----------
Violation: GEN
WI		1
		1
Chile		1
Total Violations for GEN: 3
----------
Violation: INS
Chile		1
TX		1
Total Violations for INS: 2
----------
Violation: NCK
NY		1
MI		1
IN		1
Total Violations for NCK: 3
----------
Violation: NCL
VA		1
NC		1
NY		2
CO		2
IL		2
NV		1
TX		1
TX		1
FL		1
HI		1
PA		1
MO		1
OK		1
AR		1
OH		1
Total Violations for NCL: 18
----------
Violation: SHC
MA		1
GA		1
Total Violations for SHC: 2
----------
Violation: SHP
LA		1
Total Violations for SHP: 1
----------
Violation: SPC
MS		1
NJ		1
Total Violations for SPC: 2
----------
Violation: TMP
SD		2
AZ		3
VT		1
PA		1
LA		2
VA		1
CO		1
CA		1
Total Violations for TMP: 12
----------
---------------------------------
---------------------------------
Total Violations: 48 




Here's the code:
Code
Select All
** PROGRAMMING SECTION: [ GLOBAL CODE ] [] **
// Declare variable to hold the compare values
stat gvState as String
stat gvViolation as String

// Declare variable to hold summary
stat gvGrandTotal as Int

// Declare array to hold state totals
stat gvaStateCounts as Array[100, 2] of String

// Function to parse the State out of the City value.
// The State is whatever follows the comma and space.
Function GetState(vVal as String) As String
var vPos as Int
var vLen as Int
var vRet as String

	vRet = ""
	vLen = @Len(vVal)

	// Find the comma
	vPos = @InStr(vVal, ",")

	// Advance past the comma and space
	vPos = vPos + 2

	// Make sure there is something there
	If(vLen >= vPos)
	{
		// Parse out the State.
		vRet = @Mid(vVal, vPos, vLen)
	}

	Return(vRet)

End Function

// Subroutine to clear state totals array
Subroutine ClearCounts()
var vOuterLoop as Int
var vInnerLoop as Int

	For vOuterLoop = 1 To 100
		For vInnerLoop = 1 To 2
			gvaStateCounts[vOuterLoop, vInnerLoop] = ""
		Next
	Next

End Subroutine

// Function to find a state in the state totals array
Function FindState(vVal as String) As Int
var vLoop as Int
var vRet as Int

	vRet = 0
	vLoop = 1
	While((vLoop <= 5) And (vRet = 0))
	{
		If vVal = gvaStateCounts[vLoop, 1]
		{
			vRet = vLoop
		}
		vLoop += 1
	}

	Return(vRet)

End Function


// Function to find an empty slot in the state totals array
Function FindStateSlot() As Int
var vLoop as Int
var vCount as Int
var vRet as Int

	vRet = 0
	vLoop = 1
	While((vLoop <= 100) And (vRet = 0))
	{
		vCount = @ToNumber(gvaStateCounts[vLoop, 2])
		If (gvaStateCounts[vLoop, 1] = "") And (vCount = 0)
		{
			vRet = vLoop
		}
		vLoop += 1
	}

	Return(vRet)

End Function


// Subroutine to write summary to WriteLn Window
Subroutine WriteSummary()
var vLoop as Int
var vCount as Int
var vTotal as Int

	WriteLn("Violation: " + gvViolation)
	For vLoop = 1 To 100
		vCount = @ToNumber(gvaStateCounts[vLoop, 2])
		If (gvaStateCounts[vLoop, 1] <> "") Or (vCount > 0)
		{
			WriteLn(gvaStateCounts[vLoop, 1] + Tab(2) + @Str(vCount))
			vTotal += vCount
		}
	Next
	WriteLn("Total Violations for " + gvViolation + ": " + @Str(vTotal))
	WriteLn("----------")
	gvGrandTotal += vTotal

End Subroutine


	// Initialize variables to values in first record
	gvState = GetState(City0)
	gvViolation = Violation Code

	// Initialize Summary
	gvGrandTotal = 0

	// Initialize State Counts
	ClearCounts()
	gvaStateCounts[1, 1] = gvState
	gvaStateCounts[1, 2] = "0"






** PROGRAMMING SECTION: [Courier] [Mass Update] **
var vCurState as String
var vCurViolation as String
var vPos as Int

	// Initialize variables to values in current record
	vCurState = GetState(City0)
	vCurViolation = Violation Code

	If vCurViolation <> gvViolation
	{
		WriteSummary()
		ClearCounts()
		gvaStateCounts[1, 1] = vCurState
		gvaStateCounts[1, 2] = "0"
		gvViolation = vCurViolation
	}

	vPos = FindState(vCurState)
	If vPos > 0
	{
		gvaStateCounts[vPos, 2] = @Str(@ToNumber(gvaStateCounts[vPos, 2]) + 1)
	}
	Else
	{
		vPos = FindStateSlot()
		If vPos > 0
		{
			gvaStateCounts[vPos, 1] = vCurState
			gvaStateCounts[vPos, 2] = @Str(@ToNumber(gvaStateCounts[vPos, 2]) + 1)
		}
	}

	If @ResultSetCurrentPosition() = @ResultSetTotal()
	{
		WriteSummary()
		WriteLn("---------------------------------")
		WriteLn("---------------------------------")
		WriteLn("Total Violations: " + @Str(gvGrandTotal))
	} 

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Blightman
Member
*
Offline


No personal text

Posts: 17
Location: D.C. person living in Utah
Joined: May 12th, 2004
Re: Counting fields with Sesame
Reply #19 - May 19th, 2004 at 6:41pm
Print Post Print Post  
Hammer,

Brilliant!  You're amazing.  I never would have figured out this on my own.  Thanks  Grin

When I copied the code into my database as a global code, I tested the database for errors.  On every single layout element in which I have coding (about 15), I have the following error on line 1, position 1:

Statement or End-of-Line expected OR
Unknown identifier

Is the Global Code overwriting the individual programming that all ready exists, so the database isn't certain which programming code to run?  Is it something else?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Counting fields with Sesame
Reply #20 - May 19th, 2004 at 6:47pm
Print Post Print Post  
Blightman,

The code I provided is a Mass Update, not regular Form programming. Part of it goes in Global Code for the Mass Update, the other part can go in any other element.

The part headed with
** PROGRAMMING SECTION: [ GLOBAL CODE ] [] **
goes in Global Code. Do not include the header line itself.

The part headed with
** PROGRAMMING SECTION: [Courier] [Mass Update] **
goes in any other element. Do not include the header line itself.

Remember to sort your retrieved records by violation code, or it won't work properly.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Blightman
Member
*
Offline


No personal text

Posts: 17
Location: D.C. person living in Utah
Joined: May 12th, 2004
Re: Counting fields with Sesame
Reply #21 - May 19th, 2004 at 6:58pm
Print Post Print Post  
Hello?!  I should have caught that.  I think I have the after-lunch nods....
  
Back to top
 
IP Logged
 
Blightman
Member
*
Offline


No personal text

Posts: 17
Location: D.C. person living in Utah
Joined: May 12th, 2004
Re: Counting fields with Sesame
Reply #22 - May 19th, 2004 at 7:55pm
Print Post Print Post  
Just out of curiosity, and for my own reference in the future, if I wanted option #1 (State is primary), if I were to take the code you sent and change the references to "state" and make them "violation," would I get the the results that I would want?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Counting fields with Sesame
Reply #23 - May 19th, 2004 at 8:04pm
Print Post Print Post  
Quote:
Just out of curiosity, and for my own reference in the future, if I wanted option #1 (State is primary), if I were to take the code you sent and change the references to "state" and make them "violation," would I get the the results that I would want?


Nope!  Smiley

The need to parse out the State and manually keep track of it means that the code would need some adjustments to manage State being primary. That's why I asked before creating the code.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send Topic Send Topic Print Print