I am finally getting around to writing stuff in cfscript, and so I am starting with writing some needed formatting functions. Here is an example:
Function FormatBoolean(MyBool, Format) {
Switch(Format){
Case "YES/NO":{
If (MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
What I would like to do is make Format an optional argument. If you don't include the argument, the function will currently still run, but it won't find format, and it seems that cfparam did not get translated to cfscript.
Will I just have to check if Format is defined and give it a value? Or is there a nicer way of doing this?
Thanks
Personally I prefer to set defaults to this kind of arguments. Also I've refactored function a bit... But not tested :)
function FormatBoolean(required any MyBool, string Format = "") {
switch(arguments.Format) {
case "YES/NO":
return YesNoFormat(arguments.MyBool EQ 1);
default:
return (arguments.MyBool eq 1) ? "Yes" : "";
}
}
Please note that (arguments.MyBool EQ 1) may be replaced with (arguments.MyBool), so it covers all boolean values. You may be interested to make it more reliable, something like this (isValid("boolean", arguments.MyBool) AND arguments.MyBool) -- this should allow to check any value at all.
All variables passed into a function are available to access programmatically via the ARGUMENTS scope. You can refer to it as if it were an array (because it is), as well as standard struct key access (which I've done for you below for the MyBool parameter):
<cfscript>
Function FormatBoolean(MyBool) {
var theFormat = '';
if (ArrayLen(ARGUMENTS) GT 1)
theFormat = ARGUMENTS[2];
Switch(theFormat){
Case "YES/NO":{
If (ARGUMENTS.MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (ARGUMENTS.MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
</cfscript>
Add your preferred additional levels of data validation as necessary.
Related
I a newbie C++ programmer trying to test aruments/parameters passed to a program.
Multiple arguments can be passed to the program, however I want to test that if certain arguments are passed then other arguments become invalid.
e.g. PGM accepts arg(1) arg(2) arg(3) arg(4) arg(5) etc...
if arg(1) and arg(2) are supplied then arg(3), arg(4) and arg(5) etc... are invalid and the program should terminate with an error message if they are also supplied along with arg(1) and arg(2).
I've thought that using boolean IF tests would be a good way to check if certain values are true/false.
I searched on stackoverflow but not found an answer that encompasses exactly what i'm trying to do. If someone can point me in the right direction or suggest a far more efficient way of doing this I would be very grateful.
My code currently looks like this:
bool opt1 = false;
bool opt2 = false;
bool opt3 = false;
bool opt4 = false;
bool opt5 = false;
for(int i=1; i<argc; i++) {
char *str = argv[i];
if (strcmp (str, "-opt1:")==0) {opt1 = true;}
else if (strcmp (str, "-opt2:")==0) {opt2 = true;}
else if (strcmp (str, "-opt3:")==0) {opt3 = true;}
else if (strcmp (str, "-opt4:")==0) {opt4 = true;}
else if (strcmp (str, "-opt5:")==0) {opt5 = true;}
}
if((opt1) && (opt2) && (~(opt3)) && (~(opt4)) && (~(opt5)) {
** DO SOMETHING **
} else {
** DISPLAY ERROR MESSAGE AND USAGE TEXT **
}
A good solution would be using operands ! and &&
! denotes "not" (or in such case "not true") while && combines two different logical comparisons (in such case, "logic test 1" and "logic test 2")
Here's an example to do it:
if((opt1 && opt2)&&(!(opt3||opt4||opt5))){
/*
Do something if opt1 and opt2 are true and others are false
*/
}
This is practically the same as #Fareanor's solution above (first solution)
A possible fix could be (if I have well understood your problem):
if(opt1 && opt2) // opt3, opt4 and opt5 are invalid
{
if(!(opt3 || opt4 || opt5))
{
// Do something
}
else
{
// Display error message because at least opt3 or opt4 or opt5 is provided and not requested
}
}
else // opt3, opt4 and opt5 are valid
{
// Do something
}
But I think it could be better to just ignore the obsolete parameters instead of display an error while you can still run your process with only opt1 and opt2. Which could lead us to the simpler code:
if(opt1 && opt2)
{
// Do something without using opt3, opt4 and opt5
}
else
{
// Do something taking into account opt3, opt4 and opt5
}
I hope it is what you was looking for.
I am using getopt to parse command line arguments and my issue is that some of my options have options. My project is to test different backend implementations of maps and the -b flag specifies which implementation to use. Most of the options are straight forward but for the backends that use hash tables (chained and open) there is an additional -number that can be added to the end to specify the load factor. So it would be -b chained-0.75.
My idea is that I would take the substring from 8 to the end (or 5 for the "open" option) because that would ignore the "chained-" part of the string and then use atof() to convert it to a double and then declare my map. I believe optarg is a char array (?) and I keep running into type mismatch errors even though I have tried std::string str(optarg); I also don't know what to write in place of else if (strcasecmp(optarg, "chained") == 0) because there could be any number at the end of it. So right now when I do -b chained-0.75 it calls the usage function.
Here is what I have so far:
while ((c = getopt(argc, argv, "hb:n:p:")) != -1) {
switch (c) {
case 'b':
if (strcasecmp(optarg, "unsorted") == 0) {
map = new UnsortedMap();
} else if (strcasecmp(optarg, "sorted") == 0) {
map = new SortedMap();
} else if (strcasecmp(optarg, "bst") == 0) {
map = new BSTMap();
} else if (strcasecmp(optarg, "unordered") == 0) {
map = new UnorderedMap();
} else if (strcasecmp(optarg, "chained") == 0) {
double load_factor;
std::string str(optarg);
std::string ld_str = str.substr(8, str.length()-1);
load_factor = atof(ld_str);
map = new ChainedMap(load_factor);
} else if (strcasecmp(optarg, "open") == 0) {
map = new OpenMap();
} else {
usage(1);
}
break;
Any hints or ideas would be appreciated!
strcasecmp() is an exact match comparison function, this strcasecmp() will obviously not match "chained-0.75". The only thing that strcasecmp() will match against the string "chained" is "chained", not "chained-0.75", not "changed-foobar", not "chained-anything".
The right function is strncasecmp:
} else if (strncasecmp(optarg, "chained-", 8) == 0) {
Note that you're comparing against "chained-", and not just "chained". A few moments' of thinking should make it clear why.
The existing code also fails to take into account the possibility that the string after "chained-" is not a number, since atof() does not handle parsing errors. If you need to be able to detect and handle an error here, use strtod() instead of atof().
I'm trying to implement a very basic clone of redis in C++. So when I get the queries, I need to parse those. Right now I am doing this:
void Query::buildQuery(){
std::string query_type = lower(args[0]);
if(query_type == "get"){ //do something }
else if(query_type == "set"){ //do something }
else if(query_type == "getbit"){ //do something }
else if(query_type == "setbit"){ //do something }
else if(query_type == "zadd"){ //do something }
else if(query_type == "zcard"){ //do something }
else if(query_type == "zcount"){ //do something }
else if(query_type == "zrange"){ //do something }
else if(query_type == "save"){ //do something }
else { throw(QueryException("Invalid query type")); }
}
Is there any other, shorter way to do this? I don't want to include any other library than the STL.
If those do_somethings can be extracted into separate methods, then you could create a pre-initialized hash map (unordered_map) from string to pointer to member function and do something like
(this->*queryHandlers[query_type])();
You'll have to choose between lots of functions and one large function, though.
If you're running on an Intel/AMD processor and feeling 'brave' you might like to take a look at these implementations of strcmp, strlen, etc that use SSE instructions. That'd be quicker.
As for the general structure, you could turn "set" & "do something" into an class which has a test method and a do-something method. Have an array of those, and iterate across it calling the test method passing query_type. The test method for the object would automatically call the do-something method if it matches the string.
Without if...else if, you can do this by switch statement. Like:
void Query::buildQuery(){
std::string query_type = lower(args[0]);
switch(str2int(query_type) ){
case str2int("set"):// do something
break;
case str2int("getbit"):// do something
break;
................
..........
default:
throw(QueryException("Invalid query type"));
}
}
According to Serhiy where str2int is like:
constexpr unsigned int str2int(const char* str, int h = 0)
{
return !str[h] ? 5381 : (str2int(str, h+1)*33) ^ str[h];
}
I receive via POST a value. Then, I´m comparing the value received (1, 2, 3, 4, 5) with variables pre defined in my code.
Is it possible to do it with FOR or another way to simplify it without changing the functionality of the code?
Yes, I need to receive the value as number and compare it with variables (no MYSQL).
I set on each test the name, eg: $varname = "Paul";
Here´s what I´m doing and what I´d like to change.
Thanks a lot
// from previous page with input name thenumber
$thenumber = $_POST['thenumber'];
$option1 = "1";
$option1 = "2";
$option1 = "3";
$option1 = "4";
$option1 = "5";
$option1 = "6";
...
...
More options
if($thename == $option1)
{
$varname = "Paul";
}
else if ($thename == $option2)
{
$varname = "Louie";
}
else if ($thename == $option3)
{
$varname = "Dimitri";
}
...
...
...
It would be much cleaner to do this with a switch.
I don't think using a for loop will be a good idea.
Be sure to put a break after each case ends.
The default case is when $thename is none of the numbers in the cases.
switch ($thename) {
case 1:
$varname = "paul";
break;
case 2:
$varname = "Louie";
break;
case 3:
$varname = "Dimitri";
break;
...
default:
$varname = "default_name";
break;
}
I would like to transform my if() conditional to switch() from this:
if($configuration['application'][$applicationName]['subdomain'] == true){
foreach($configuration['language'] as $language){
if($language['abbreviation'].'.'.$configuration['application'][$applicationName]['domain'] == $_SERVER['HTTP_HOST']){
$_SESSION['language'] = $language['abbreviation'];
}
}
// If no subdomain detected and redirection is enabled, set default language
if(!isset($_SESSION['language'])){
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
}
}
else {
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
}
To this:
switch($configuration['application'][$applicationName]['subdomain']){
case true:
foreach($configuration['language'] as $language){
if($language['abbreviation'].'.'.$configuration['application'][$applicationName]['domain'] == $_SERVER['HTTP_HOST']){
$_SESSION['language'] = $language['abbreviation'];
break;
}
}
default:
$_SESSION['language'] = $configuration['application'][$applicationName]['language'];
break;
}
I think it should be the same but it behaves differently ...
Switch is not working properly ...
I have reformatted your code, please check to make sure it is still correct.
As for your problem, to begin with you are missing a break; statement at the end of your case true: statement. (The break inside the foreach loop simply breaks out of that loop, not the case itself).