I am working on Python2.7.6 and came across the following problem:
x=eval(input("Enter a number between 0 and 1: "))
Here, the input is supposed to create a string, but it's not running unless I wrap input in single quote marks too, check out the following:
x=eval('input("Enter a number between 0 and 1: ")')
Can someone please clarify why the first code wasn't running and the second one worked? It's really frustrating...I'd appreciate your help!
In Python 2, input is just the composition of eval and raw_input. In effect, your first line is:
x=eval(eval(raw_input("Enter a number between 0 and 1: ")))
Typing in 123 will result in the first call to eval passing the integer 123 into the second eval function, which throws a nice error:
TypeError: eval() arg 1 must be a string or code object
Typing in '123' will make the string pass through unmodified, since eval("'123'") == '123'. Since you don't want to evaluate anything, just use raw_input:
x = raw_input("Enter a number between 0 and 1: ")
Related
I'm looking for some argument (ARG) such that this code:
A = 5
B = OCONV(A,'ARG5')
PRINT B
will print to the screen
00005
Anybody know something which will do this for me?
In Universe I would use the MR% conversion code. Just be aware that it will truncate anything longer than 5 characters.
A = 5
B = OCONV(A,'MR%5')
PRINT B
I use this a lot when I need to use EVAL in a conditional or as an aggregate function in a SQL or other TCL statement like to find the record with the most fields in a file.
SELECT MAX(EVAL "DCOUNT(#RECORD,#FM)") FROM VOC;
SELECT MAX(EVAL "OCONV(DCOUNT(#RECORD,#FM),'MR%8')") FROM VOC;
Masking aside these generally return 2 different values on our system.
I am using UniData, but looking at the commands reference manual I can't see anything quite right, in terms of one simple argument to OCONV, or similar. I came up with these (somewhat kludgy) alternatives, though:
NUMLEN=5
VALUE=5
PRINT CHANGE(SPACES(NUMLEN-LEN(VALUE))," ","0"):VALUE
Here you are using the SPACES function to create that amount of space characters and then convert them to zeros.
PRINT OCONV(VALUE,"MR":NUMLEN:"(#####)")
This is using OCONV but has to define a string with the "mask" to only shew the final 5 digits. So if NUMLEN changes then the mask string definition would have to change.
PRINT OCONV(VALUE,"MR":NUMLEN)[3,NUMLEN]
This version uses OCONV but prints starting at the 3rd character and shews the next NUMLEN characters, therefore trimming off the initial "0." that is made by using the "MR" parameter
PADDED.VALUE = VALUE 'R%5' is the simplest way to do this.
There's a string with a (single) number somewhere in it. I want to increment the number by one. Simple, right? I wrote the following without giving it a second thought:
sub("([[:digit:]]+)", as.character(as.numeric("\\1")+1), string)
... and got an NA.
> sub("([[:digit:]]+)", as.character(as.numeric("\\1")+1), "x is 5")
[1] NA
Warning message:
In sub("([[:digit:]]+)", as.character(as.numeric("\\1") + 1), "x is 5") :
NAs introduced by coercion
Why doesn't it work? I know other ways of doing this, so I don't need a "solution". I want to understand why this method fails.
The point is that the backreference is only evaluated during a match operation, and you cannot pass it to any function before that.
When you write as.numeric("\\1") the as.numeric function accepts a \1 string (a backslash and a 1 char). Thus, the result is expected, NA.
This happens because there is no built-in backreference interpolation in R.
You may use a gsubfn package:
> library(gsubfn)
> s <- "x is 5"
> gsubfn("\\d+", function(x) as.numeric(x) + 1, s)
[1] "x is 6"
It does not work because the arguments of sub are evaluated before they are passed to the regex engine (which gets called by .Internal).
In particular, as.numeric("\\1") evaluates to NA ... after that you're doomed.
It might be easier to think of it differently. You are getting the same error that you would get if you used:
print(as.numeric("\\1")+1)
Remember, the strings are passed to the function, where they are interpreted by the regex engine. The string \\1 is never transformed to be 5, since this calculation is done within the function.
Note that \\1 is not something that works as a number. NA seems to be similar to null in other languages:
NA... is a product of operation when you try to access something that is not there
From mpiktas' answer here.
I want to find all numbers in a string with the following code:
re:=regexp.MustCompile("[0-9]+")
fmt.Println(re.FindAllString("abc123def", 0))
I also tried adding delimiters to the regex, using a positive number as second parameter for FindAllString, using a numbers only string like "123" as first parameter...
But the output is always []
I seem to miss something about how regular expressions work in Go, but cannot wrap my head around it. Is [0-9]+ not a valid expression?
The problem is with your second integer argument. Quoting from the package doc of regex:
These routines take an extra integer argument, n; if n >= 0, the function returns at most n matches/submatches.
You pass 0 so at most 0 matches will be returned; that is: none (not really useful).
Try passing -1 to indicate you want all.
Example:
re := regexp.MustCompile("[0-9]+")
fmt.Println(re.FindAllString("abc123def987asdf", -1))
Output:
[123 987]
Try it on the Go Playground.
#icza answer is perfect for fetching positive numbers but, if you have a string which contains negative numbers also like below
"abc-123def987asdf"
and you are expecting output like below
[-123 987]
replace regex expression with below
re := regexp.MustCompile(`[-]?\d[\d,]*[\.]?[\d{2}]*`)
I want to process the input so that we know when the input contains a certain character.
For example
# Title
Member 1
Member 2
Member 3
# Title 2
Member 1
Member 2
Member 3
etc.
How can I know that anything that starts with # is a title, and the lines starting below it until the next # sign are other variables that I can process?
also if the input is
title 1 + title 2
how can we know that there is a + sign and we want to parse out title 1 and title 2 members?
How can I know that anything that starts with # is a title
If the 1st char of str (s[0]) is a hash '#':
check the substring that comes next using substr() function.
and the lines starting below it until the next # sign are other variables
Keep working with substr() until you realise that 1st char in line is a hash '#' again.
how can we know that there is a + sign
You can either iteare string or use find() function, and then split your string for further analysis. To do this, you can either use your self-made explode() function or have some fun playing with substr() function again. Is that clear enough for you?
So I have to make a flex program that matches numbers, floats, symbols, and comments.
The regular expressions are in the file.
The flex.l file http://pastebin.com/iuJ8WW6m
The weird part is the output.
Lets say I'm giving it:
0 0.0 323 323.4 1.3.4
variable another_variable
"string"
;comment
69
This is the output:
Number: -->0<--
Float: -->0.0<--
Number: -->323<--
Float: -->323.4<--
Float: -->1.3<--
Number: -->4<--
Symbol: -->variable<--
<--bol: -->another_variable
String: -->"string"<--
<--ment: -->;comment
Number: -->69<--
Why is the output at "another_variable" like this <--bol: -->another_variable ?
I know some c/c++ and for me this makes 0 sense.
Same goes for <--ment: -->;comment
Apparently it takes the 3 last character (<--) and places them on top of the first 3(Com), But why?
If i give it only
;comment
The output is "Comment: -->;comment<--", As soon as I insert a new line after it, it messes up again. I also tried the same with printf and using ECHO, but the result is the same.
Help, thanks!
I suspect that part of the the newline sequence that follows the recognized comment or symbol is being captured into yytext, and hence echoed back in your debug trace.
Try adding \r to the character class, like so:
SYMBOLS [a-zA-Z][^\,\.\"\(\) \n\t\r]*
COMMENTS ";"[^\n\r]*
In any event, you might want to pipe your debug output into a file so that you can examine it characterwise, with a tool like od.