C
C#12mo ago
mrdudebro1

Branching if statements

so. i have some data in a database that needs to be assigned data based on an id. I have a database full of items, and each item has attributes. lets say they are tools. the id could be HAMMERSTEEL.10 or HAMMERIRON12.12. the first one means it's a 10" long steel hammer with a standard head size and the second one is a 12" long iron hammer with a 12mm head or something of that nature. i have at least four different base types HAMMER AXE SCREWDRIVER. I need to assign attributes in a new table based on the item id's. what i already have is a database filled with what i want the data to look like. i'll read in hte item id HAMMERSTEEL.10 and then i'll asign the attributes in another table. for example:
if(itemId.matches `hammerregex`) {
// build an update statement in sql that will assign the appropriate values
}

if(itemId.matches `axeregex) {
//build an update statemet in sql that will assign appropriate axe values
}
if(itemId.matches `hammerregex`) {
// build an update statement in sql that will assign the appropriate values
}

if(itemId.matches `axeregex) {
//build an update statemet in sql that will assign appropriate axe values
}
I want to cut down on the amount of branching if statements. any suggestions?
12 Replies
Angius
Angius12mo ago
If you could reasonable split those strings into item, material, size then sure, you could use a switch If you rely on regex, then no if...else chain it is If the items are limited, you could reasonably split it with reges, ig (hammer|axe|screwdriver)\w+(0-9\.)+ or some such
mtreit
mtreit12mo ago
Storing things like the length in the textual ID seems like a kind of questionable design choice.
Angius
Angius12mo ago
But with capture groups Oh yeah, I would absolutely never store data as JOHNDOE42
mrdudebro1
mrdudebro1OP12mo ago
yeah. it's a problem i can't really fix immediately that's the way it was before i got here interesting. i guess that's the best way to do it yeah i can reliably do that. i just hate the idea of branching if statements
Angius
Angius12mo ago
https://regex101.com/r/uy8HRT/1 this regex seems to be doing working Again, assuming the list of tools is limited
mrdudebro1
mrdudebro1OP12mo ago
i see what you did there. yes it will be limited by what i know works in our database everything else will be addressed afterwards.
Angius
Angius12mo ago
https://regex101.com/r/FOMAob/1 will work, actually The previous one didn't work for SCREWDRIWERCOPPER18.
mrdudebro1
mrdudebro1OP12mo ago
actually that's pretty nifty mind if i copy and play with it to make it match what i actually am working with
Angius
Angius12mo ago
Go ahead
mrdudebro1
mrdudebro1OP12mo ago
ty Any recommendations?
mtreit
mtreit12mo ago
Store it in a separate column / field?
mrdudebro1
mrdudebro1OP12mo ago
Lol. Well thats what im gonna do now. Idk what happened 5 years ago to make them do this Thanks anyways Also the fact of the matter is its a database that we didnt design I have to convert the data from a limited CRM/ERP system to usable for an ecommerce site
Want results from more Discord servers?
Add your server