FAQ Database Discussion Community


Format both an expression and its result without eval

common-lisp,eval
I am trying to format an arbitrary expression, say (+ 2 3), and at the same time, its result, 5. I have the following: (defun expr-and-result (expr) (format t "~a returns ~a~%" expr (eval expr))) CL-USER> (expr-and-result '(+ 2 3)) (+ 2 3) returns 5 Though it's a simple matter...

How to use this javascript code with eval or in a iim file?

javascript,eval,imacros
I want to use this code with eval or in a iim file without using js file.I tried for using it into eval but didn't work.Or is there any other way that can allow this code into iim file directly? please help me! do { do { iimPlay("CODE:ondialog pos=1 button=ok...

appending multiple similar functions to protoype

javascript,node.js,eval
I want to avoid redundant code for one of my projects, which is a simple logger. This logger needs functions like *.alert, *.notice, etc. and I also decided to assign aliases to them (.alert = .al, etc.). This leads to multiple similar functions, that only differ by one number, and...

is there a way to avoid eval() to accomplish dynamic dispatch of a function specified in a list in python

python,eval
I have a list as defined here: header_fields = [ ['delim',0,4,'strip()'], ['qual',4,5,None], ['NLS_CODE',6,25,'strip()'], ['source_system_id',26,45,'strip()'], ['extract_name',46,65,'strip()'], ['extract_serial_number',66,85,'strip()'], ['file_counter',86,88,"lstrip('0')"], ['total_file_count',89,91,'strip()'], ['extract_run_date_time',92,126,'strip()'], ['from_date',127,161,'strip()'], ['thru_date',162,196,'strip()'], ['number_of_rows',197,216,'strip()'], ['data_recons1',217,246,'strip()'],...

How could I change this Maths quiz in order to not use eval to find the answer in Python?

python,eval
I heard eval was very bad practice in Python because of security issues. So I was wondering if there is a way I could not use eval in this program. for _ in range(10): n1 = random.randint(1, 10) n2 = random.randint(1, 10) operator = random.choice("+-*") question = (n1,operator,n2) questionNo +=1...

How to avoid using JavaScript eval() in user-defined function

javascript,json,eval
I'm trying to make a generic table-top RPG helper web app. It has to be generic because Wizards of the Coast is very protective of their copyrights. So, in order to avoid cease-and-desists, the system has to be capable of loading arbitrary rules. That said, my buddies and I (along...

converting string to tuple in python

python,string,tuples,eval
I have a string returnd from a software like "('mono')" from that I needed to convert string to tuple . that I was thinking using ast.literal_eval("('mono')") but it is saying malformed string....

in R, what is the difference between get(“abc”) and eval(parse(text=“abc”))

r,parsing,eval
I'd like to know the difference(s) between those two operations; in this example, they seem to work the same, but there must be situations where one will work and the other not. I just can't figure out when this would be the case. > a <- 298 > eval(parse(text =...

Save a dictionary key as a variable

python,dictionary,multidimensional-array,key,eval
I'm working on a small framework and I've found a place where it would be beneficial to save a dictionary key as variable. The problem I have is that the dictionary may have any number of layers, so it's not just a case of storing the final key. For example...

R convert character to dataframe column name and then replace values in a column

r,eval,assign
I was able to get content of the column pointed by character variable b. But i want to make column emp 0. How could i do that? emp=c(1,2,30) abc=data.frame(emp) b="emp" #below line gives content of column emp eval(parse(text=paste("abc$", b, sep = ""))) #how can i replace each value in column...

What's wrong with `var myEval = eval; myEval(“2+2”)`

javascript,eval
As per MDN-EVAL You cannot indirectly use the eval function by invoking it via a name other than eval(); if you do, a runtime error might occur. For example, you should not use the following code: var x = 2; //1 var y = 4; //2 var myEval = eval;...

Evaluate expression in R data.table

r,eval,data.table
I have the following data.table: > dt = data.table(expr = c("a + b", "a - b", "a * b", "a / b"), a = c(1,2,3,4), b = c(5,6,7,8)) > dt expr a b 1: a + b 1 5 2: a - b 2 6 3: a * b 3...

How to concatenate period in eval ASP.net

html,asp.net,eval
I have a ASP.net Label whose Text is defined as follows Text='<%# Eval("Price", "{0:&pound;0.00 ex. VAT}") %>' This displays the label as follows ignoring the 'period' between ex and VAT. How do i handle this? £99.99 ex VAT...

Dynamically created layer names, problems with eval()

eval,layer,openlayers-3
I want to create some layers with names taken from database. "Trees" in database becomes a "Layer_Trees" openlayers layer. I tried many things with eval function, yet with no success. Seems like its completely uncapable of defining new variables. function addLayer_ImageWMS(SourceName,SourceLayerName) { LayerName="Layer_" + SourceLayerName; eval(LayerName) = new ol.layer.Image({ title:...

undefined local variable or method after eval when not declared before

ruby,eval
In Ruby 2.1.2 :068 > a=1 => 1 2.1.2 :069 > eval("a=4") => 4 2.1.2 :070 > a => 4 2.1.2 :071 > eval("b=4") => 4 2.1.2 :072 > b NameError: undefined local variable or method `b' for main:Object So,the question is why the variable 'b' will be 'undefined local...

Why can you use eval() with the function repr(y) inside and not str(y)?

python,string,eval,repr
Here is some code that I've found and would like for someone to explain. If I assign a string to the variable y, evaluate it with the eval function and assign the content to the variable y2, the interpreter returns true. >>> y = 'a string' >>> y2 = eval(repr(y))...

Perl sigdie handler and eval

perl,eval,log4perl
I am overriding my SIG die handler as below inside my Logger module. # Catch die messages and log them with logdie $SIG{__DIE__} = \&logdie; Now below program run as expected and post processing will be called. use strict; use warnings; use File::Path; # use MyLogger; my $dir="/random"; eval {...

Php: json to constants

php,eval,constants
I have a Json that contains a few contants I'd like to auto-define. It works fine with simple "key": "val" statements, but I can't make it work for eval staments: $cat test.php <?php function objDefinition($obj) { foreach ($obj as $key => $val) { $val = $val; \define($key, $val); } }...

How to De-Stringify Stringified JavaScript?

javascript,parsing,eval,stringify
How to 'de-stringify' JavaScript code that has been stringified? JSON.parse does not seem to work. We want to store JavaScript code in the database and then retrieve it and then eval it. Here's the code in the database: //# sourceURL=journal.js function onBlur(e) { var drAmount = script.getGridRowFieldValue('debitamount', e.rowuid) var crAmount...

Calculate Dice Roll from Text Field

java,random,eval,jtextfield,dice
QUESTION: How can I read the string "d6+2-d4" so that each d# will randomly generate a number within the parameter of the dice roll? CLARIFIER: I want to read a string and have it so when a d# appears, it will randomly generate a number such as to simulate a...

Evaluating text in data table

r,eval,data.table
I'm trying to aggregate a lot of variables in a very large data table by a single variable. The issue I'm having is with eval(parse(text=...)) which I believe is because I'm evaluating several expressions rather than just one. Here is a quick example: library(data.table) data(mtcars) mtcars<-as.data.table(mtcars) mtcars[,j=list( eval(parse(text='mean_mpg=mean(mpg), sum_mpg=sum(mpg)')) ),by=gear]...

Python parse mathematical text expression

python,regex,math,eval
I was wondering if anyone knew of a good python library for evaluation text-based mathematical expressions. So for example, >>> evaluate("Three plus nine") 12 >>> evaluate("Eight + two") 10 I've seen similar examples that people have done for numeric values and operators in a string. One method used eval to...

Alternative for eval() function for this codeblock

javascript,jquery,html,html5,eval
I am working on a HTML code editor like JSBin. I am using eval() to evaluate the JavaScript in a JS textbox of the editor. However, I came to know that I can't use it online due to security issues. Please help me in finding the alternative. Here's my code....

Use of eval when building a dict in a for loop. Name is not defined

python,dictionary,eval
Mostly curious why this syntax isn't behaving in the way I would expect it to. Inside of a class's init which accepts multiple parameters (id, departmentId, active), I have the following variable: attributes = [ {'id': 5}, {'departmentId': 10}, {'active': True} ] Essentially, I'm trying to build a dictionary where...

Using eval to print an array value

php,arrays,eval
$cars = array ( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) ); $key = '[1][0]'; $str = '$cars'.$key; echo $str."\n"; eval($str); Output: $cars[1][0] PHP Parse error: syntax error, unexpected end of file in ... : eval()'d code on line 1 I'm expecting it to print the value, i.e BMW...

Eval Inside Of Funtion Results In Error, While Outside It Does Not

python,eval
I wrote a small python script that takes in descriptions of simple impedance networks and calculates the effective impedance. The code is pastebined here. The code, as is, runs correctly, but if you sub in the commented out lines that are below them, which simply moves the eval to the...

PHP eval not executing properly

php,eval,heredoc
Could someone point out what I'm mistaking here? :) <?php $q = $_GET[q]; $acuman = <<<PARSE input: (contains: "hello"){ output: "hello"; } PARSE; $acuman = str_replace("input: (contains: ", 'if(strpos(', $acuman); $acuman = str_replace("){", ', $q) !== false) {', $acuman); $acuman = str_replace("output: ", '$output = ', $acuman); eval($acuman); ?> I'm...

How to convert a statement in a string into a format that can be evaluated (Ruby)

ruby,string,eval
I am writing a quiz program where I need to store boolean statements as strings in an array and output them to the terminal as part of a question. I then want to evaluate the contents of these strings and return the value so that I can test whether I...

Foreach loop unable to find object

r,foreach,parallel-processing,eval
I am trying to use foreach with the parallel backend to speed up computation (of cross validation of an {AUCRF} random forest for feature selection, if this does matter). In the process of doing so i need to get a subset of a vector. The name of the vector can...

Remove quotation marks from an object

ruby,string,eval
I want to call User.first, but I get it like "User.first". How can I strip the quotation marks so I can call User? Using a regex like this: gsub!(/\A"|"\Z/, "") returns nil instead of the expression.

Is this eval() in python safe?

python,eval,code-injection
consider this code: from re import sub input = request.POST['input'] sub_pattern = {'1':"sub('pattern1','txt1',input)", '2':"sub('pattern2','txt2',input)", } eval(sub_pattern['1']) is there code-injection in this code? can you provide POC on this code injection?...

Can an inserted