Converting a retruned string value to a number

Hi List,
How do i convert a string variable returned by
some XPath API into a number so that I can 
compare it or loop using this number.

I am reading one number from XML and I want to
use it for looping.

Thanks and Regards,
Manish
0
manishs
4/6/2005 2:17:57 PM
perl.beginners 29360 articles. 3 followers. Follow

6 Replies
340 Views

Similar Articles

[PageSpeed] 38


----- Original Message -----
From: Manish Sapariya <manishs@gs-lab.com>
Date: Wednesday, April 6, 2005 10:17 am
Subject: Converting a retruned string value to a number

> Hi List,
HellO,
> How do i convert a string variable returned by
> some XPath API into a number so that I can 
> compare it or loop using this number.
You should not need to convert, Perl is smart enough to figure it out on it's own.

#!PERL
use warnings;
use strict;

my $te="123";
print $te if $te eq "123" && $te == 123;

> 
> I am reading one number from XML and I want to
> use it for looping.
> 
> Thanks and Regards,
> Manish
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 

0
mgoland
4/6/2005 4:02:04 PM
On Apr 6, 2005 5:17 PM, Manish Sapariya wrote:
> Hi List,
> How do i convert a string variable returned by
> some XPath API into a number so that I can
> compare it or loop using this number.
> 
> I am reading one number from XML and I want to
> use it for looping.
> 

In general, conversion from a string to a number is transparent in
Perl. "4" is the same as 4.

HTH,
-- 
Offer Kaye
0
offer
4/6/2005 4:11:39 PM
Am Mittwoch, 6. April 2005 18.02 schrieb mgoland@optonline.net:
> ----- Original Message -----
> From: Manish Sapariya <manishs@gs-lab.com>
> Date: Wednesday, April 6, 2005 10:17 am
> Subject: Converting a retruned string value to a number
>
> > Hi List,
>
> HellO,
>
> > How do i convert a string variable returned by
> > some XPath API into a number so that I can
> > compare it or loop using this number.
>
> You should not need to convert, Perl is smart enough to figure it out on
> it's own.

....and therefore...

> #!PERL
> use warnings;
> use strict;
>
> my $te="123";
> print $te if $te eq "123" && $te == 123;

....it's sufficient to compare numerical, which is more performant (I think):

print $te if $te == 123;

 :-)

(but your code won't produce a warning in the case that $te's value is not 
numeric)

[...]
0
security
4/6/2005 10:58:41 PM
Hello,
Thanks for all your replies.

This however does not work for me OR I dont understand
the problem I encountered.

This is the exact error that I encouter when running my
perl code.

--------------
Argument "XML::XPath::Node::Element=REF(0x86c31c8)->findvalue('loo..." isn't numeric in numeric eq (==) at ./gencap.pl line 124.
-----------------

The code snippet is

     57 my $doc = XML::XPath-> new(filename=>$specfile);
     58
     59 my @mails = $doc-> findnodes('//mail');
     60
     61 foreach my $mail (@mails) {


..
..
..
    123     $loopcount = "$mail->findvalue('loopcount')";
    124     if($loopcount == 0) {$loopcount = 1};

So is that the value returned by findvalue is not string and something
else.

Thanks and Regards,
Manish


On 04/06/2005 09:41 PM, Offer Kaye wrote:
> On Apr 6, 2005 5:17 PM, Manish Sapariya wrote:
> 
>>Hi List,
>>How do i convert a string variable returned by
>>some XPath API into a number so that I can
>>compare it or loop using this number.
>>
>>I am reading one number from XML and I want to
>>use it for looping.
>>
> 
> 
> In general, conversion from a string to a number is transparent in
> Perl. "4" is the same as 4.
> 
> HTH,
0
manishs
4/7/2005 5:29:13 AM
Hello,
I searched throught perldoc XML::Xpath and here what it has to say:
--------------------------------------------
       findvalue($path, [$context])

       Returns either a "XML::XPath::Literal", a "XML::XPath::Boolean" or a
       "XML::XPath::Number" object. If the path returns a NodeSet, $node-
       set->to_literal is called automatically for you (and thus a
       "XML::XPath::Literal" is returned). Note that for each of the objects
       stringification is overloaded, so you can just print the value found,
       or manipulate it in the ways you would a normal perl value (e.g. using
       regular expressions).
----------------------------------------------
But I am not able to get the numeric value as return value in following
code snippet.

    123     $loopcount = $mail->findvalue('loopcount')->value();
    124     if($loopcount == 0) {$loopcount = 1};

I get this error when I removed the quotes around findvalue call.

-------------------------------------------
Argument "" isn't numeric in numeric eq (==) at ./gencap.pl line 124.
--------------------------------------------

How do I go about this?

Thanks and Regards,
Manish



On 04/06/2005 09:41 PM, Offer Kaye wrote:
> On Apr 6, 2005 5:17 PM, Manish Sapariya wrote:
> 
>>Hi List,
>>How do i convert a string variable returned by
>>some XPath API into a number so that I can
>>compare it or loop using this number.
>>
>>I am reading one number from XML and I want to
>>use it for looping.
>>
> 
> 
> In general, conversion from a string to a number is transparent in
> Perl. "4" is the same as 4.
> 
> HTH,
0
manishs
4/7/2005 7:06:03 AM
Hello everybody,
Thanks for all the help.
I got it working. My xml file was not properly
consturcted and hence the problem. Has nothing
to do with the conversion.

Sorry for the noise created.

Thanks and Regards,
Manish

P.S. : I observe that my mail message dont appear
immediately in the newgroup. Is it the problem with
my mail client. I am using ThunderBird. I have not
still seen my last two messages. :-(.


On 04/06/2005 09:41 PM, Offer Kaye wrote:
> On Apr 6, 2005 5:17 PM, Manish Sapariya wrote:
> 
>>Hi List,
>>How do i convert a string variable returned by
>>some XPath API into a number so that I can
>>compare it or loop using this number.
>>
>>I am reading one number from XML and I want to
>>use it for looping.
>>
> 
> 
> In general, conversion from a string to a number is transparent in
> Perl. "4" is the same as 4.
> 
> HTH,
0
manishs
4/7/2005 7:19:40 AM
Reply: