
evaluating strings
Hello,
I would really appreciate any help with the problem I am currently
experiencing with the eval function.
s1='myif(plus(1,2),times(3,4))';
eval(s1);
When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
This is not acceptable since both plus and times are executed when
only one should be.
I have tried using function handles like this:
s2=@myif(@plus(1,2),@times(3,4));
eval(s2);
This works better however MATLAB executes either plus(1,2) or
times(3,4) and then myif. It executes the nested function first which
is also unacceptable.
I know this works perfectly:
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
eval(s3);
It executes myif and then either plus(1,2) or times(3,4), depending
on the myif test.
So, what I would like to know is this, is there a way to go from s1
to s3 automatically?
s1='myif(plus(1,2),times(3,4))';
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
I have tried to write a program using the brackets to indicate how
many quotation marks to wrap each function in but it is a very messy
solution. I have used strtok and strcat etc. but I have problems when
several brackets (delimiters) are touching each another.
If anyone out there can think of a solution to my problem (or the
larger eval issue) I would be extremely grateful.
Thank you so much in advance for your help.
Matt.
free_wombat@hotmail.com


0




Reply

free_wombat (13)

7/20/2006 12:40:59 AM 

See related articles to this posting
In article <ef3c24f.1@webcrossing.raydaftYaTP>,
"Matt C" <free_wombat@hotmail.com> wrote:
> Hello,
>
> I would really appreciate any help with the problem I am currently
> experiencing with the eval function.
>
> s1='myif(plus(1,2),times(3,4))';
> eval(s1);
>
> When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
> This is not acceptable since both plus and times are executed when
> only one should be.
>
> I have tried using function handles like this:
>
> s2=@myif(@plus(1,2),@times(3,4));
> eval(s2);
>
> This works better however MATLAB executes either plus(1,2) or
> times(3,4) and then myif. It executes the nested function first which
> is also unacceptable.
>
> I know this works perfectly:
>
> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
> eval(s3);
>
> It executes myif and then either plus(1,2) or times(3,4), depending
> on the myif test.
>
> So, what I would like to know is this, is there a way to go from s1
> to s3 automatically?
>
> s1='myif(plus(1,2),times(3,4))';
> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
>
> I have tried to write a program using the brackets to indicate how
> many quotation marks to wrap each function in but it is a very messy
> solution. I have used strtok and strcat etc. but I have problems when
> several brackets (delimiters) are touching each another.
>
> If anyone out there can think of a solution to my problem (or the
> larger eval issue) I would be extremely grateful.
>
> Thank you so much in advance for your help.
>
> Matt.
>
> free_wombat@hotmail.com
This is one of the most bizarre questions I have seen. Congratulations.
I have no idea what you're trying to do, but here are some observations.
1) We have no idea what the function myif is. Perhaps we don't need to
know, but knowing helps put the question into context.
2) How do you know when only one or the other argument is evaluated?
3) s3 evaluates to
>> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')'
s3 =
myif('plus('1','2')','times('3','4')')
plus('1','2') is not at all the same as plus(1,2).
4) In the s1 example you seem surprised that both arguments are
evaluated before they are passed into myif, but no other behavior is
possible. In your other examples you apparently defer execution by
passing in a string instead of a value. Why, I don't know.
5) Since I don't know what myif is let's pretend that it's
if A  B
...
end
MATLAB uses "short_circuit" operators in this case so if you just wrote
if plus(1,2)  times(3,4)
you should get the behavior you desire.

Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.


0




Reply

see9548 (1248)

7/20/2006 1:05:26 AM


Doug Schwarz wrote:
>
>
> In article <ef3c24f.1@webcrossing.raydaftYaTP>,
> "Matt C" <free_wombat@hotmail.com> wrote:
>
>> Hello,
>>
>> I would really appreciate any help with the problem I am
> currently
>> experiencing with the eval function.
>>
>> s1='myif(plus(1,2),times(3,4))';
>> eval(s1);
>>
>> When I do eval(s1) it executes plus(1,2) then times(3,4) then
> myif.
>> This is not acceptable since both plus and times are executed
> when
>> only one should be.
>>
>> I have tried using function handles like this:
>>
>> s2=@myif(@plus(1,2),@times(3,4));
>> eval(s2);
>>
>> This works better however MATLAB executes either plus(1,2) or
>> times(3,4) and then myif. It executes the nested function first
> which
>> is also unacceptable.
>>
>> I know this works perfectly:
>>
>> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
>> eval(s3);
>>
>> It executes myif and then either plus(1,2) or times(3,4),
> depending
>> on the myif test.
>>
>> So, what I would like to know is this, is there a way to go
from
> s1
>> to s3 automatically?
>>
>> s1='myif(plus(1,2),times(3,4))';
>> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
>>
>> I have tried to write a program using the brackets to indicate
> how
>> many quotation marks to wrap each function in but it is a very
> messy
>> solution. I have used strtok and strcat etc. but I have
problems
> when
>> several brackets (delimiters) are touching each another.
>>
>> If anyone out there can think of a solution to my problem (or
the
>> larger eval issue) I would be extremely grateful.
>>
>> Thank you so much in advance for your help.
>>
>> Matt.
>>
>> free_wombat@hotmail.com
>
> This is one of the most bizarre questions I have seen.
> Congratulations.
>
> I have no idea what you're trying to do, but here are some
> observations.
>
> 1) We have no idea what the function myif is. Perhaps we don't
> need to
> know, but knowing helps put the question into context.
>
> 2) How do you know when only one or the other argument is
> evaluated?
>
> 3) s3 evaluates to
>
> >> s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')'
> s3 =
> myif('plus('1','2')','times('3','4')')
>
>
> plus('1','2') is not at all the same as plus(1,2).
>
> 4) In the s1 example you seem surprised that both arguments are
> evaluated before they are passed into myif, but no other behavior
> is
> possible. In your other examples you apparently defer execution by
>
> passing in a string instead of a value. Why, I don't know.
>
> 5) Since I don't know what myif is let's pretend that it's
>
> if A  B
> ...
> end
>
> MATLAB uses "short_circuit" operators in this case so if you just
> wrote
>
> if plus(1,2)  times(3,4)
>
> you should get the behavior you desire.
>
> 
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.
>
Hello,
I would really appreciate any help with the problem I am currently
experiencing with the eval function.
s1='myif(plus(1,2),times(3,4))';
eval(s1);
When I do eval(s1) it executes plus(1,2) then times(3,4) then myif.
This is not acceptable since both plus and times are executed when
only one should be.
I have tried using function handles like this:
s2=@myif(@plus(1,2),@times(3,4));
eval(s2);
This works better however MATLAB executes either plus(1,2) or
times(3,4) and then myif. It executes the nested function first which
is also unacceptable.
I know this works perfectly:
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
eval(s3);
It executes myif and then either plus(1,2) or times(3,4), depending
on the myif test.
So, what I would like to know is this, is there a way to go from s1
to s3 automatically?
s1='myif(plus(1,2),times(3,4))';
s3='myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')';
I have tried to write a program using the brackets to indicate how
many quotation marks to wrap each function in but it is a very messy
solution. I have used strtok and strcat etc. but I have problems when
several brackets (delimiters) are touching each another.
If anyone out there can think of a solution to my problem (or the
larger eval issue) I would be extremely grateful.
Thank you so much in advance for your help.
Matt.
Thank you very much to those people who have looked at my problem and
especially to Doug Schwarz for replying.
I apologise if the way I originally presented my problem was not
clear. I guess the short version was too simplified so here�s the
long one. Although I do admit to making one error in my first post in
that s3 should have been written as
'myif(''plus(''''1'''',''''2'''')'',''times(''''3'''', ''''4'''')'')'
rather than
'myif(''plus(''1'',''2'')'',''times(''3'',''4'')'')'
as Doug realised.
This question has to do with the MATLAB genetic programming toolbox
written by Sara Silva which is available open source from <http://gplab.sourceforge.net/index.html>.
I believe it currently has one major unresolved problem and it
involves the eval function.
The GP program generates a population of random programs
(�individuals�) to solve problems. These generated programs are
represented as �trees� which are stored in nested structs. The
algorithm needs to evaluate each program to determine which ones work
the best.
Currently, programs are evaluated by first converting the tree struct
into a single layered string. In the artificial ant problem for
example, a program might look like this
string = 'antif(antif(antmove,antright),antleft)'
GPLAB then simply does eval(string) to evaluate the programs.
However, this does not work correctly (see my example programs at the
end).
About a year ago some French researchers using GPLAB wrote a program
to convert the tree structure into the strings with levels which
GPLAB could then evaluate correctly like this:
string = ' antif(''antif(''''antmove'''', ''''antright'''') '',
''antleft'') '
Unfortunately the toolbox is very large and making this change
affects many other programs.
It would be much simpler to have a new program that just converts
this
string = 'antif(antif(antmove,antright),antleft)'
to this
string = ' antif(''antif(''''antmove'''', ''''antright'''') '',
''antleft'') '
whenever an individual needs to be evaluated.
So there you have it. If anyone can think of an easy way to do this,
that would be great.
If you want to see how the eval function (mis)behaves then try out
each case of the following evalif program.
Thanks.
%evalif
clear all
clc
global x switchvar
x=0;
fprintf('\neval if')
fprintf('\n\n')
switchvar='1';
switch switchvar
case '1' % does NOT evaluate correctly
iffunction(acttrue,actfalse);
case '2' % does NOT evaluate correctly
str='iffunction(acttrue,actfalse)';
ans=eval(str);
case '3' % does evaluate correctly
iffunction(@acttrue,@actfalse);
case '4' % but this does NOT evaluate correctly
iffunction(iffunction(@acttrue,@actfalse),@actfalse);
case '5' % does evaluate correctly
str='iffunction(''acttrue'',''actfalse'')';
ans=eval(str);
end
fprintf('\n\n')
fprintf('\nfinal result: x = %d',x)
fprintf('\ncorrect result: x = 1\n')
function x=iffunction(a,b)
global x switchvar
fprintf('\nif function\n')
switch switchvar
case '1' % if passing one string
if 1
a;
else
b;
end
case '2' % if calling functions directly
if 1
a;
else
b;
end
case '3' % if passing function handles
if 1
feval(a);
else
feval(b);
end
case '4' % if passing function handles
if 1
feval(a);
else
feval(b);
end
case '5' % if passing multiple strings
if 1
ans=eval(a);
else
ans=eval(b);
end
end
function x=action1
global x
x=x+1;
fprintf('\nact true: x = %d\n',x)
function x=action2
global x
x=x+2;
fprintf('\nact false: x = %d\n',x)


0




Reply

free_wombat (13)

7/20/2006 3:09:50 AM



2 Replies
90 Views
Similar Articles
[PageSpeed]
40

Similar Artilces:
paste a string as a string without evaluating contentsI am pasting data from Excel. In a long list of numbers/letters, I have the string: 1E4. I paste this into a cell array using the variable editor and "paste excel data", and what I get is 10000 because this got interpreted as scientific notation. Caused major headache and lost a lot of time. Is there a way to ensure that things get pasted as strings? I tried predefining the array as a cell (b=cell(11000,1)) but that did not prevent the problem.
Is there a way to ensure that something is pasted as a string?
Thanks!
... How to evaluate this string?Hi all,
I have a string like
$string='My marks are ( (10+20)*2) '
i should be able to convert this string to
$string='My marks are 60 '
the arithemaic expression may vary to any complexity. it may have
logical expressions like '&&' or '' etc.
Can anyone help me out?
Thank you
Kaushik
kaushik wrote:
> Hi all,
> I have a string like
> $string='My marks are ( (10+20)*2) '
> i should be able to convert this string to
> $string='My marks are 60 '
>
> the arithemaic expression may vary to any complexity. it may ha... string evaluationAssume I have following arrays with sizes
var1, var2, var3 ........ = 300 X 1
I wanted to copy a portion of these variables into another variables say
for i = 10 : 20
strcat('var',num2str(i)) = strcat('var',num2str(i9))(10:200);
end
What I actually wanted to do is
var10 = var1(10:200);
var11 = var2(10:200);
........
Is there a better way to do this in a loop, I could assign the variables individually but wanted to do in a loop if the variables are increasing.
Thanks
Did you create all of these variables in the first place? If so, that was you... Evaluating Strings...In php, I have an array called "q". The elements are:
$q[0] = 2
$q[1] = 4
$q[2] = 5
$q[3] = 3
I'd like to be able to loop through this array via a While loop, and
echo the contents of these array elements back to the screen.
When I try something like:
for($i = 0; $i < 4; $i++) {
echo $q[i];
echo eval("$q[i]");
echo eval("\$q[" . i . "]");
}
I get the "String" instead of the "Contents". For example, the output
is $q[0] instead of 2, $q[1] instead of 4, etc.
How can I fix this? Thanks.
number1.email@gmail.com ... a String which evaluates to itself.Can you imagine any String that satisfies the
following formula?
aString = (Compiler evaluate: aString)
It means aString evaluates to itself.
'0'? No. It evaluates to an Integer.
''? No. It evaluates to nil.
''''''(2 letter string)? No. It evaluates to
an empty String, outmost quotes are eliminated.
Here is my example code. It produces a String
exactly equal to its code text when evaluated.
================ cut here ================
 stringBank stringBankStream quoteChar quoteSubstitute stringDelimiter
preQuoteString quotedStri... Reevaluating a string?I'm writing a program to send data over the serial port. I'm using
pyserial, and I'm on WindowsXP. When I use literals I can get the data
accross how I want it for example:
1 2 3 4 5 6
serialport.write('!SC'+'\x01'+'\x05'+'\xFA'+'\x00'+'\r')
1=Get devices attention
2=Select channel on device
3=Rate for movement
4=Low byte of 16 bits
5=High bytes of 16 bits
6=Carriage return signaling command is over
This command works as desired. Sends the first 3 ASCII characters, then
some numbers in hex ... evaluating a object string var x = eval("{ 'flag' : 1 }");
alert(x);
alert(x.flag);
why doesn't the above work?
On Mar 6, 12:17=A0pm, jman wrote:
> =A0 =A0 =A0 =A0 =A0 =A0 var x =3D eval("{ 'flag' : 1 }");
> =A0 =A0 =A0 =A0 =A0 =A0 alert(x);
> =A0 =A0 =A0 =A0 =A0 =A0 alert(x.flag);
>
> why doesn't the above work?
Because the  eval  method treats its string input as a javascript
program and a javascript program commencing with an opening brace is a
program that starts with a block statement. That makes the content of
the block statement a sy... Evaluate string in LispHello,
is it possible to evaluate a string in LISP?
For example, there is a string "(pprint \"Hello World\")". Is it
possible to evaluate this string?
Regards
Michael
Michael H�nisch <Michael.Hoenisch@ascad.de> writes:
> is it possible to evaluate a string in LISP?
>
> For example, there is a string "(pprint \"Hello World\")". Is it
> possible to evaluate this string?
>
Combine eval with readfromstring

Howard Ding
<hading@hading.dnsalias.com>
On Wed, 15 Sep 2004 14:44:48 +0200, Michael H�nisch <Michael.Hoeni... String Evaluation #2
I need to evaluate a string expression which is made of some regular matlab functions and some created functions that don't take any arguments. For example:
String = 'sin(plus( CreatedFunction1 , CreatedFunction2 ) )';
For sure I can get the job done by using the comand 'eval', but the thing is that it gets too slow (since I have thousands of strings to evaluate at a time). I also tried the function 'str2func' to generate a function handle to be evaluated by 'feval' but 'str2func' only seems to work with existing functions.
The fastest way until ... delaying string evaluationHi,
I would like to partially expand a string and then further expand it
later on when more information is available. I am currently using
string interpolation with a reeval method suggested by Bill Kelly.
class String
def reeval(b = TOPLEVEL_BINDING)
eval("<<END_REEVAL\n" + self + "\nEND_REEVAL\n", b).chomp!
end
end
This lets me do stuff like this:
expanded = "one"
delayed = "two"
s = "#{expanded} \#{delayed}" # => "one #{delayed}"
s = s.reeval(binding) # => "one two"
As a more... evaluate string of variablesHi
I need to evaluate a string of vectors with for example str2num. so,
what I have is a vector, x = [3 7 11 ]', and a string s1 = 'ones(size
(x)) x x.^2 x.^3 x.^4 x.^5' , now I want to do the polynomial
calculation in the string. str2num(s1) obviously doesnt work. I should
also say that I am actually using comsol script, but it seems like
str2num and eval is used the same way as in matlab.
Thankful for any help.
... numerical evaluation of a stringHi,
is there a way to evaluate in PHP a string like "2+3*5" ? I'm
expecting 17 as a result. A way to achieve that would be to invoke the
bc calculator but I was wondering if you can do such a thing directly
in PHP?
Cheers,
Patrick
..oO(geantbrun)
>is there a way to evaluate in PHP a string like "2+3*5" ? I'm
>expecting 17 as a result. A way to achieve that would be to invoke the
>bc calculator but I was wondering if you can do such a thing directly
>in PHP?
eval()
Micha
... Evaluating String Parameters
I am having trouble understanding how Parameters are evaluated
(especially String Parameters). For many of my fixed point models, I use
a Precision parameter to define the precision of an operation or port.
Currently, Precision is specified as a String to support several
different precision formats.
In many cases, I specify the precision directly with a valid Precision
string (for example "(1.8)"). In other cases, I would like to specify
the precision with a parameter defined at a higher level in the
hierarchy (i.e. as described in Section 2.5.1 Parameters in Hiera... Evaluating string as varIs there a way to do the following
declare @var1 varchar
select @var1 = "@var2 = 5"
declare @var2 varchar
select @var2 = 5
set rowcount 5
select * from the..table where @var1
so that rows are actually returned.
In a nut shell we have a table that has parts of a where clause.
vars are being passed in, and we want to append the selected clause to
the statement.
in the above example in ecmascript I could just do eval(@var1) in the
select and wit would return rows...is there a sybase equivilent???
Do not write code like this. You are supposed to know what you a... Evaluate a simple string?Trying to figure out how to evaluate a simple string.
(setq x "princ 3")
(eval x)
=> "princ 3"
Hpw do I evaluate x to get:
=> 3
Bytheway I tried:
(eval `,x)
=> "princ 3"
Thanks for any help!
____
Asif
On 20121009, Asif <asif.haswarey@gmail.com> wrote:
> Trying to figure out how to evaluate a simple string.
>
> (setq x "princ 3")
> (eval x)
>=> "princ 3"
>
> Hpw do I evaluate x to get:
>=> 3
With difficulty, since "princ 3" is not a Lisp expression.
You m... Evaluating a string as C++ CodeHello, Usenet.
I come from a background of mostly Perl and Ruby, so C++ is the first
truly low level programming language that I know. However, I have
something I'd like to ask the community about. I'm rewriting a BC
esque terminal calculator I did in Perl. (You can find it on
SourceForge as PerlCalc, I believe.), and I've encountered a problem
that my manual book doesn't cover. In Perl, you can evaluate a string
as code like this:
#Getting the string
my $string = <STDIN>;
chomp( $string );
# Evaluate it and print it
print(eval($string));
How, pray tell, might I do tha... Python selfevaluating stringsHi all,
An earlier post today got me thinking about "quines" (programs that
output themselves) in Python. I tried to find some on the web but
didn't find many ([1]). In particular I didn't find any that
corresponded to my instinctive (LISPinduced, probably) criterion:
def self_evaluating(s):
"Return True if string s evaluates to itself"
return s == eval(s)
Here is the result of my efforts so far:
1. Starting from the classic idea (lambda x:x%x)('lambda x:x%x') I got
the following
v=(lambda x:x%('"''""'+x+'&... Evaluate string expression in if statement?Is there anyway to literally evaluate the contents of a string in an if
statement?
For example:
int i = 0;
char * str = "i == 0";
if(str) /* I know this doesn't do what I want */
{
...
}
would be the same as:
int i = 0;
if(i == 0)
{
...
}
The general application is that I'm in interested in reading if
conditionals from a file, not only the conditional variables but the
conditional operators as well (the whole phrase).
Any ideas appreciated.
Thanks.
No Such Luck said:
> Is there anyway to literally evaluate the contents of a string in an if
> statement?... Evaluate a string as a variable nameI have a structure that has multiple name/value pairs, and I want to
extract the value that corresponds with the name. However, I am only
passing in a string of the name, not the structure.name, so I need a
way to combine the two such that I am evaluating the string. It might
be easier to explain with an example:
***
;create a simple structure
struct={name1:9, $
name2:5, $
name3:1}
;variable that matches the part of the structure I want
x='name2'
;create a string that if it were a variable, it would be part of the
structure, 'struct.name2&... self evaluation of a json stringi've a problem with the following statement:
var eg=eval('('+strjson+')');
making debug with firebug, a firefox extension i got
>>> typeof(strjson)
"string"
>>> inspect(strjson)
{"data":"01\/05\/2006","tipo_selezionato":"premiazione",
"lista_orari":[{"id":"1","ora_inizio":"10:30","ora_fine":"14:30"}],
"1":{"dettaglio_evento":{"titolo":"premio birra","descrizione":"chi beve
birra cam... using textscan to evaluate strings?hello all, I'm having a bit of trouble figureing out how textscan
works.. I thought someone here could help me.
I have a text file, called test.txt with 7 header lines, and then
several lines such as:
temp = 45
cond = 48
depth = 51
etc, So I would like to load this file, without the textlines and then
evaluate those strings, so that the constants are in my workspace. so
far I am doing this:
fid = fopen('test.txt','r');
InputText=textscan(fid,'%s','delimiter','\n','headerLines',7);
this gives me a cell of what I want to evaluate, but Im n... comparing/evaluating string inputhey there,
basically i'm running a simple psychological experiment on matlab as part of some coursework and i'm brand new to the program.
the short and sweet of it is that I want to compare the input of participants (it will be a string  a word which is the pair of a word they have memorised o response 'NO' if the word is not one they memorised) and then I want to compare this to the word they should have said, or the 'NO' response they should have said. It will be across 10 trials and I need to be able to record whether responses were correct or not, in the form 1 ... evaluate erf in a symbolic stringDoes anyone know how to pick out the erf( ) from a symbolic string,
evaluate it (should be straightforward since no dependent variables are
found in the integrand), and put it back into symbolic form?
Thanks if anyone has some experience with this.
...



