Strings and Operators

Discussion in 'Perl' started by JamesW, Feb 20, 2004.

  1. JamesW

    JamesW Guest

    Sorry if this occurs twice, my nicely typed one just vanished. Anyway in brief:

    I want to store a range in a string, e.g. '2-5'.

    I tried $range = $numOne.'-'.$numTwo+1;

    And $range stored the sum of $numOne+1!
    I got it to work with,

    $range = $numOne.'-'.($numTwo+1);

    I understand why this works not not why my initial instinct didn't.
    Could someone please enlighten me?

    Thanks
    James
     
    JamesW, Feb 20, 2004
    #1
    1. Advertising

  2. JamesW

    dna Guest

    this problem relates to operator/list precedence with and without
    parenthesis.

    for a good reference please read through 'perlop' in the perldocs. you will
    be enlightened!

    good luck

    "JamesW" <> wrote in message
    news:...
    > Sorry if this occurs twice, my nicely typed one just vanished. Anyway in

    brief:
    >
    > I want to store a range in a string, e.g. '2-5'.
    >
    > I tried $range = $numOne.'-'.$numTwo+1;
    >
    > And $range stored the sum of $numOne+1!
    > I got it to work with,
    >
    > $range = $numOne.'-'.($numTwo+1);
    >
    > I understand why this works not not why my initial instinct didn't.
    > Could someone please enlighten me?
    >
    > Thanks
    > James
     
    dna, Feb 20, 2004
    #2
    1. Advertising

  3. JamesW

    dna Guest

    another thing, i would bet that specifying the -w option would have thrown
    an error/warning when you tried to run that code... make sure you try to
    always do:

    #!/usr/bin/perl -w

    use strict; ## mandatory as well! :)

    Code:
    
    "dna" <magmalianathotmaildotcom> wrote in message
    news:...[color=blue]
    >
    > this problem relates to operator/list precedence with and without
    > parenthesis.
    >
    > for a good reference please read through 'perlop' in the perldocs.  you[/color]
    will[color=blue]
    > be enlightened!
    >
    > good luck
    >
    > "JamesW" <> wrote in message
    > news:...[color=green]
    > > Sorry if this occurs twice, my nicely typed one just vanished.  Anyway[/color][/color]
    in[color=blue]
    > brief:[color=green]
    > >
    > > I want to store a range in a string, e.g.  '2-5'.
    > >
    > > I tried $range = $numOne.'-'.$numTwo+1;
    > >
    > > And $range stored the sum of $numOne+1!
    > > I got it to work with,
    > >
    > > $range = $numOne.'-'.($numTwo+1);
    > >
    > > I understand why this works not not why my initial instinct didn't.
    > > Could someone please enlighten me?
    > >
    > > Thanks
    > > James[/color]
    >
    >[/color]
     
    dna, Feb 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    717
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    387
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    574
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page