By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

simple perl program in python gives errors

P: n/a
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python, i.e:

$a += 200000 * 140000;

print $a;

Jan 30 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Maybe you should initialize a before using it? :)

Jan 30 '06 #2

P: n/a
On 2006-01-30, sq****@peoriadesignweb.com <sq****@peoriadesignweb.com> wrote:
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python,


Ah, well, there's your problem. Python doesn't run Perl
programs. It runs Python programs. If you write your program
in Python instead of Perl, you'll not have such problems.

--
Grant Edwards grante Yow! If this was a SWEDISH
at MOVIE, I'd take off your
visi.com GO-GO BOOTS!!
Jan 30 '06 #3

P: n/a
On Monday 30 January 2006 17:52, sq****@peoriadesignweb.com wrote:
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python, i.e:

$a += 200000 * 140000;

print $a;


a = 0
a += 200000 * 140000
print a

Seems to be more a problem for you than for Python.

Christoph

P.S.: Xah, is that you?
P.P.S.: You should have told what's going wrong.
P.P.P.S.: Variables are not automatically initialised in Python.
And you shouldn't even rely on that in Perl.
--
Never trust a system administrator who wears a tie and suit.
Jan 30 '06 #4

P: n/a
Of course Grant Edwards is right, but I thought that your programm is
something like this:

------------------------
a += 200000 * 140000
print a
------------------------

and I thought that this is your perl program:
--------------------
$a += 200000 * 140000
print $a
--------------------

Funny if Grant Edwards is right... :)

Jan 30 '06 #5

P: n/a
i was hoping one didnt have to initialize variables because perl
defaults their value to zero. Also I noticed if I initialize a variable
as 0 , then I can only do integer math not floating math. this just
seems kind of backward as I am used to php and perl which dont require
such strict rules.

Jan 30 '06 #6

P: n/a
sq****@peoriadesignweb.com wrote:
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python, i.e:

$a += 200000 * 140000;

print $a;

Yes, an annoying feature of the Python interpreter is that it doesn't
always interpret Perl correctly.

You will find, though, that

print 200000 * 140000

works quite well in both languages (with or without the trailing
semi-colon).

Variables in Python don't need a $ or @ to indicate whether they are
scalar or structured - that depends on their value.

So

a = 0
a += 200000 * 140000

should also work quite well. Note the first assignment is needed because
while there's no need to declare variables, you cannot assume a value
until they have been assigned one (in Pythonic jargon "bound to a value").

What does "+=" mean in Perl, by the way? Does it just assume $a is zero
if it isn't currently existent?

happily-forgotten-perl-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 30 '06 #7

P: n/a
sq****@peoriadesignweb.com wrote:
Also I noticed if I initialize a variable
as 0 , then I can only do integer math not floating math. this just
seems kind of backward as I am used to php and perl which dont require
such strict rules.


Not quite:
foo = 0
foo += 122
print foo 122 print foo / 7 17 print foo / 7.0

17.4285714286
Jan 30 '06 #8

P: n/a
On 2006-01-30, sq****@peoriadesignweb.com <sq****@peoriadesignweb.com> wrote:
i was hoping one didnt have to initialize variables because perl
defaults their value to zero.
Repeat after me: "Python is not Perl. Python is not Perl.
Python is not Perl. Python is not Perl. Python is not Perl.
Python is not Perl. Python is not Perl.Python is not Perl.
Python is not Perl. Python is not Perl. Python is not
Perl.Python is not Perl. Python is not Perl."
Also I noticed if I initialize a variable
as 0 , then I can only do integer math not floating math.
Python is a strictly typed language. Perl isn't -- Perl does
all sorts of stuff "automagically" by trying to guess what you
wanted". I perfer languages that do exactly what I tell them
to rather than what the language's author thought I might have
meant.
this just seems kind of backward as I am used to php and perl
which dont require such strict rules.


Really, and Perl/PHP seemed so primitive and backward to me. I
guess it's all in the perspective.

--
Grant Edwards grante Yow! Darling, my ELBOW
at is FLYING over FRANKFURT,
visi.com Germany...
Jan 30 '06 #9

P: n/a
On Mon, 30 Jan 2006, Grant Edwards wrote:
On 2006-01-30, sq****@peoriadesignweb.com <sq****@peoriadesignweb.com> wrote:
i was hoping one didnt have to initialize variables because perl
defaults their value to zero. Also I noticed if I initialize a variable
as 0 , then I can only do integer math not floating math.


Python is a strictly typed language. Perl isn't -- Perl does all sorts
of stuff "automagically" by trying to guess what you wanted". I perfer
languages that do exactly what I tell them to rather than what the
language's author thought I might have meant.


Especially when that's Larry Wall ... :)

tom

--
Don't trust the laws of men. Trust the laws of mathematics.
Jan 30 '06 #10

P: n/a
sq****@peoriadesignweb.com a écrit :
I am a little annoyed at why such a simple program in Perl is causing
so much difficulty for python, i.e:

$a += 200000 * 140000;

print $a;


a = 200000 * 140000
print a

where's the problem ?
Jan 31 '06 #11

P: n/a

On Jan 31, 2006, at 8:28 AM, morris carre wrote:
sq****@peoriadesignweb.com a écrit :

$a += 200000 * 140000;
print $a;


a = 200000 * 140000
print a

where's the problem ?


Not sure if you typo'd that, but that should read:

a += 200000 * 140000
print a

-dan

--
I am not a vegetarian because I love animals; I am a vegetarian because
I hate plants. -A. Whitney Brown
Jan 31 '06 #12

P: n/a
Dan Lowe <da*@tangledhelix.com> wrote:

Not sure if you typo'd that, but that should read:

a += 200000 * 140000
print a


Did you try to run that?
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Feb 1 '06 #13

P: n/a

On Feb 1, 2006, at 7:59 AM, Tim Roberts wrote:
Dan Lowe <da*@tangledhelix.com> wrote:

Not sure if you typo'd that, but that should read:

a += 200000 * 140000
print a


Did you try to run that?


Well, I did, but I had given 'a' a value first. And now I'm thinking
that I may have forgotten some of the earlier thread... where I
suppose the point was that this doesn't work when 'a' hasn't already
been set. I was focusing on = vs += ...

So in that case, please ignore me :-)

-dan

--
Black holes are where God divided by zero. -Steven Wright
Feb 1 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.