simple perl program in python gives errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • squash@peoriadesignweb.com

    #1

    simple perl program in python gives errors

    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;

  • kishkin

    #2
    Re: simple perl program in python gives errors

    Maybe you should initialize a before using it? :)

    Comment

    • Grant Edwards

      #3
      Re: simple perl program in python gives errors

      On 2006-01-30, squash@peoriade signweb.com <squash@peoriad esignweb.com> wrote:
      [color=blue]
      > I am a little annoyed at why such a simple program in Perl is causing
      > so much difficulty for python,[/color]

      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!!

      Comment

      • Christoph Haas

        #4
        Re: simple perl program in python gives errors

        On Monday 30 January 2006 17:52, squash@peoriade signweb.com wrote:[color=blue]
        > 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;[/color]

        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.

        Comment

        • kishkin

          #5
          Re: simple perl program in python gives errors

          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... :)

          Comment

          • squash@peoriadesignweb.com

            #6
            Re: simple perl program in python gives errors

            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.

            Comment

            • Steve Holden

              #7
              Re: simple perl program in python gives errors

              squash@peoriade signweb.com wrote:[color=blue]
              > 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;
              >[/color]
              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/

              Comment

              • John Zenger

                #8
                Re: simple perl program in python gives errors

                squash@peoriade signweb.com wrote:[color=blue]
                > 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.
                >[/color]

                Not quite:
                [color=blue][color=green][color=darkred]
                >>> foo = 0
                >>> foo += 122
                >>> print foo[/color][/color][/color]
                122[color=blue][color=green][color=darkred]
                >>> print foo / 7[/color][/color][/color]
                17[color=blue][color=green][color=darkred]
                >>> print foo / 7.0[/color][/color][/color]
                17.4285714286

                Comment

                • Grant Edwards

                  #9
                  Re: simple perl program in python gives errors

                  On 2006-01-30, squash@peoriade signweb.com <squash@peoriad esignweb.com> wrote:
                  [color=blue]
                  > i was hoping one didnt have to initialize variables because perl
                  > defaults their value to zero.[/color]

                  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."
                  [color=blue]
                  > Also I noticed if I initialize a variable
                  > as 0 , then I can only do integer math not floating math.[/color]

                  Python is a strictly typed language. Perl isn't -- Perl does
                  all sorts of stuff "automagica lly" 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.
                  [color=blue]
                  > this just seems kind of backward as I am used to php and perl
                  > which dont require such strict rules.[/color]

                  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...

                  Comment

                  • Tom Anderson

                    #10
                    Re: simple perl program in python gives errors

                    On Mon, 30 Jan 2006, Grant Edwards wrote:
                    [color=blue]
                    > On 2006-01-30, squash@peoriade signweb.com <squash@peoriad esignweb.com> wrote:
                    >[color=green]
                    >> 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.[/color]
                    >
                    > Python is a strictly typed language. Perl isn't -- Perl does all sorts
                    > of stuff "automagica lly" 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.[/color]

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

                    tom

                    --
                    Don't trust the laws of men. Trust the laws of mathematics.

                    Comment

                    • morris carre

                      #11
                      Re: simple perl program in python gives errors

                      squash@peoriade signweb.com a écrit :[color=blue]
                      > 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;
                      >[/color]

                      a = 200000 * 140000
                      print a



                      where's the problem ?

                      Comment

                      • Dan Lowe

                        #12
                        Re: simple perl program in python gives errors


                        On Jan 31, 2006, at 8:28 AM, morris carre wrote:
                        [color=blue]
                        > squash@peoriade signweb.com a écrit :[color=green]
                        >>
                        >> $a += 200000 * 140000;
                        >> print $a;[/color]
                        >
                        > a = 200000 * 140000
                        > print a
                        >
                        > where's the problem ?[/color]

                        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


                        Comment

                        • Tim Roberts

                          #13
                          Re: simple perl program in python gives errors

                          Dan Lowe <dan@tangledhel ix.com> wrote:[color=blue]
                          >
                          >Not sure if you typo'd that, but that should read:
                          >
                          >a += 200000 * 140000
                          >print a[/color]

                          Did you try to run that?
                          --
                          - Tim Roberts, timr@probo.com
                          Providenza & Boekelheide, Inc.

                          Comment

                          • Dan Lowe

                            #14
                            Re: simple perl program in python gives errors


                            On Feb 1, 2006, at 7:59 AM, Tim Roberts wrote:
                            [color=blue]
                            > Dan Lowe <dan@tangledhel ix.com> wrote:[color=green]
                            >>
                            >> Not sure if you typo'd that, but that should read:
                            >>
                            >> a += 200000 * 140000
                            >> print a[/color]
                            >
                            > Did you try to run that?[/color]

                            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


                            Comment

                            Working...