Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl calculator script 1

Status
Not open for further replies.

xmassey

Programmer
Apr 9, 2007
62
GB
I have a reasonably simple problem I need to solve.

I have built a calculator which basically stores each function to a file. For example when I press the button 1 it adds "1" to the file, then i press + so it adds "+" to the file (therefore the file currently has "1+" stored in it.

The idea is to then open that file under a variable i.e. @calculate and work out the sum. So imagine I had stored in the file 1+1. I would like to do the following:

$finalSUM = @calculate;

I assumed that I would get this result

$finalSUM = 1+1;

therefore $finalSUM = 2

However I am getting the result of 1 for every sum completed. I assume this is because of perl being unable to to use @calculate (1+1) and calculate it in this way... Please help

Here is part of my code...



#! /usr/bin/perl
use strict;
use CGI ':standard';

my ($calculate);

open (LOG, "</../../../../../../../../../calculations.txt") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
my @calculations = <LOG>;
close (LOG) || Error ('close', 'file');

$calculate = (@calculations);

open (LOG, ">/../../../../../../../../../calculations.txt") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
print LOG "$calculate";
close (LOG);

#####################
## Print statement ##
#####################
print "Content-type: text/html\n\n";

print <<"HTML code";
<HTML><HEAD>
<TITLE>HTML Editor/Viewer</TITLE>
<meta http-equiv="Refresh" content="1; url=http://www.ChrisMassey.co.uk/Perl/Scripts/Calculator/Calculations.txt">
</HEAD><BODY>

<p><font size="2" face="Arial"><a href="calculations.txt">Complete</a></font></p>

</BODY></HTML>

HTML code

print "";

###########
## Error ##
###########
sub Error {
print "Content-type: text/html\n\n";
print "<font face=arial size=2>The server can't $_[0] the $_[1]: $! \n</font>";
exit;
}


Imagine stored in the file calculations.txt is 1+1+1

The result i get is "1" when it should be "3
 
I "think" you need to eval that array to have it actually calculate.

so
$calculate = eval @calculate;

Other criticism

I wouldn't do your paths like this
"</../../../../../../../../../calculations.txt"

I would just put the full path into the script.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Thank you very much for responding. I will see if the eval function works.

The reason I put my path as "</../../../../../../../../../calculations.txt" is so that I hide my path from forum users. I do actually put the full path, however I changed it all to ".." before posting the message.
 
The eval function unfortunately didn't work. Any other suggestions?
 
Weird.. it works on variables :)

@test = ("1+1");
$cal = eval $test[0];
print "$cal\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
eval works on scalars. If you pass it an array, all it will do is return the number of elements of the array.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@a[/blue] = [red]([/red]
	[red]q{[/red][purple]print "foo"; "foo"[/purple][red]}[/red],
	[red]q{[/red][purple]print "bar"; "bar"[/purple][red]}[/red],
	[red]q{[/red][purple]print "baz"; "baz"[/purple][red]}[/red],
[red])[/red][red];[/red]


[black][b]my[/b][/black] [blue]@r[/blue] = [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [blue]@a[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple], [/purple][red]'[/red], [blue]@r[/blue][red];[/red]

The above prints "3";

- Miller
 
scalars ... variables... you guys knew what i meant right [2thumbsup]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
is this school/course work xmassey?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
No its not coursework, i'm just trying to teach myself perl and the best way I can think of is to use a book and try and create a number of scripts. So i'm learning simple operations and file opening at the moment, and combining them by making a calculator
 
If the eval function only works with scalers then surely its a pointless function because I can evaluate (use and calculate) scalers without using the eval function

i.e.

my ($sumA, $sumB, $total);
$sumA = 3;
$sumB = 4;
$total = $sumA + $sumB;

And the result is "7
 
Your kind of comparing apples to oranges

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
The word scalar is only a language variable semantic in this case. It did not imply just simple scalars, but expressions as strings as well.

For your calculator program, it appears that you are saving each button press as an element of an array. For example:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@array[/blue] = [red]([/red][red]'[/red][purple]7[/purple][red]'[/red], [red]'[/red][purple]+[/purple][red]'[/red], [red]'[/red][purple]13[/purple][red]'[/red], [red]'[/red][purple]-[/purple][red]'[/red], [red]'[/red][purple]4[/purple][red]'[/red][red])[/red][red];[/red]

Now evaling that directly would simply produce the result 5, which is useless. However, just use join to create a string and then eval to get a meaningful result:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$result[/blue] = [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [url=http://perldoc.perl.org/functions/join.html][black][b]join[/b][/black][/url] [red]'[/red][purple][/purple][red]'[/red], [blue]@array[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [blue]$@[/blue] [olive][b]if[/b][/olive] [blue]$@[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Result is '[blue]$result[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

Good luck with your tinkering.

- Miller
 
arrays can also be evaluated as strings by double-quoting them:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@array[/blue] = [red]([/red][red]'[/red][purple]7[/purple][red]'[/red], [red]'[/red][purple]+[/purple][red]'[/red], [red]'[/red][purple]13[/purple][red]'[/red], [red]'[/red][purple]-[/purple][red]'[/red], [red]'[/red][purple]4[/purple][red]'[/red][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$result[/blue] = [url=http://perldoc.perl.org/functions/eval.html][black][b]eval[/b][/black][/url] [red]"[/red][purple][blue]@array[/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/warn.html][black][b]warn[/b][/black][/url] [blue]$@[/blue] [olive][b]if[/b][/olive] [blue]$@[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Result is '[blue]$result[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

output:

Result is '16'

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
If the eval function only works with scalers then surely its a pointless function because I can evaluate (use and calculate) scalers without using the eval function

You are correct, but you are saving the binary math operators as strings before trying to use them as operators. So you have to use eval in that situation.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top