I want to scroll through the alphabet in order to scroll some data to the
closest name that starts with a letter. If the user hits the H button then
it should scroll to the letter closest to H. If no one exists with H, then
go to I, etc. If its near the end, say 'V', and the last person is a 'T'
then it should work its way back up the alphabet. I was trying to loop as
if the Char's were ints but I am having problems
I have buttons with A .. Z that have the letter in the Tag field.
char key = button.Tag.ToSt ring().ToCharAr ray(1,1)[0]; // get the letter
to search for ...
// work our way down the alphabet
while (row == 0 && (int)key <= (int)'Z') {
row = list.find (firstletter = key )
(int)key++;
}
// if we didn't find something at least lower than the button pressed
then try going up the alphabet
if (row==0) {
key = (char)(int)butt on.Tag.ToString ().ToCharArray( 1,1)[0] - 1;
// work our way up the alphabet
while (row == 0 && (int)key >= (int)"A") {
row = this.dwPatientL ist.FindRow("le ft(sorted_name, 1)='" + key
+ "'",row,dwPatie ntList.RowCount );
(int)key--;
}
}
Scroll to row 8 3261
Jack... OFF THE TOP OF MY POINTED HEAD here is some code to find partial
matches in an array list. You could modify and TEST this code to suit
your needs.
////////////// UNTESTED CODE ///////////////
using System;
using System.Collecti ons;
namespace TestBinarySearc h
{
/// <summary>
/// Summary description for Class1.
/// UNTESTED CODE
/// </summary>
///
class MyCaseInsensiti veCompare : IComparer
{
// ASSERT x, y are strings not null
// Case insensitive
public int Compare(object x, object y)
{
return String.Compare( (string)x,(stri ng)y,true);
}
}
class MyCompare : IComparer
{
// ASSERT x, y are strings not null
// Case insensitive
public int Compare(object x, object y)
{
int length= ((string)y).Len gth;
if (((string)x).Le ngth < length) return -1;
return String.Compare( (string)x,0,(st ring)y,0,length ,true);
}
}
class Class1
{
private ArrayList al= new ArrayList();
// ASSERT find not null
public void FindIt(string find)
{
// Do BINARY_SEARCH to find any partial match
al.Sort(new MyCaseInsensiti veCompare());
int index= al.BinarySearch (find, new MyCompare());
System.Console. WriteLine("Find : "+find);
if (index < 0) System.Console. WriteLine("No Match.");
else
//got at least one match for criteria find&
{
System.Console. WriteLine("Outp ut");
// now do LINEAR_SEARCH for other matches
int firstIndex= index;
int lastIndex= index;
MyCompare mc= new MyCompare();
for (int i=index-1; i>=0 && (mc.Compare(al[i],find)==0) ; i--)
{
firstIndex= i;
}
for (int i=index+1; (i<al.Count) && (mc.Compare(al[i],find)==0);
i++)
{
lastIndex= i;
}
for (int i=firstIndex;i< =lastIndex;i++)
{
System.Console. WriteLine(al[i]);
}
}
}
//ASSERT al not null
public Class1(ArrayLis t al)
{
this.al= al;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
ArrayList al= new ArrayList();
al.Add("Louie,J eff");
al.Add("Smith,A nn");
al.Add("Smith,J ");
al.Add("Smith,D ick,");
al.Add("Smith,P eter,");
al.Add("Smith,J an");
al.Add("Smith,T om");
al.Add("Smith,H arry");
al.Add("Smith,J oe");
al.Add("Smith,J oe");
al.Add("Smith,J ohn");
Class1 c1= new Class1(al);
c1.FindIt("SMIT H,JO");
System.Console. ReadLine();
}
}
}
Regards,
Jeff I want to scroll through the alphabet in order to scroll some data to
the closest name that starts with a letter.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Second try... I just wrote this off the top of my pointed head. This
code looks for a partial match in an array list. You could try to modify
it.
////////// UNTESTED CODE //////////////////
using System;
using System.Collecti ons;
namespace TestBinarySearc h
{
/// <summary>
/// Summary description for Class1.
/// UNTESTED CODE
/// </summary>
///
class MyCaseInsensiti veCompare : IComparer
{
// ASSERT x, y are strings not null
// Case insensitive
public int Compare(object x, object y)
{
return String.Compare( (string)x,(stri ng)y,true);
}
}
class MyCompare : IComparer
{
// ASSERT x, y are strings not null
// Case insensitive
public int Compare(object x, object y)
{
int length= ((string)y).Len gth;
if (((string)x).Le ngth < length) return -1;
return String.Compare( (string)x,0,(st ring)y,0,length ,true);
}
}
class Class1
{
private ArrayList al= new ArrayList();
// ASSERT find not null
public void FindIt(string find)
{
// Do BINARY_SEARCH to find any partial match
al.Sort(new MyCaseInsensiti veCompare());
int index= al.BinarySearch (find, new MyCompare());
System.Console. WriteLine("Find : "+find);
if (index < 0) System.Console. WriteLine("No Match.");
else
//got at least one match for criteria find&
{
System.Console. WriteLine("Outp ut");
// now do LINEAR_SEARCH for other matches
int firstIndex= index;
int lastIndex= index;
MyCompare mc= new MyCompare();
for (int i=index-1; i>=0 && (mc.Compare(al[i],find)==0) ; i--)
{
firstIndex= i;
}
for (int i=index+1; (i<al.Count) && (mc.Compare(al[i],find)==0);
i++)
{
lastIndex= i;
}
for (int i=firstIndex;i< =lastIndex;i++)
{
System.Console. WriteLine(al[i]);
}
}
}
//ASSERT al not null
public Class1(ArrayLis t al)
{
this.al= al;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
ArrayList al= new ArrayList();
al.Add("Louie,J eff");
al.Add("Smith,A nn");
al.Add("Smith,J ");
al.Add("Smith,D ick,");
al.Add("Smith,P eter,");
al.Add("Smith,J an");
al.Add("Smith,T om");
al.Add("Smith,H arry");
al.Add("Smith,J oe");
al.Add("Smith,J oe");
al.Add("Smith,J ohn");
Class1 c1= new Class1(al);
c1.FindIt("SMIT H,JOHN");
System.Console. ReadLine();
}
}
}
Regards,
Jeff I want to scroll through the alphabet in order to scroll some data to
the closest name that starts with a letter.<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jeff,
Thanks for the code... It makes sense but kinda missed what I needed help
with, I think it did more than what I need to do.
I am tryingto simulate the contacts in outlook with the list of letters down
the side. I have a set of data (alphabeticly sorted names) and I want to
try and get the closest hit based on the first letter of the last name. My
big problem was how to increment my search character. If I can't find 'W',
then try 'X' etc. If I hit 'Z' with no luck then start looking up for 'V',
then 'U'.
I have my char from the button.Tag.ToSt ring().ToArray( )[0]. I was trying to
convert it to an int, increment it, then try my search again. I was trying
to be too fancy with the shorthand ie (int)key++ etc but after a while my
code just became confusing. I was looking for a simple approach via a
method I wasn't familiar with or something.
thx
jack
"Jeff Louie" <je********@yah oo.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. . Second try... I just wrote this off the top of my pointed head. This code looks for a partial match in an array list. You could try to modify it.
////////// UNTESTED CODE ////////////////// using System; using System.Collecti ons;
namespace TestBinarySearc h { /// <summary> /// Summary description for Class1. /// UNTESTED CODE /// </summary> /// class MyCaseInsensiti veCompare : IComparer { // ASSERT x, y are strings not null // Case insensitive public int Compare(object x, object y) { return String.Compare( (string)x,(stri ng)y,true); } } class MyCompare : IComparer { // ASSERT x, y are strings not null // Case insensitive public int Compare(object x, object y) { int length= ((string)y).Len gth; if (((string)x).Le ngth < length) return -1; return String.Compare( (string)x,0,(st ring)y,0,length ,true); } } class Class1 { private ArrayList al= new ArrayList();
// ASSERT find not null public void FindIt(string find) { // Do BINARY_SEARCH to find any partial match al.Sort(new MyCaseInsensiti veCompare()); int index= al.BinarySearch (find, new MyCompare()); System.Console. WriteLine("Find : "+find); if (index < 0) System.Console. WriteLine("No Match."); else //got at least one match for criteria find& { System.Console. WriteLine("Outp ut");
// now do LINEAR_SEARCH for other matches int firstIndex= index; int lastIndex= index; MyCompare mc= new MyCompare(); for (int i=index-1; i>=0 && (mc.Compare(al[i],find)==0) ; i--) { firstIndex= i; } for (int i=index+1; (i<al.Count) && (mc.Compare(al[i],find)==0); i++) { lastIndex= i; } for (int i=firstIndex;i< =lastIndex;i++) { System.Console. WriteLine(al[i]); } } } //ASSERT al not null public Class1(ArrayLis t al) { this.al= al; } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { // // TODO: Add code to start application here // ArrayList al= new ArrayList(); al.Add("Louie,J eff"); al.Add("Smith,A nn"); al.Add("Smith,J "); al.Add("Smith,D ick,"); al.Add("Smith,P eter,"); al.Add("Smith,J an"); al.Add("Smith,T om"); al.Add("Smith,H arry"); al.Add("Smith,J oe"); al.Add("Smith,J oe"); al.Add("Smith,J ohn"); Class1 c1= new Class1(al); c1.FindIt("SMIT H,JOHN"); System.Console. ReadLine(); } } }
Regards, JeffI want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter.<
*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
You're rather vague on what your list looks up (you use "list.find" in
one spot and "this.dwPatient List.FindRow" in another), but it seems the
find/FindRow is inadequate for what you want to do, so don't use it.
char key = button.Tag.ToSt ring()[0];
// if you store the letter in the button's Text property, you can make
that even simpler:
// char key = button.Text[0];
MyRow found_row = null;
foreach (MyRow row in dwPatientList)
{
found_row = row;
if (row.sorted_nam e[0] >= key)
break;
}
The loop will exit when the first letter of found_row is equal to or
greater that key (which is what you what) -- or -- it will exit at the end
of the list, with found_row set to the last item (i.e., the first one BEFORE
key)
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Jack Addington" <ja********@sha w.ca> wrote in message
news:uw******** ******@TK2MSFTN GP10.phx.gbl... I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button
then it should scroll to the letter closest to H. If no one exists with H,
then go to I, etc. If its near the end, say 'V', and the last person is a 'T' then it should work its way back up the alphabet. I was trying to loop as if the Char's were ints but I am having problems
I have buttons with A .. Z that have the letter in the Tag field.
char key = button.Tag.ToSt ring().ToCharAr ray(1,1)[0]; // get the letter to search for ...
// work our way down the alphabet while (row == 0 && (int)key <= (int)'Z') { row = list.find (firstletter = key ) (int)key++; }
// if we didn't find something at least lower than the button pressed then try going up the alphabet if (row==0) { key = (char)(int)butt on.Tag.ToString ().ToCharArray( 1,1)[0] - 1; // work our way up the alphabet while (row == 0 && (int)key >= (int)"A") { row = this.dwPatientL ist.FindRow("le ft(sorted_name, 1)='" +
key + "'",row,dwPatie ntList.RowCount ); (int)key--; } }
Scroll to row
Jack.... I think your question has been answered. A linear search may
work on
a small data list, but is going to be very inefficient on a list of any
considerable size. A sorted list begs to be searched with a binary
search, not
a linear search. The code I posted attempts to provide a common
functionality, finding the set of rows with a partial match to a key in
a sorted
list. If you anticipate the list may grow significantly, then I would
seriously
consider a search algorithm other than a linear search. The most
efficient may
be to write your own binary_search that leaves you at the nearest match
if no
match is found. If your data is best described as buckets of data
organized by
letter, you could break up the data into buckets by letter (say an array
list for
each letter in the alphabet) and then go to the bucket that needs to be
searched. If the bucket is empty, it would be trivial to iterate to the
first
bucket of data that is not empty.
Regards,
Jeff I am tryingto simulate the contacts in outlook with the list of letters
down
the side. I have a set of data (alphabeticly sorted names) and I want to
try and get the closest hit based on the first letter of the last name<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
You are right... I wasn't very clear
I'm not writing the find routine... the dwXXX object does it already. I was
just trying to be generic when I put list.find at the top and forgot to do
it at the bottom.
What I was doing was trying to do was to iterate through the search term and
let the dwXXX object do the searching. My problem was coming up with a
clean and efficient syntax (not algorithm) as I am new to c# that let me
cleanly loop through the letters.
"James Curran" <Ja*********@mv ps.org> wrote in message
news:OO******** ********@TK2MSF TNGP14.phx.gbl. .. You're rather vague on what your list looks up (you use "list.find" in one spot and "this.dwPatient List.FindRow" in another), but it seems the find/FindRow is inadequate for what you want to do, so don't use it.
char key = button.Tag.ToSt ring()[0]; // if you store the letter in the button's Text property, you can make that even simpler: // char key = button.Text[0]; MyRow found_row = null;
foreach (MyRow row in dwPatientList) { found_row = row; if (row.sorted_nam e[0] >= key) break; }
The loop will exit when the first letter of found_row is equal to or greater that key (which is what you what) -- or -- it will exit at the end of the list, with found_row set to the last item (i.e., the first one BEFORE key) -- Truth, James Curran Home: www.noveltheory.com Work: www.njtheater.com Blog: www.honestillusion.com Day Job: www.partsearch.com (note new day job!)
"Jack Addington" <ja********@sha w.ca> wrote in message news:uw******** ******@TK2MSFTN GP10.phx.gbl... I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. If no one exists with H, then go to I, etc. If its near the end, say 'V', and the last person is a 'T' then it should work its way back up the alphabet. I was trying to loop as if the Char's were ints but I am having problems
I have buttons with A .. Z that have the letter in the Tag field.
char key = button.Tag.ToSt ring().ToCharAr ray(1,1)[0]; // get the letter to search for ...
// work our way down the alphabet while (row == 0 && (int)key <= (int)'Z') { row = list.find (firstletter = key ) (int)key++; }
// if we didn't find something at least lower than the button pressed then try going up the alphabet if (row==0) { key = (char)(int)butt on.Tag.ToString ().ToCharArray( 1,1)[0] - 1; // work our way up the alphabet while (row == 0 && (int)key >= (int)"A") { row = this.dwPatientL ist.FindRow("le ft(sorted_name, 1)='" + key + "'",row,dwPatie ntList.RowCount ); (int)key--; } }
Scroll to row
Jeff,
Sorry I'm not that clear. I'm not looking for a search/sort routine. My
object containing the data already has a find routine. Thats why I
generically called it list.Sort and accidently left the exact code
dwPatientList.F ind(...) in the lower section. I'm very sorry if you feel
I've wasted your time. Although I did spend a long time reading over your
earlier post.
What I was looking for was help with the c# syntax to 'neatly and
efficiently' increase/decrease the alphabetical character if the find was
unsuccessful. The data object I am using has a very very fast search so I
wasn't worried about not calling it over and over but with a different first
argument. As my list grows there is far more chance that I will pick up
each letter of the alphabet as a last name so the chance of doing more than
one search will diminish.
thanks so much for you time.
"Jeff Louie" <je********@yah oo.com> wrote in message
news:un******** *****@TK2MSFTNG P15.phx.gbl... Jack.... I think your question has been answered. A linear search may work on a small data list, but is going to be very inefficient on a list of any considerable size. A sorted list begs to be searched with a binary search, not a linear search. The code I posted attempts to provide a common functionality, finding the set of rows with a partial match to a key in a sorted list. If you anticipate the list may grow significantly, then I would seriously consider a search algorithm other than a linear search. The most efficient may be to write your own binary_search that leaves you at the nearest match if no match is found. If your data is best described as buckets of data organized by letter, you could break up the data into buckets by letter (say an array list for each letter in the alphabet) and then go to the bucket that needs to be searched. If the bucket is empty, it would be trivial to iterate to the first bucket of data that is not empty.
Regards, JeffI am tryingto simulate the contacts in outlook with the list of letters down the side. I have a set of data (alphabeticly sorted names) and I want to try and get the closest hit based on the first letter of the last name<
*** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
>Sorry I'm not that clear.<
Not a problem. What I was looking for was help with the c# syntax to 'neatly and
efficiently' increase/decrease the alphabetical character if the find
was unsuccessful.<
OK Have fun.
////////////// SOME CODE ///////////////////////////
using System;
namespace TestASCII
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[STAThread]
static void Main(string[] args)
{
string myString= "ABCXYabcxy z";
foreach (char ch in myString)
{
System.Console. Write(ch);
System.Console. WriteLine(Class 1.GetNextLetter (ch));
}
// USAGE
try
{
System.Console. WriteLine(Class 1.GetNextLetter (myString[0]));
System.Console. WriteLine(Class 1.GetNextLetter ('1'));
}
catch(Exception e)
{
System.Console. WriteLine(e);
}
System.Console. ReadLine();
}
//ASSERT inChar is a letter
public static char GetNextLetter(c har inChar)
{
if ((inChar>64) && (inChar<91)) // UPPER CASE
{
if (inChar == 90) {return (char)65;}
else return (++inChar);
}
else if ((inChar>96) && (inChar<123)) //lower case
{
if (inChar == 122) {return (char)97;}
else return (++inChar);
}
else throw new ArgumentOutOfRa ngeException();
}
}
}
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jeff Wagner |
last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read
a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that
Numerology program I wrote years ago in VB.
There are times I am totally stuck (for instance, I just had an idea to put the numerical values of
the alphabet and months of the year in a dictionary located in a function. Then, I can import the...
|
by: FIFO |
last post by:
Hi,
Can you suggest me a (time) efficient way to "rotate" a array of char?
In particular:
- The array of char have a fixed (26) length
- The array of char will not be modified in the code
- The rotation is by only one char to the right side
|
by: Nebula |
last post by:
Consider
enum Side {Back,Front,Top,Bottom};
enum Side a;
Now, why is
a = 124;
legal (well it really is an integer, but still,
checking could be performed..) ?
Wouldn't enums be more useful if there was a
bit more typechecking ?
|
by: Phillip N Rounds |
last post by:
I have an application which is heavily graphics intensive, all the graphics
being custom.
Scattered throughout by app, I have
MyView->OnDraw( this->GetDC() );
Apparently, each call to this->GetDC() creates a GDI object and, 16,000 or
so calls to OnDraw() results in the Application hanging because it can no
longer create any new GDI objects ( I know, 16,384 )
|
by: Rhino |
last post by:
I am dabbling with print CSS for the first time and I need some guidance.
The web pages on my site look fine - to my untrained eye - when displayed on
the monitor in any of the standard browsers. However, I am new to print CSS
and am having trouble getting the text on my printed pages to flow
correctly.
I know that I need a separate CSS for printing than the one I use for
displaying on the monitor; I know that is accomplished by having...
| |
by: vgame64 |
last post by:
Hi, I have been struggling with writing a program for a few hours. The
requirements are that: """You will be writing a program which will
determine whether a date is valid in
terms of days in that month. We are assuming that the year will be
valid 4 digit integer.
So you don't have to think much about that(in terms of validation)
except for the month
of February. If the month is February, then you have to check whether
that year is Leap...
|
by: Rob Cowie |
last post by:
Hi all,
I wish to generate a sequence of the form 'aaa', 'aab', aac'.... 'aba',
'abb', 'abc' etc. all the way to 'zzz'.
How would you construct a generator to acheive this?
A simple, working but somewhat inelegant solution is...
alpha = #shortened for brevity
|
by: Army1987 |
last post by:
Is this a good way to check wheter a file already exists?
#include <stdio.h>
#include <stdlib.h>
int ask(const char *prompt);
typedef char filename;
int main(int argc, char *argv)
{
FILE *in, *out;
|
by: moondaddy |
last post by:
Using c# 3.0:
I need to do a loop and for each iteration I need to call the next letter of
the alphabet. Sometimes this code may only loop 10 times, and other cases
it may loop 100 times.
I need to do something line this:
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |