Keeping computer hostname in sequence

B

bthumber

Problem - My application is design to count the total number of records of a
"like" kind in the database, then increment that hostname/number by one.

Example - if no records are deleted and the last record is buffal10 the next
record will be buffal11. Total number of records 11.

Problem - if you delete a record , say buffal05, the count would be 10
records but the last hostname ends in 11. This causes an error in the
application.

Status of Program - I have say 50 records (buffal50) and 10 records are
deleted.

Resolution - Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time?
 
J

Jeff Johnson

Problem - My application is design to count the total number of records of
a
"like" kind in the database, then increment that hostname/number by one.

Example - if no records are deleted and the last record is buffal10 the
next
record will be buffal11. Total number of records 11.

Problem - if you delete a record , say buffal05, the count would be 10
records but the last hostname ends in 11. This causes an error in the
application.

Status of Program - I have say 50 records (buffal50) and 10 records are
deleted.

Resolution - Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time?

I don't understand exactly what you're trying to do. In the example above,
do you want to rename buffal06 through buffal11 to buffal05 through
buffal10? Why does the "application" care about the names of these things? I
dealt with something similar several years ago. I was creating a collection
that was ordered but could have "holes" in it. Another programmer (I
hesitate to actually apply that label to him) turned the collection into an
array(!) and then tried to equate numbers in the collection to array indices
instead of just iterating the collection. Needless to say there were errors.
Also needless to say, this guy was an idiot. So given my prior experience, I
can't help but think this "application" is kind of brain-dead and should be
re-designed. But then you didn't give anywhere near enough detail about it
to let me make a reasonable judgement.
 
B

Ben Voigt [C++ MVP]

bthumber said:
Problem - My application is design to count the total number of records of
a
"like" kind in the database, then increment that hostname/number by one.

Example - if no records are deleted and the last record is buffal10 the
next
record will be buffal11. Total number of records 11.

Problem - if you delete a record , say buffal05, the count would be 10
records but the last hostname ends in 11. This causes an error in the
application.

Status of Program - I have say 50 records (buffal50) and 10 records are
deleted.

Resolution - Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time?

You can test each possible name starting with 01, that's the best way I know
of to find the holes.

But I think I would leave the holes and just handle the error by adding +1
until I found the next unused entry.
 
F

Family Tree Mike

bthumber said:
Problem - My application is design to count the total number of records of a
"like" kind in the database, then increment that hostname/number by one.

Example - if no records are deleted and the last record is buffal10 the next
record will be buffal11. Total number of records 11.

Problem - if you delete a record , say buffal05, the count would be 10
records but the last hostname ends in 11. This causes an error in the
application.

Status of Program - I have say 50 records (buffal50) and 10 records are
deleted.

Resolution - Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time?

Which part are you having the problem?

The way I see it, you can loop as long as the integer at the end of the
last hostname is larger than the count in the list.

If the last index needs to change, then just look for the first int that
is not found in the list. When the "empty slot" is found, change the
name of the highest entry to that slot.

I suspect you are not really trying to change "hostnames" from the
remote computer.
 
B

bthumber

Jeff, what info do you think you need? If re-designing was a option I might
have did that, but that not the case, so lets stick to the problem at hand.
 
B

bthumber

Ben,

Can you give me example of what you mean...like use a while(loop) then if
(something){do something}?
 
J

Jeff Johnson

Jeff, what info do you think you need?

I think I need to know what you mean by the statement "Part would be fill in
the hostname that are out of sequence. Question is how would do that, one
record at a time?" That's not clear English, so I'm not positive what you're
trying to do.
 
B

bthumber

<<Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time? >>

That was my guest at part of the resolution to the problem. <<<<Part of the
resolution would be to fill in the hostnames that are out of sequence. The
question is, how would you do that?>>>> Ok, that clears that up. Now back to
the question!
 
J

Jeff Johnson

<<Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time? >>

That was my guest at part of the resolution to the problem. <<<<Part of
the
resolution would be to fill in the hostnames that are out of sequence.
The
question is, how would you do that?>>>> Ok, that clears that up. Now back
to
the question!

WHAT question??? YOU know what it is that you want to do, but WE don't read
minds in this group. You are not explaining clearly what you want the final
outcome to be. Give a concrete example.
 
F

Family Tree Mike

bthumber said:
<<Part would be fill in the hostname that are out of sequence.
Question is how would do that, one record at a time? >>

That was my guest at part of the resolution to the problem. <<<<Part of the
resolution would be to fill in the hostnames that are out of sequence. The
question is, how would you do that?>>>> Ok, that clears that up. Now back to
the question!

See if this does what you want. If not, please specify in what way this is
not what you require.

class Program
{
static int GetID(string host, string basename)
{
// put in error checking....
string id = host.Substring(basename.Length);
return int.Parse(id);
}

static void Main(string[] args)
{
string basename = "buffal";
List<string> hosts = new List<string>
{ "buffal01", "buffal03", "buffal04", "buffal08", "buffal10" };

int last = hosts.Count - 1;
while (GetID(hosts.Last(), basename) > hosts.Count)
{
Console.WriteLine("Need to fix {0}", hosts.Last());

for (int idx = 1; idx <= hosts.Count; ++idx)
{
string name = string.Format("{0}{1:d2}", basename, idx);
if (hosts.IndexOf(name) == -1)
{
Console.WriteLine("Changing name to {0}.", name);
hosts [hosts.Count - 1] = name;
hosts.Sort();
break;
}
}
}

foreach (string n in hosts) Console.WriteLine(n);
Console.ReadLine();
}
}
 
B

Ben Voigt [C++ MVP]

Something like:

i = 0;
while (true)
{
try {
string id = "buffal" + (i++).ToString();
db.AddNewRecord(id, other, fields, here);
return id;
} catch (DatabaseKeyNotUniqueException) {}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top