removing . from a string

P

pkassies

Hello everybody,

I need to strip "90.00" back to "90".
I wrote the following program

#!/perl

$item = "90.0";
print &strip_dot($item);

sub strip_dot
{
print "1 - $_[0]\n";
if ($_[0] =~ m/./)
{
print "2 - $_[0]\n";
return substr ($_[0], 0, index($_[0],"."));
} else
{
print "3 - $_[0]\n";
return "$_[0]";
}
}

its output is:
1 - 90.0
2 - 90.0
90
Great that works.

but if I make $item = "90";
the output is:
1 - 90
2 - 90
9

Hmm, that is not what I want.
The if statement should generate FALSE and it shouldn't be stripped to
9.
How can I solve this?

Any help is appreaciated.
 
L

Lambik

Hello everybody,

I need to strip "90.00" back to "90".
I wrote the following program

#!/perl

$item = "90.0";
print &strip_dot($item);

sub strip_dot
{
print "1 - $_[0]\n";
if ($_[0] =~ m/./)

You need to escape the dot m/\./ but that only removes the dot, not the
figures behind it.
{
print "2 - $_[0]\n";
return substr ($_[0], 0, index($_[0],"."));
} else
{
print "3 - $_[0]\n";
return "$_[0]";
}
}

its output is:
1 - 90.0
2 - 90.0
90
Great that works.

but if I make $item = "90";
the output is:
1 - 90
2 - 90
9
Hmm, that is not what I want.
The if statement should generate FALSE and it shouldn't be stripped to
9.
How can I solve this?

easiest way is perl -e "print int 90.00"
 
J

J. Gleixner

Hello everybody,

I need to strip "90.00" back to "90".
I wrote the following program

#!/perl

$item = "90.0";
print &strip_dot($item);

sub strip_dot
{
print "1 - $_[0]\n";
if ($_[0] =~ m/./)
{
print "2 - $_[0]\n";
return substr ($_[0], 0, index($_[0],"."));
} else
{
print "3 - $_[0]\n";
return "$_[0]";
}
}

its output is:
1 - 90.0
2 - 90.0
90
Great that works.

but if I make $item = "90";
the output is:
1 - 90
2 - 90
9

Hmm, that is not what I want.
The if statement should generate FALSE and it shouldn't be stripped to
9.
How can I solve this?

Any help is appreaciated.

You need to escape it.

if ($_[0] =~ m/\./)

See perldoc perlre
" . Match any character (except newline) "

Other options:

my $val = '90.0';
$val =~ s/\.*//;

Or:
$val = int $val;

Or:
$val = $val + 0;
 
G

Gunnar Hjalmarsson

I need to strip "90.00" back to "90".

There are built-in functions to help you with that.

my $item = '90.00';
print int $item;

But that just cuts the decimals. If you actually want to round the
number correctly, you can do:

my $item = '90.00';
printf '%.0f', $item;

See:

perldoc -f int
perldoc -f sprintf
perldoc -f printf
I wrote the following program

#!/perl

use strict;
use warnings;
$item = "90.0";

my $item = "90.0";
print &strip_dot($item);
--------^
Don't use '&' in function calls unless you know what it means (in Perl 5).
sub strip_dot
{
print "1 - $_[0]\n";
if ($_[0] =~ m/./)

You need to escape the dot, or else you just check if the string
contains any character...

if ($_[0] =~ m/\./)
 
T

Tad McClellan

I need to strip "90.00" back to "90".


-------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $item = "90.0";
$item += 0;
print "$item\n";
 
C

Clenna Lumina

Tad said:
-------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $item = "90.0";
$item += 0;
print "$item\n";
-------------------------

1) This code doesn't work as expected if the fraction part is
anything other than .0 (i.e., using "90.4" will end up
printing 90.4 )

2) I thought it was generally frowned upon to quote a variable
this way, rather than, say, C< print $itme, "\n"; > or so?
 
C

Clenna Lumina

Sherm said:
No, what's frowned upon is *unnecessary* quoting that accomplishes
nothing, as in C<print "$item", "\n";>. If you want to print a
variable with a new- line, then double-quoting is a perfectly valid
way to do that.

There are certainly occasions where passing a list to print() is more
efficient and/or readable than interpolation. I wouldn't say that just
printing a single scalar followed by a newline is one of those
situations though.

Understood.

I was referring more to situations like C< my $s = "$t"; > vs C< my
$s = $t; > but I can see I may have been in error about the former
usage in terms of print and adding simply a newline. I was thinking at
the time that C< print "$item\n"; > would be better written akin to
the latter usage.
 
T

Tad McClellan

Clenna Lumina said:
1) This code doesn't work as expected if the fraction part is
anything other than .0 (i.e., using "90.4" will end up
printing 90.4 )


The OP did not ask for code that works if the fraction part is
anything other than .0

The value of an answer is often in direct proportion to the
care taken in framing the question.

Ask a poor question, get a poor answer.

2) I thought it was generally frowned upon to quote a variable
this way, rather than, say, C< print $itme, "\n"; > or so?


No, quoting a *lone* variable is warned about in the FAQ.

I was not quoting a lone variable, and I in fact did want to make
use of one of the two things that double quoting gives you
(backslash escapes and/or interpolation).
 

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
474,204
Messages
2,571,063
Members
47,671
Latest member
peterweyand

Latest Threads

Top