McWhirter

I have a source text file in the following format (irrelevant fields removed)

CODE,DESC,COLOR,SIZE
1234,FRED,BLACK,8
1234,FRED,BLACK,10
1234,FRED,BLACK/WHITE,8
1234,FRED,BLACK/WHITE,10
1234,FRED,BLACK/BLACK,8
1234,FRED,BLACK/BLACK,10
1234,FRED,BLACK/BROWN,8
1234,FRED,BLACK/BROWN,10
1234,FRED,BLACK/BLUE,8
1234,FRED,BLACK/BLUE,10

I need to create a unique 3 character code, for each of the BLACK, BLACK/WHITE, BLACK/BROWN, BLACK/BLUE etc

So my result would look something like this

CODE,DESC,SHORTCOLOR,COLOR,SIZE
1234,FRED,BLK,BLACK,8
1234,FRED,BLK,BLACK,10
1234,FRED,B/W,BLACK/WHITE,8
1234,FRED,B/W,BLACK/WHITE,10
1234,FRED,B/B,BLACK/BLACK,8
1234,FRED,B/B,BLACK/BLACK,10
1234,FRED,BBW,BLACK/BROWN,8
1234,FRED,BBW,BLACK/BROWN,10
1234,FRED,BBL,BLACK/BLUE,8
1234,FRED,BBL,BLACK/BLUE,10

While the Code is the same, there can be no duplicated 3 character short color codes. As soon as the code is diferent, i can
use the same short character codes again.

I can handle the opening, exporting etc and everything else fine. I can't work out how to validate each code/color/shortcolour
variation as it is processed to that there are no duplicate short color codes.

Its made more difficult by the fact that the color field could be ANYTHING and will not always contain the same values..

I have no drama in creating a database file that stores created colour codes, so that they can be re-used at a later time if that is required.

Can anyone help me please
Cheers
Mc



Re: Visual Basic General Generating a Unique String

campbellony

Why use a random three character code, when you could use the six digit hexidecimal RGB value



Re: Visual Basic General Generating a Unique String

Swade

Keep in mind that a basic 32-bit application uses 32-bit colors. (Actually 24 bit + 8 bits for Alpha). Thus, if you want to capture EVERY UNIQUE COLOR in a 32-bit application, you would need something that encompases 2^24 = 16,777,216 unique colors.

As mentioned above using HEX, 16^6 = 16,777,216. thus I would suggest HEX also.

As you're doing it though, it can be done, but not using just the alphabet. Doing it with 3 letters, (26^3) only covers 17,756 colors, thus you are grossly short.

If you use the entire ASCII code, you can do it (256^3) = 16,777,216.






Re: Visual Basic General Generating a Unique String

DMan1

If the OP wants to use an integer value instead of an alpha code then there is a method to accomplish getting the unique integer for each color:

Dim AquaColorCode As Integer = Color.Aqua.ToArgb

Dim BlackColorCode As Integer = Color.Black.ToArgb






Re: Visual Basic General Generating a Unique String

McWhirter

Good suggestions, except that the program this data is being imported into, wants a 3 char text string. It must be a 3 char representation of the full colour, and each one must be unique.

Im starting to think im going to have to do it manually...





Re: Visual Basic General Generating a Unique String

Swade

I gave you a 3 char option. But they have to be ASCII Chars.




Re: Visual Basic General Generating a Unique String

McWhirter

So what is Aubergine/Modni Print in hex then



Re: Visual Basic General Generating a Unique String

SJWhiteley

I think you may have to come up with your own abbreviations, if you want all named colors. This means you'd need an array of color names and their 3 letter equivalent. There's no easy way to take any given random name and compress that name down to 3 characters...unless you perform a hash function: hopefully it won't give you any 3 character duplicates. Of course, the resultant 3 characters will not represent the name of the character (they will be obscure).

if you only want to use 3 letters maximum, then there are about 20 thousand combinations...(letters, numbers and space is around 50K combinations).






Re: Visual Basic General Generating a Unique String

Swade

McWhirter wrote:
So what is Aubergine/Modni Print in hex then

How exactly are you determining the color names Is someone choosing from a color wheel Are you just keeping a running table Where is the input coming from






Re: Visual Basic General Generating a Unique String

McWhirter

The source data is an export from a system. All the full colour names are manually entered and could be just about anything. The problem is that the destination system requires the short code for each colour, and it has to be either generated automatically. Then of course there can be no duplications.

I'm just going to do it manually i think.





Re: Visual Basic General Generating a Unique String

weirdbeardmt

I don't see why this would be too difficult, although it would be a bit brute forcey (not to be confused with the host of the generation game). You would need a hashtable and then a recursive function that generates a string from the given input.

This is completely untested as I've written it now, but something along these lines would be a starting point.

dim ht1 as new hashtable

dim ht2 as new hashtable

dim inputColour as string = "BLACK"

dim colourCode as string = GetColourCode(inputColour as string)

private function GetColourCode(inputColour) as string

dim cc as string

do

cc = GenerateColourCode(inputColour)

while not ht.containsValue(cc)

ht.add(inputColour,cc)

return cc

end function

private function GenerateColourCode(inputColour) as string

dim temp as string

for i as integer = 0 to inputcolour.length - 1

if temp.length < 3

temp = temp & inputcolour.substring(i,1)

else

temp = temp.substring(0,2) & inputcolour.substring(i,1)

if temp.length = 3 andalso not ht2.containsValue(temp)

ht2.add(inputColour,temp)

return temp

next

end function

Like I said, that's right off the top of my head and no doubt it could be refined a lot, but it ought to be a starting point.






Re: Visual Basic General Generating a Unique String

McWhirter

I ended up doing the final conversion of the colours manually, but this will probably help with future processes that include new colours not in the inital application.

It might also help me with another issue i found today.

Cheers :)