my last question tonight... s///

Discussion in 'Perl Misc' started by Guy, Apr 20, 2009.

  1. Guy

    Guy Guest

    I have a list of number like 001, 002, 003, ... 100, in my first field. I
    want to remove the leading zeros. Should I do:

    $a = $field[0] + 0; # this appears to work

    or

    ($a = $field[0]) =~ s/^0+//; # this also appears to work.

    or is there yet a better way?
    Guy
     
    Guy, Apr 20, 2009
    #1
    1. Advertising

  2. Guy

    Guest

    On Sun, 19 Apr 2009 20:36:13 -0300, "Guy" <> wrote:

    >I have a list of number like 001, 002, 003, ... 100, in my first field. I
    >want to remove the leading zeros. Should I do:
    >
    >$a = $field[0] + 0; # this appears to work
    >
    >or
    >
    >($a = $field[0]) =~ s/^0+//; # this also appears to work.
    >
    >or is there yet a better way?
    >Guy
    >

    Why do anything. Its both a string and a number.
    Its how you use it. If you want to print it as a
    number use printf "%d",$a otherwise use printf "%s", $a.
    The trick is to be specific, think of it as a coersion.

    -sln
     
    , Apr 20, 2009
    #2
    1. Advertising

  3. Guy

    Guest

    On Sun, 19 Apr 2009 16:50:30 -0700, wrote:

    >On Sun, 19 Apr 2009 20:36:13 -0300, "Guy" <> wrote:
    >
    >>I have a list of number like 001, 002, 003, ... 100, in my first field. I
    >>want to remove the leading zeros. Should I do:
    >>
    >>$a = $field[0] + 0; # this appears to work
    >>
    >>or
    >>
    >>($a = $field[0]) =~ s/^0+//; # this also appears to work.
    >>
    >>or is there yet a better way?
    >>Guy
    >>

    >Why do anything. Its both a string and a number.
    >Its how you use it. If you want to print it as a
    >number use printf "%d",$a otherwise use printf "%s", $a.
    >The trick is to be specific, think of it as a coersion.
    >


    But if it bothers you, s/^0+// is what you want.

    -sln
     
    , Apr 20, 2009
    #3
  4. Guy

    Ben Bullock Guest

    On Sun, 19 Apr 2009 20:36:13 -0300, Guy wrote:

    > I have a list of number like 001, 002, 003, ... 100, in my first

    field. I
    > want to remove the leading zeros. Should I do:
    >
    > $a = $field[0] + 0; # this appears to work
    >
    > or
    >
    > ($a = $field[0]) =~ s/^0+//; # this also appears to work.
    >
    > or is there yet a better way?


    Another way to do it is $a = int $field[0].

    Which way is "better" depends what you mean by better - easier to read,
    faster, more compact, etc.
     
    Ben Bullock, Apr 20, 2009
    #4
    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. Roedy Green

    shut down your computers tonight

    Roedy Green, Oct 28, 2003, in forum: Java
    Replies:
    7
    Views:
    370
    Roedy Green
    Oct 29, 2003
  2. John
    Replies:
    1
    Views:
    338
    Leor Zolman
    Apr 12, 2004
  3. Guido van Rossum
    Replies:
    0
    Views:
    255
    Guido van Rossum
    Jan 15, 2004
  4. Replies:
    0
    Views:
    316
  5. Paul Prescod

    Reminder: Vanouver Python Meeting Tonight

    Paul Prescod, Apr 5, 2005, in forum: Python
    Replies:
    0
    Views:
    251
    Paul Prescod
    Apr 5, 2005
Loading...

Share This Page