The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Perl - String to Integer
SteveD
post Dec 23 2006, 12:40 AM
Post #1


Newbie
*

Group: Members
Posts: 18
Joined: 16-November 06
From: Iowa
Member No.: 936



I thought Perl would automatically convert strings to numbers, etc., based on the operator. However, I keep getting a warning message indicating it is confused.

I read a line in from a text file. The line has strings and integers. Everything works fine except the last element in the file is an integer. I'm guessing that since that also has the newline, chomp is making it a string. Then when I do a comparison on this value to a certain number, the warning says I have an uninitialized value at the > sign.

I tried doing a $int_var = $int_var + 0; thinking that would force it to a number but then I get an uninitialized value at + sign.

I took out the chomp command and that didn't help.

Isn't there a simple way to force a string to be an integer?

This post has been edited by SteveD: Dec 23 2006, 12:51 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Dec 23 2006, 03:55 AM
Post #2


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



QUOTE
I tried doing a $int_var = $int_var + 0; thinking that would force it to a number but then I get an uninitialized value at + sign.
Hmm... It sounds like something else is going on. I might have written $int_var += 0 ; or possibly $int_var *= 1 ; but that should make sure the $int_var is an integer. Not that it really matters until you use it in an expression, at which point it can be converted to an integer as necessary.

I suppose you could try adding a debugging print statement right before that point to see what value $int_var has.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
SteveD
post Dec 23 2006, 02:48 PM
Post #3


Newbie
*

Group: Members
Posts: 18
Joined: 16-November 06
From: Iowa
Member No.: 936



I'm an old assembly programmer, but I changed my code to $int_val += 0; just to make it better and that seemed to fix the problem. Not sure why but if it works I'm happy.

Thanks for the help.
Steve
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Curtis
post Dec 28 2006, 05:32 AM
Post #4


Newbie
*

Group: Members
Posts: 11
Joined: 28-December 06
From: California
Member No.: 1,397



Wow, that is odd, I can't seem to recreate the problem (I know issue is solved, but I'm curious). Perl is indeed dynamically and loosely typed.

I even tried:
CODE
#!C:/Perl/bin/perl

use strict;
use warnings;

# Print errors to browser, rather than server 500 internal errors
use CGI::Carp qw/fatalsToBrowser/;
use Data::Dumper;

# Header
print "Content-Type: text/plain\r\n\r\n";

my $int_val = 'abc15';
#$int_val = $int_val + 0;

print Dumper($int_val) if ( $int_val < 20 );


This post has been edited by Curtis: Dec 28 2006, 05:33 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 03:37 AM