Accessing or creating nested JavaScript objects with string key without eval

You could split the path and reduce the path by walking the given object. If no Object exist, create a new property with the name, or an array. Later assign the value. function setValue(object, path, value) { var way = path.replace(/\[/g, ‘.’).replace(/\]/g, ”).split(‘.’), last = way.pop(); way.reduce(function (o, k, i, kk) { return o[k] = … Read more

Process mathematical equations in php

My standard answer to this question whenever it crops up: Don’t use eval (especially as you’re stating that this is user input) or reinvent the wheel by writing your own formula parser. Take a look at the evalMath class on PHPClasses. It should do everything that you’ve listed here. EDIT re: Unfortunately evalMath does not … Read more

Is there C/C++ equivalent of eval(“function(arg1, arg2)”)?

C++ doesn’t have reflection so you must hack it, i. e.: #include <iostream> #include <map> #include <string> #include <functional> void foo() { std::cout << “foo()”; } void boo() { std::cout << “boo()”; } void too() { std::cout << “too()”; } void goo() { std::cout << “goo()”; } int main() { std::map<std::string, std::function<void()>> functions; functions[“foo”] = … Read more

Assign to a bash array variable indirectly, by dynamically constructed variable name

arr$varEnvCol[$index]=”$(…)” doesn’t work the way you expect it to – you cannot assign to shell variables indirectly – via an expression that expands to the variable name – this way. Your attempted workaround with eval is also flawed – see below. tl;dr If you use bash 4.3 or above: declare -n targetArray=”arr$varEnvCol” targetArray[index]=$(echo $line | … Read more

How do I use variables to set properties in VBA (Excel)

Solution 1. Use CallByName. Option Explicit Private Type Callable o As Object p As String End Type Public Sub SetProperty(ByVal path As String, ByVal Value As Variant, Optional ByVal RootObject As Object = Nothing) With GetObjectFromPath(RootObject, path) If IsObject(Value) Then CallByName .o, .p, VbSet, Value Else CallByName .o, .p, VbLet, Value End If End With … Read more

Haskell: how to evaluate a String like “1+2”

Your question leaves a lot of room for interpretation. I’m taking a guess you aren’t accustom to building a whole pipeline of lexing, parsing, maybe type checking, and evaluating. The long answer would involve you defining what language you wish to evaluate (Just integers with ‘+’, perhaps all rationals with ‘+’, ‘-‘ ‘*’, “https://stackoverflow.com/”, or … Read more

How to run a string as a command in VBA

You may be tempted by adding your own string “Executer”: Sub StringExecute(s As String) Dim vbComp As Object Set vbComp = ThisWorkbook.VBProject.VBComponents.Add(1) vbComp.CodeModule.AddFromString “Sub foo()” & vbCrLf & s & vbCrLf & “End Sub” Application.Run vbComp.name & “.foo” ThisWorkbook.VBProject.VBComponents.Remove vbComp End Sub Sub Testing() StringExecute “MsgBox” & “””” & “Job Done!” & “””” End Sub

tech