convert string to number

Hello, if I use the sprintf function it would give me a number STRING, but =
not number, so I have to trick the string to become number by adding and su=
btracting the number with 1. Is there a better way than this ? I want it to=
 be a number data type and not string.=0A=0Ae.g =0A=0Amy $strNumber =3D spr=
intf("%04d", 123);=0Amy $number =3D $strNumber + 1 - 1; # to trick Perl to =
convert to number datatype instead of string. =0A=0A=0AThanks,=0A=0AWilliam=
=0A=0ASend instant messages to your online friends http://uk.messenger.yaho=
o.com 
0
esia168
6/15/2008 12:09:51 PM
📁 perl.beginners
📃 29388 articles.
⭐ 4 followers.

💬 11 Replies
👁️‍🗨️ 974 Views


On Sun, Jun 15, 2008 at 8:09 PM, William <esia168@yahoo.com> wrote:
>
> my $strNumber = sprintf("%04d", 123);
instead of that, you would say:
my $strNumber = sprintf("%d", 123);
This is already a number.
-- 
Jeff Peng - Peng.Kyo@gmail.com
Professional Squid supports in China
http://www.ChinaSquid.com/
0
peng
6/15/2008 12:38:43 PM
William schreef:
> Hello, if I use the sprintf function it would give me a number
> STRING, but not number, so I have to trick the string to become
> number by adding and subtracting the number with 1. Is there a better
> way than this ? I want it to be a number data type and not string.
>
> e.g
>
> my $strNumber = sprintf("%04d", 123);
> my $number = $strNumber + 1 - 1; # to trick Perl to convert to number
> datatype instead of string.

    my $number = 0+ $strNumber;
or "in place":
    $strNumber += 0;
But there really is no need! The first time you use the variable as a
number, it will become a number (or rather a numeric reality will be
added to it), which is used in further numeric contexts.
-- 
Affijn, Ruud
"Gewoon is een tijger."
0
rvtol
6/15/2008 3:01:40 PM
>     my $number =3D 0+ $strNumber;=0A> =0A> or "in place":=0A> =0A>     $s=
trNumber +=3D 0;=0A> =0A> But there really is no need! The first time you u=
se the variable as a=0A> number, it will become a number (or rather a numer=
ic reality will be=0A> added to it), which is used in further numeric conte=
xts=0A=0Ahaha instead of adding and substracting 1 , it's even better addin=
g zero,=0AI understand what you are talking about, but  there is really a n=
eed for me to convert to number while I am using =0Athe module, Language::P=
rolog::Yaswi,  to convert the perl datatype into prolog dataatype, which  i=
s another language. If I did not do so, it would treat that as string inste=
ad of number. So I think there is not better way than adding 0.=0A=0A=0ATha=
nk you.=0A=0ASend instant messages to your online friends http://uk.messeng=
er.yahoo.com 
0
esia168
6/15/2008 3:40:36 PM
William schreef:
> [attribution dammit] Ruud:
>>     my $number = 0+ $strNumber;
>>
>> or "in place":
>>
>>     $strNumber += 0;
>>
>> But there really is no need! The first time you use the variable as a
>> number, it will become a number (or rather a numeric reality will be
>> added to it), which is used in further numeric contexts
>
> haha instead of adding and substracting 1 , it's even better adding
> zero,
> I understand what you are talking about, but  there is really a need
> for me to convert to number while I am using
> the module, Language::Prolog::Yaswi,  to convert the perl datatype
> into prolog dataatype, which  is another language. If I did not do
> so, it would treat that as string instead of number. So I think there
> is not better way than adding 0.
There is no real way to test if a value inside a variable has a numeric
"personality". Variables can have multiple "personalities", each with
there own binary value.
So maybe the problem just comes from the leading zeroes that your
sprintf format added?
-- 
Affijn, Ruud
"Gewoon is een tijger."
0
rvtol
6/15/2008 3:57:48 PM
> There is no real way to test if a value inside a variable has a numeric=
=0A> "personality". Variables can have multiple "personalities", each with=
=0A> there own binary value.=0A> =0A> So maybe the problem just comes from =
the leading zeroes that your=0A> sprintf format added?=0A=0A=0ANot exactly =
from the leading zeros,=0A=0Ause Data::Dumper;=0Amy $numStr =3D sprintf("%4=
d", 1234);=0Aprint (Dumper $numStr), "\n";=0A=0Amy $numStr =3D sprintf("%05=
d", 1234);=0Aprint (Dumper $numStr), "\n";=0A=0Amy $num =3D sprintf("%05d",=
 1234);=0A$num +=3D 0;=0Aprint (Dumper $num), "\n";=0A=0A=0A  use Language:=
:Prolog::Types::overload;=0A=0A  use Language::Prolog::Types qw(:ctors);=0A=
=0A  print prolog_functor('foo', $numStr), "\n";=0A  print prolog_functor('=
foo', $num), "\n";=0A=0A=0A$VAR1 =3D '1234';=0A$VAR1 =3D '01234';=0A$VAR1 =
=3D 1234;=0Afoo(01234)=0Afoo(1234)=0A=0A=0AAs you can see the last of $VAR1=
 does not have single quote, so it's really in number data type. The proble=
m is I need to use the number variable in prolog_functor function=0A=0AThis=
 is module that serve as a glue between Perl and Programming Logic (Prolog)=
 language, if it's not =0Ain number data type it will become a string in Pr=
olog language (in Prolog it is called atom). =0A=0AI think so far, adding z=
ero solve the problem.=0A=0AThanks.=0A=0ASend instant messages to your onli=
ne friends http://uk.messenger.yahoo.com 
0
esia168
6/15/2008 4:31:38 PM
William schreef:
> [*attribution dammit*] Ruud:
>> There is no real way to test if a value inside a variable has a
>> numeric "personality". Variables can have multiple "personalities",
>> each with there own binary value.
>>
>> So maybe the problem just comes from the leading zeroes that your
>> sprintf format added?
>
> Not exactly from the leading zeros,
>
> use Data::Dumper;
> my $numStr = sprintf("%4d", 1234);
> print (Dumper $numStr), "\n";
>
> my $numStr = sprintf("%05d", 1234);
> print (Dumper $numStr), "\n";
>
> my $num = sprintf("%05d", 1234);
> $num += 0;
> print (Dumper $num), "\n";
>
>
>   use Language::Prolog::Types::overload;
>
>   use Language::Prolog::Types qw(:ctors);
>
>   print prolog_functor('foo', $numStr), "\n";
>   print prolog_functor('foo', $num), "\n";
>
>
> $VAR1 = '1234';
> $VAR1 = '01234';
> $VAR1 = 1234;
> foo(01234)
> foo(1234)
>
>
> As you can see the last of $VAR1 does not have single quote, so it's
> really in number data type.
It is not "in number data type", it is merely available as numeric. (as
I have said before)

> I think so far, adding zero solve the problem.
That is indeed one of the ways to add a numeric personality to a
variable.
Testing with a numeric operator also "works", see below:
$ perl -Mstrict -Mwarnings -MData::Dumper -wle'
   my $s = "1234";
   print Dumper($s);
   print 1 if $s == 0;
   print Dumper($s);
   print 2 if $s eq "1234";
   print Dumper($s);
'
$VAR1 = '1234';
$VAR1 = 1234;
2
$VAR1 = 1234;
That Data::Dumper prefers to print the numeric face of the variable, if
available, is a matter of choice inside the code of Data::Dumper.
Maybe the other modules that you use, have Data::Dumper embedded?
-- 
Affijn, Ruud
"Gewoon is een tijger."
0
rvtol
6/15/2008 5:01:34 PM
> Testing with a numeric operator also "works", see below:=0A> =0A> $ perl =
-Mstrict -Mwarnings -MData::Dumper -wle'=0A>    my $s =3D "1234";=0A>    pr=
int Dumper($s);=0A>    print 1 if $s =3D=3D 0;=0A>    print Dumper($s);=0A>=
    print 2 if $s eq "1234";=0A>    print Dumper($s);=0A> '=0A> $VAR1 =3D '=
1234';=0A> =0A> $VAR1 =3D 1234;=0A> =0A> 2=0A> $VAR1 =3D 1234;=0A> =0A=0ATh=
at is interesting.=0A=0A=0A> That Data::Dumper prefers to print the numeric=
 face of the variable, if=0A> available, is a matter of choice inside the c=
ode of Data::Dumper.=0A> Maybe the other modules that you use, have Data::D=
umper embedded?=0A=0AThat I am not sure, I have to dig into=0ALanguage::Pro=
log::Types::overload;=0A=0A, if I got time. =0A=0A=0AThank you.=0A=0A=0ASen=
d instant messages to your online friends http://uk.messenger.yahoo.com 
0
esia168
6/15/2008 6:14:29 PM
William schreef:
>> Testing with a numeric operator also "works", see below:
>>
>> $ perl -Mstrict -Mwarnings -MData::Dumper -wle'
>>    my $s = "1234";
>>    print Dumper($s);
>>    print 1 if $s == 0;
>>    print Dumper($s);
>>    print 2 if $s eq "1234";
>>    print Dumper($s);
>> '
>> $VAR1 = '1234';
>>
>> $VAR1 = 1234;
>>
>> 2
>> $VAR1 = 1234;
>>
>
> That is interesting.
Yes, and knowing this can save you performance loss. For example with
mod_perl a variable doesn't stay shared (between forked off processes)
if any of its guts get changed. This leads to code like:
    our $shared_number = "123.45"; $shared_number == $_ and 1 for 0,
0.1;
If you do this before forking, the variable has much more solid guts. :)
-- 
Affijn, Ruud
"Gewoon is een tijger."
0
rvtol
6/16/2008 1:12:05 AM
On Jun 15, 10:09=A0pm, esia...@yahoo.com (William) wrote:
> Hello, if I use the sprintf function it would give me a number STRING, but=
 not number, so I have to trick the string to become number by adding and su=
btracting the number with 1. Is there a better way than this ? I want it to =
be a number data type and not string.
>
> e.g
>
> my $strNumber =3D sprintf("%04d", 123);
> my $number =3D $strNumber + 1 - 1; # to trick Perl to convert to number da=
tatype instead of string.
The only improvement I can see is the rather obvious:
my $number =3D 123;
The sprintf() function *always* returns a string (PV). It therefore
follows that if you want to convert what sprintf() returned into a
number (IV, UV, or NV) then you *must* perform some "trick" on that
returned value. You can do it as:
my $number =3D sprintf("%04d", 123) + 1 - 1; # your original approach
or
my $number =3D sprintf("%04d", 123) + 0;
or
my $number =3D sprintf("%04d", 123) * 1;
And there are other similar convoluted approaches available to you.
You can possibly even get away with such things as:
my $number =3D exp(log(sprintf("%04d", 123))); # not recommending
this :-)
The thing that strikes me as strange is this:
Given that sprintf() always returns a string, and given that you need
a number, why on earth are you using sprintf() in the first place ?
(Perhaps it was just for demonstration purposes ?)
Cheers,
Rob
0
sisyphus359
6/16/2008 1:32:21 PM
> use strict;=0A> use warnings;=0A> =0A> use Devel::Peek qw/Dump/;=0A> =0A>=
 my $numstr =3D sprintf("%4d", 1234);=0A> Dump $numstr;=0A> =0A> $numstr =
=3D sprintf("%05d", 1234);=0A> Dump $numstr;=0A> =0A> my $num =3D sprintf("=
%05d", 1234);=0A> $num +=3D 0;=0A> Dump $num;=0A> =0A> **OUTPUT**=0A> =0A> =
SV =3D PV(0x365ec) at 0x3652c=0A>   REFCNT =3D 1=0A>   FLAGS =3D (PADBUSY,P=
ADMY,POK,pPOK)=0A>   PV =3D 0x182ea94 "1234"\0=0A>   CUR =3D 4=0A>   LEN =
=3D 8=0A> SV =3D PV(0x365ec) at 0x3652c=0A>   REFCNT =3D 1=0A>   FLAGS =3D =
(PADBUSY,PADMY,POK,pPOK)=0A>   PV =3D 0x182ea94 "01234"\0=0A>   CUR =3D 5=
=0A>   LEN =3D 8=0A> SV =3D PVIV(0x37604) at 0x357c4=0A>   REFCNT =3D 1=0A>=
   FLAGS =3D (PADBUSY,PADMY,IOK,pIOK)=0A>   IV =3D 1234=0A>   PV =3D 0x183b=
a74 "01234"\0=0A>   CUR =3D 5=0A>   LEN =3D 8=0A> =0A> So in the first two =
cases you can see that the POK flag (denoting a string=0A> value) is set, b=
ut in the third case, after adding zero, the IOK flag becomes=0A> set (indi=
cating an integer) while the POK flag is cleared, meaning that the the=0A> =
string value is no longer valid, even though there is still space allocated=
 for=0A> it and its value is unchanged.=0A=0AThank you, now it's very clear=
 to me. So scalar variable might look very flexible to our programmer's eye=
s as it can contain either integer or string, but I never know what is happ=
ening behind the scene until you show me that one. It create a big problem =
to me when I did not convert it to integer first before getting into the SW=
I-Prolog . Because Perl string means atom in SWI-Prolog, only Perl number m=
eans number in SWI-Prolog.=0A=0AThanks,=0A=0AWilliam=0A=0A=0ASend instant m=
essages to your online friends http://uk.messenger.yahoo.com 
0
esia168
6/16/2008 5:46:35 PM
William wrote:
>> use strict;
>> use warnings;
>>
>> use Devel::Peek qw/Dump/;
>>
>> my $numstr = sprintf("%4d", 1234);
>> Dump $numstr;
>>
>> $numstr = sprintf("%05d", 1234);
>> Dump $numstr;
>>
>> my $num = sprintf("%05d", 1234);
>> $num += 0;
>> Dump $num;
>>
>> **OUTPUT**
>>
>> SV = PV(0x365ec) at 0x3652c
>>   REFCNT = 1
>>   FLAGS = (PADBUSY,PADMY,POK,pPOK)
>>   PV = 0x182ea94 "1234"\0
>>   CUR = 4
>>   LEN = 8
>> SV = PV(0x365ec) at 0x3652c
>>   REFCNT = 1
>>   FLAGS = (PADBUSY,PADMY,POK,pPOK)
>>   PV = 0x182ea94 "01234"\0
>>   CUR = 5
>>   LEN = 8
>> SV = PVIV(0x37604) at 0x357c4
>>   REFCNT = 1
>>   FLAGS = (PADBUSY,PADMY,IOK,pIOK)
>>   IV = 1234
>>   PV = 0x183ba74 "01234"\0
>>   CUR = 5
>>   LEN = 8
>>
>> So in the first two cases you can see that the POK flag (denoting a string
>> value) is set, but in the third case, after adding zero, the IOK flag becomes
>> set (indicating an integer) while the POK flag is cleared, meaning that the the
>> string value is no longer valid, even though there is still space allocated for
>> it and its value is unchanged.
> 
> Thank you, now it's very clear to me. So scalar variable might look very
> flexible to our programmer's eyes as it can contain either integer or string,
> but I never know what is happening behind the scene until you show me that one.
> It create a big problem to me when I did not convert it to integer first before
> getting into the SWI-Prolog . Because Perl string means atom in SWI-Prolog, only
> Perl number means number in SWI-Prolog.
My apologies, I'm afraid we lost track of your purpose. My best advice is to
force the value to numeric or string at the point of call and *comment* it so
that it doesn't look like a mistake.
  my $ypm = '17'.'60';
  dbase_function($ypm+0);          # Call requires a numeric value
or
  my $index = sprintf '%05d', $n;
  dbase_function("$index");        # Call requires a string value
HTH,
Rob
0
rob
6/16/2008 9:49:56 PM
Reply: