CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
I am trying to write a program


New User

Jul 2, 2013, 4:17 AM

Post #1 of 2 (8669 views)
I am trying to write a program Can't Post

I am trying to write a program that has bool term as an input for example (1,0,1)+0,1+(1,0)
this is my bool term now I need a program that can solve this and tell me at the end if itīs false or truee. the commas are the AND operator and the + are the OR operators. I've tried so many different ways but I just couldn't write a meaningful program in perl that can do this. I hope you guys can help me.

INFO.: I had an approach with for loop and i converted the string (my boolterm is saved as a string) into an array and had a for loop with an array.. but still i have no idea how I can get this done.Note that my bool term can also contain brackets.

I am a perl beginner and hope someone could help me approach this problem.
Thank you very much.


Jul 2, 2013, 8:49 PM

Post #2 of 2 (8659 views)
Re: [GIGI] I am trying to write a program [In reply to] Can't Post

Use the substitution operator to convert to a perl expression and evaluate it with a string evaluation.

use strict; 
use warnings;
my $term = '(1,0,1)+0,1+(1,0)';
my %op = (
',' => ' and ',
'+' => ' or ',
$term =~ s/([,+])/$op{$1}/ge;
print $term, "\n", eval $term, "\n";


(1 and 0 and 1) or 0 and 1 or (1 and 0) 

A final version should check for valid expressions.
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives