I just wanted to know if there is a way to improve this for loop, by skipping those if somehow.
Var String
can have more parameters and their order can be casual.
In order to replace the param with a real value, I need :
- To split it
- To check what parameter is and in which position
Here is a synthetic example on how I thought it:
String = "{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}"
String_split = String.split("_")
for params in range(len(String_split)):
if "FullDate" in String_split[params]:
# Do something
elif "Name" in String_split[params]:
# Do something
elif "ElementID" in String_split[params]:
# Do something
elif "ElementCD" in String_split[params]:
# Do something
elif "Year" in String_split[params]:
# Do something
elif "Day" in String_split[params]:
# Do something
elif "Month" in String_split[params]:
# Do something
UPDATE: That's what i would like to accomplish
# Default values
FullDate = now().format("yyyy-MM-dd_HH:mm:ss")
Name = "John"
ElementID = "Apple"
ElementCD = "01Appxz"
Year = now().format("yyyy")
Day = now().format("dd")
Month = now().format("MM")
############################
String = "{FullDate}_{Month}_{Day}_{Year}_{ElementID}_{ElementCD}"
String_split = String.split("_")
for params in range(len(String_split)):
if "FullDate" in String_split[params]:
Report_Name = Report_Name + FullDate + "_"
elif "Name" in String_split[params]:
Report_Name = Report_Name + Name + "_"
elif "ElementID" in String_split[params]:
Report_Name = Report_Name + ElementID + "_"
elif "ElementCD" in String_split[params]:
Report_Name = Report_Name + ElementCD + "_"
elif "Year" in String_split[params]:
Report_Name = Report_Name + Year + "_"
elif "Day" in String_split[params]:
Report_Name = Report_Name + Day + "_"
elif "Month" in String_split[params]:
Report_Name = Report_Name + Month + "_"
# Report_Name must return default values, ordered by String variable (eg: FullDate, 1st position; Month 2nd position etc..)
# >> "1999-01-01_10:10:29_01_01_1999_Apple_01Appxz"
# if the String variable changes the params order to
# String = "{Year}_{Month}_{ElementCD}_{FullDate}_{ElementID}_{Day}"
# Report_Name should return
# >> "1999_01_01Appxz_1999-01-01_10:10:29_Apple_01"