my last question tonight... s///

G

Guy

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
 
S

sln

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
 
S

sln

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
 
B

Ben Bullock

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top