Comparison of Hex Numbers in Tcl - hex

I am trying to compare the two following hex numbers in a TCL Script:
0x00001111
32'h00001111
They should be equal to each other, but I am having trouble comparing them because of the difference in formatting.
I have tried using "scan" command to convert, but have not gotten the correct output I am looking for.
Code snippet:
set read1 0x00001111
set read2 32'h00001111
set new_read [scan $read2 %x]
if ($read1 == $read2) {
puts "Two values equal"
}
This code does not work and sets new_read to 4369. Any help is appreciated

You want to change the format specifier to scan to sth. like:
if {[scan $read2 "%d'h%x" bw new_read] == 2} {
# on success
} else {
# on failure
}
This way, you will be able to compare scan $read1 %x and $new_read using == & friends.

Related

Length Zero in r

Greetings I am getting an error of
Error in if (nrow(pair) == 0) { :argument is of length zero
I have checked the other answers but do not seem to work on a variable like mine. Please check code below, please assist if you can.
pair<-NULL
if(exists("p.doa.ym")) pair <- rbind(pair, p.doa.ym[,1:2])
if(exists("p.doa.yd")) pair <- rbind(pair, p.doa.yd[,1:2])
if(nrow(pair) == 0) {
print("THERE ARE NO MATCHES FOR TODAY. STOP HERE")
quit()
}
Since you set pair=NULL and then it might happen that pair stays null if those two if statements are not true, you either need to check if pair is null first, or you could set pair to an empty data frame, or something else.
One option:
if (!is.null(pair)) {
if (nrow(pair)==0) {
# your code
}
}
Another option:
pair=data.frame()
# your code

Select any character string over an NA in an If statement in R

I am trying to create a function which will look at two vectors of character labels, and print the appropriate label based on an If statement. I am running into an issue when one of the vectors is populated by NA.
I'll truncate my function:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b)}
if(is.na(b)) {print(a)}
if(a=="BW"& b=="BW",) {print("BW")}
if(a=="?BW"& b=="BW") {print("?BW")}
...#and so on
}
Some data:
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = TRUE)
The function works fine for the first two, selecting the label I've designated in my if statements. However, when it gets to the third pair I receive this error:
Error in if (a == "?BW" & b == "BW") { :
missing value where TRUE/FALSE needed
I'm guessing this is because at that place, b=NA, and this is the first if statement, outside of the 'is.na' statements, that need it to ignore missing values.
Is there a way to handle this? I'd really rather not add conditional statements for every label and NA. I've also tried:
-is.null (same error message)
-Regular Expressions:
if(a==grepl([:print:]) & b==NA) {print(a)}
In various formats, including if(a==grepl(:print:)... No avail. I receive an 'Error: unexpected '[' or whatever character R didn't like first to tell me this is wrong.
All comments and thoughts would be appreciated. ^_^
if all your if conditions are exclusives, just call return() to avoid checking other conditions when one is met:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b);return()}
if(is.na(b)) {print(a);return()}
if(a=="BW"& b=="BW",) {print("BW");return()}
if(a=="?BW"& b=="BW") {print("?BW");return()}
...#and so on
}
You need to use if .. else statements instead of simply if; otherwise, your function will evaluate the 3rd and 4th lines even when one of the values is n/a.
Given you mapply statement, I also assume you want the function to output the corresponding label, not just print it?
In that case
eventTypepriority<-function(a,b) {
if(is.na(a)) b
else if(is.na(b)) a
else if(a=="BW"& b=="BW") "BW"
else if(a=="?BW"& b=="BW") "?BW"
else "..."
}
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = T)
c
returns
Pm BW ?BW
"..." "..." "?BW"
If you actually want to just print the label and have your function return something else, you should be able to figure it out from here.

Compare each member of a vector with its number

The problem might be simple, but I cant solve it:
I got numeric vector, i need to compare its maximum/2 with each member except its maximum and if all these comparisons are FALSE , a=a+1.
I got these:
comp=c(6.674971, 11.208241, 18.296459, 5.165752, 123.000000)
a=0
if (max(comp)/2<comp[comp < max(comp)]){
a=a+1
}
a
Thank you in advance.
The condition is true if :
sum(2*comp > max(comp)) ==1
In this case , I would write it like this without using ìf:
a <- (sum(2*comp > max(comp)) ==1) + 1
I don't know if you want to add 1 every time you find a item bigger than max/2, which is what I am showing you. In case you want all to be false, add this line at the end.
if (a>0)
a=1;
I think this will work, haven't compiled it because here I don't have R compiler, but this is the idea:
comp=c(6.674971, 11.208241, 18.296459, 5.165752, 123.000000)
a=0
temp=comp/max(comp)
for (n in 1:length(comp))
{ if ((temp(i)>max(comp)/2)&&(temp(i)!1))
a=a+1
}
Hope it helps.

Unit test failing on two equal Qstrings (one is read from a file)?

I'm trying to compare two string which are supposed to be the same. The test however fails.
I'm testing if the file get set correctly by the router.setForwarding(true) method.
Here is the code of the test.
void router_test::testSetForwarding_true()
{
QFile myfile("/proc/sys/net/ipv4/ip_forward");
myfile.open(QIODevice::ReadOnly | QIODevice::Text);
router->setForwarding(true);
QString forward = QString(myfile.readAll());
QCOMPARE(QString("1"),forward);
}
As a result I get:
FAIL! : router_test::testSetForwarding_true() Compared values are not the same
Actual (QString("1")): 1
Expected (forward): 1
Why aren't they equal?
As you can glean from the output, you have interchanged the actual and expected values. You're also comparing the newline-terminated output against one without a newline.
This should work:
QCOMPARE(forward, QString("1\n"));
or
QCOMPARE(forward[0], QChar('1'));

awk; comparing time -- puzzling behavior of strftime

Use GNU-Awk (gawk) in UNXUTILS on a Win-7 PC. This question deals with strftime(.) and a comparison of times using that.
Have followed the discussion on how to compare strftime values because I have a similar problem. In financial market data I have a date-time string ($25) given as "03-APR-2006 09:55:25" (so time = substr($25, 13, 8)) and my objective is to count records (especially order cancellation records) that come in after 14:00:00 (2 pm).
In my code I have a line which reads
{ if ($3==3) {
{ ++CK_NR}
{ ++CO_NR[$4, substr($25, 1, 11)] }
{ if (strftime(substr($25, 13, 8)) > ("14:00:00"))\
{
{ ++CK_LATE_NR }
{ ++CO_LATE_NR[$4, substr($25, 1, 11)] }
}
}
}}
Just realized that the inequality I used -- if (strftime(substr($25, 13, 8)) > ("14:00:00")) -- has only a string in the RHS, and I didn't make this string the argument of another strftime(.). What's puzzling is that it did NOT give me an error.
Am concerned that while it has not generated any errors, and the code has run, perhaps it is giving me something other than what I intended with the code. In a Command Prompt Window, doing
gawk "{ print (strftime(\"09:55:25\") > (\"14:00:00\")) }"
does yield "0" and
gawk "{ print (strftime(\"09:55:25\") < (\"14:00:00\")) }"
does yield "1". The GNU Awk Manual (http://www.gnu.org/software/gawk/manual/gawk.html#Time-Functions) yields little information on what is required for a meaningful comparison. Just now tried the above deleting the "strftime" even from the LHS, as under
gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
and
gawk "{ print ((\"09:55:25\") > (\"14:00:00\")) }"
and got the same results. I want to be sure that I am getting the correct True/False result because GAWK is comparing time, and not for some other internal rule it uses in making string comparisons (causing the limited test to be only a coincidence). Can someone resolve this puzzle? Thanks. Best,
Murgie
You seem to be using string comparisons. Strings are compared by comparing the first character of each, then the second character of each, and so on. Thus, "10" is less than "9", since the ASCII value of "1" is less than that of "0", see http://www.gnu.org/software/gawk/manual/gawk.html#Variable-Typing and http://en.wikibooks.org/wiki/An_Awk_Primer/Search_Patterns_%282%29#Logic_Operators
If you want to do numeric comparison for strings on the form "xx:yy:zz" (for instance "10:22:45") then you can try to convert the string to a number first using gsub(/:/,"",str)

Resources