char *s = "012345";
s[3] == ' ' // to compare it to a space
printf("%c", s[3]); // prints 3
if (lval_char != NULL)
{
char pos[MAX_INPUT_LENGTH];
char val[MAX_INPUT_LENGTH];
long posval = -1, valval = -1;
char lval1 [MAX_INPUT_LENGTH];
long lval2 = 0;
char buf2[MAX_INPUT_LENGTH];
//Try to grab an lvalue variable.
lval_var = get_variable (lval_char, buf);
line = one_argument (line, buf);
line = one_argument (line, pos);
line = one_argument (line, val);
sprintf (buf2, "DEBUG0: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
if (lval_var == NULL)
return FALSE;
else
sprintf(lval1, "%ld", lval_var->value);
sprintf (buf2, "DEBUG1: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
sprintf (buf2, "DEBUG2: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
//Now process the operator.
if ((oper = keyword_lookup (fn_evals, buf)) < 0)
{
sprintf (buf,
"Cmd_eval: prog %d syntax error(4.5): '%s'",
vnum, original);
bug (buf, 0);
return FALSE;
}
sprintf (buf2, "DEBUG3: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
if (!is_number (pos))
{
sprintf (buf,
"Cmd_eval: prog %d syntax error(4.5): '%s'",
vnum, original);
bug (buf, 0);
return FALSE;
}
else
posval = atoi (pos);
sprintf (buf2, "DEBUG4: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
sprintf (buf2, "DEBUG5: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
if (!is_number (val))
{
sprintf (buf,
"Cmd_eval: prog %d syntax error(4.5): '%s'",
vnum, original);
bug (buf, 0);
return FALSE;
}
else
valval = atoi (val);
sprintf (buf2, "DEBUG6: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
if(lval1[posval] == 0 || !is_number(lval1))
{
sprintf (buf,
"Cmd_eval: prog %d syntax error(4.5): '%s'",
vnum, original);
bug (buf, 0);
return FALSE;
}
sprintf (buf2, "DEBUG7: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
lval2 = lval1[posval];
sprintf (buf2, "DEBUG8: lval1: %s lval2: %ld line: %s pos: %s val: %s posval: %ld valval: %ld buf: %s",lval1, lval2, line, pos,val,posval,valval,buf);
bug (buf2, 0);
return (num_eval (lval2, oper, valval));
}
[*****] BUG: DEBUG1: lval1: lval2: 0 line: pos: 5 val: 7 posval: -1 valval: -1 buf: ==
[*****] BUG: DEBUG1: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: -1 valval: -1 buf: ==
[*****] BUG: DEBUG2: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: -1 valval: -1 buf: ==
[*****] BUG: DEBUG3: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: -1 valval: -1 buf: ==
[*****] BUG: DEBUG4: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: 5 valval: -1 buf: ==
[*****] BUG: DEBUG5: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: 5 valval: -1 buf: ==
[*****] BUG: DEBUG6: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: 5 valval: 7 buf: ==
[*****] BUG: DEBUG7: lval1: 237495 lval2: 0 line: pos: 5 val: 7 posval: 5 valval: 7 buf: ==
[*****] BUG: DEBUG8: lval1: 237495 lval2: 53 line: pos: 5 val: 7 posval: 5 valval: 7 buf: ==
void reverse(char s[]) {
int i, j;
char c;
for (i = 0, j = strlen(s) - 1;i < j;i ++, j –) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
void itoa(int n, char s[]) {
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i ++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i ++] = '-';
s[i] = '\0';
reverse(s);
}
if ( !((n /1000) % 3) ) {
//char at 3rd to last position is 3
}
itoa(lval_var->value,lval1);
sprintf(lval1, "%ld", lval_var->value);
if ( !((n /1000) % 3) ) {
//char at 3rd to last position is 3
}
if (itoa(2370392)[2] == '4') {
//is the 3rd character 4? no, it isn't
}
else
if (itoa(2370392)[6] == '9') {
//is the 7th character 9? yes, it is
}
short int getvalue(long number, short int position) { //no check if number is actually big enough to have that many position
long temp = number / (math.pow(10, position);
return temp mod 10;
}
if (itoa(2370392)[2] == '4') {
//is the 3rd character 4? no, it isn't
}
else
if (itoa(2370392)[6] == '9') {
//is the 7th character 9? yes, it is
}
i = 12345
class Integer
def get_digit(d)
(self / 10 ** (d-1)) % 10;
end
end
(1..5).each do |n|
puts i.get_digit(n)
end
int digit(int n, int d) {
return (n / (int)Math.Pow(10, d - 1)) % 10;
}
// digit(12345, 3); // Get 3rd digit
int var1 = 0;
var1 = lval_var->value / pow(10,length - posval);
lval2 = var1 % 10;
I'm trying to figure out how to take a number… hypothetically: "2370392"
and be able to compare the 3rd digit in that number to something else. 3rd digit is the example I'm using, but any position within a string I'd like to be able to isolate and compare (and manipulate with a different function).
More specifically, in our mprogs, we have a system of "variables" that stores a variable name and value onto a player. I'm wanting to be able to basically use this like array.
Under normal circumstances, it would work basically by defining and later being able to check, for example, "testvariable" to a specific number.
I'd like to be able to set testvariable to "2370392" (no problem with that, of course) but then have a check to be able to detect what the third digit or space or whatever in the number is, and do my if checks to see what it equals.
Likewise, being able to modify a specific digit would be necessary too.
Basically, what I want to be able to do is:
if varpos $n testvariable == 3 7
and in the number "2370392" it would check the third digit, see that it equals 7, and pass as a result.
The main thing that I'm running into is that the value for the variables is a long int, and any examples I've been able to find in the code of pulling a specific location in a string is, well, a string and not an int.
My initial thought was to be attempting something along the lines of taking the string that is processed
"testvariable" "operator" "arg1" "arg2"
and do something like:
pos = arg1
val = arg2
if(variable[arg1] == arg2)
blah
At least, that's more or less how I jotted down my thinking this through process, I know that's hardly representative of actual or working code, but still might be useful for illustrative purposes.
Things I've been able to detect is that in order to nab a specific part of a string, it has to be an array or a pointer, and that there hasn't been much of a way that I've been able to figure out how to convert that long int value into an array or pointer that I can nab a specific position out of to compare.
I feel like I'm ranting, it's late and I'm tired, and have been staring at this code for several hours and haven't been able to figure it out. Google hasn't been much help either.