FAQ Database Discussion Community


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))...

Dynamically Generating Code

ruby,dynamic,methods,eval
I have a whole bunch of similarly-structured methods, each of which look something like this: def my_method_1 if params[:user_id] #code that stays the same across my_method_1, 2, 3, 4, etc. #code that varies across my_method_1, 2, 3, 4, etc. elsif params[:tag_id] #code that stays the same across my_method_1, 2, 3,...

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); } }...

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....

JS - Call function by name (in object notation)

javascript,window,eval,object-notation
I want to clean up some old code and optimize it, which often uses the same code. (with only different names of functions to call) I make a easier example and no, I don't write on a game. But this example looks more comprehensible to explaination of my issue. character.sleep(1);...

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.

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...

Is there a better alternative to eval() for my function checking algorithm

javascript,jquery,eval,jquery-mask
I have the following code below designed to check that a function exists at anytime. This code does not have any errors currently. I'm using this because my javascript files load in arbitrary orders and is necessary for my project its done that way. Since my javascript files are separated...

using Eval of ASP Listview as an inline CSS value

asp.net,listview,warnings,eval
my code is : <asp:ListView ID="lst1" runat="server"> <ItemTemplate> <div style="width:<%#Eval("Rating")%>%"> </div> </ItemTemplate> </asp:ListView> It works great, but in VisualStudio editor it's considered as invalid. Is there any way to make it valid? I know that I can create the whole ' style="width:xxx%" ' in the Eval but I'm looking for...

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...

Using Javascript's eval() on HTML Text

javascript,html,eval
I am toying around with some basic constructs in HTML and Javascript (i.e., I am a HTML/Javascript n00b), and there is a feature I simply cannot seem to implement properly. In a nutshell, I'd like to grab text from the body of an HTML page and evaluate it using Javascript...

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...

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...

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...

How can I break out segments of a 'PHP' file into raw PHP, -and- possible raw HTML -in order-

php,html,regex,eval,output-buffering
So I've got a concept of how to do this - but actually implementing me is a bit of a stumper for myself; mostly due to my lack of regex experience - but let's get into it. I'd like to 'parse' through a 'php' file that could contain something like...

How to execute JavaScript function in an Ajax() Response

javascript,jquery,ajax,function,eval
I have a home page that load the ajax() response and post it to UL as LI content, with the same page I have script function that can select the LI content with trigger for another function, but isn’t working. PAGE 1 HTML <ul id="feature-deals" class="list-products allShopping-deals"> </ul> SCRIPT $(document).ready(function(){...

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...

Javascript Eval overwriting variable

javascript,eval
Don't understand why the code below is overwriting my var arr. Any help appreciated. var arr = [1,2,3] var string = "function swap(arr) { var newarr = arr; var temp = arr[0]; newarr[0] = arr[arr.length-1]; newarr[arr.length-1] = temp; return newarr }" var test = eval("[" + string + "]")[0]; test(arr);...

Using eval() to check if global variable isset

php,eval,constants
I am trying to check defined variables based on passing a single portion of the variable. (The rest of the variable is static and all other portions of it are the same), so I made a test to find out if this is possible. It does not work, but perhaps...

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...

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...

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]...

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...

How to evalulate xml literal using scala.tools.reflect.ToolBox in runtime?

xml,scala,reflection,eval
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25). Type in expressions to have them evaluated. Type :help for more information. scala> import scala.reflect.runtime.currentMirror import scala.reflect.runtime.currentMirror scala> import scala.tools.reflect.ToolBox import scala.tools.reflect.ToolBox scala> def eval(code: String) = { | val toolbox = currentMirror.mkToolBox() | toolbox.eval(toolbox.parse(code)) | } eval:...

How to control
  • list item visibility using Eval [closed]
  • asp.net,eval
    In an unordered list the visibility of individual list items can be set thus: <li runat="server" visible=false>Apples</li> or: <li runat="server" visible="false">Bananas</li> But why can't this be done using the 'Eval'method? None of these work: <li runat="server" visible='<%# Eval("Show")%>'>Carrots</li> --where Show is a bit that has values of 0 or 1....

    How to call code behind function from label.text in asp.net

    c#,asp.net,eval,code-behind
    I am trying to call a function defined in code behind from Label.Text but it's not working. Here is the code... code in .aspx file <asp:Label runat="server" Text='<%# GetPagingCaptionString() %>' ID="pagenumberLabel"></asp:Label> code block from code behind public string GetPagingCaptionString() { int currentPageNumber = Convert.ToInt32(txtHidden.Value); int searchOrderIndex; if (int.TryParse(Convert.ToString(Session["searchOrderIndex"]), out searchOrderIndex))...

    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...

    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...

    combine multiple expressions in R into one big expression

    r,expression,eval
    I am new to R expression handling. I am stuck with below problem. Any input is appreciated. I am trying to generate two individual equations and combine them into one expression and pass it to an algorithm to find optimal value. OLD_PRICE ELAST Units 1 59.98 1.3 151 2 59.98...

    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...

    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...

    Can an inserted