Simple simple question 
Author Message
 Simple simple question

Okay, this is bugging the hell out of me, and it really shouldn't be that
hard. Problem is, neither of the books I have tells me how to do it...

I'm reading a number from a file. Only thing in the file is that number.
No problem. Except when I use 'chop' to read it, it becomes a string. I
want to now manipulate this as a number. How? The command must exist -
hell, even AppleBASIC had the val()_ command.

So, how do I convert a number to a string and vice versa?

Any help would be appreciated.



Tue, 27 Jul 1999 03:00:00 GMT  
 Simple simple question


Quote:

>Okay, this is bugging the hell out of me, and it really shouldn't be that
>hard. Problem is, neither of the books I have tells me how to do it...

>I'm reading a number from a file. Only thing in the file is that number.
>No problem. Except when I use 'chop' to read it, it becomes a string. I
>want to now manipulate this as a number. How? The command must exist -
>hell, even AppleBASIC had the val()_ command.

>So, how do I convert a number to a string and vice versa?

Perl keeps a track of the last context a scalar was used in (string vs
numeric) but in general you just use the variable.  If you want to force
it into a numeric representation then you can add 0 e.g.

  $var = '00001000';
  $var += 0;

now printing $var will give 1000 rather than 00001000

Converting to a string may involve the use of sprintf depending on how you
want the variable to be represented.

Usually in perl you can just let things take care of themselves e.g.

  $var = "100\n";
  chomp $var;
  $var += 6.5;

is quite reasonable...

Hope this helps,

Mike
--

http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/    |                   65 F3 3F 1D 27 22 B7 41



Tue, 27 Jul 1999 03:00:00 GMT  
 Simple simple question

: Okay, this is bugging the hell out of me, and it really shouldn't be that
: hard. Problem is, neither of the books I have tells me how to do it...

Take a look at the following code, which might answer your question.

#!/usr/local/bin/perl -w

$value = '' unless $value; ### mollify perl -w
$file = '/nfic/users/nvp/Perl/File/counter.fil';

open(FILE, "+<$file")
    || die("No: $!\n");
chop($value = <FILE>);
print("You are visitor number: $value\n");
seek FILE,0,0;
print FILE $value+=1,"\n";
close(FILE);

--
Nathan V. Patwardhan

"What is your quest?"



Tue, 27 Jul 1999 03:00:00 GMT  
 Simple simple question


Quote:
> I'm reading a number from a file. Only thing in the file is that number.
> No problem. Except when I use 'chop' to read it, it becomes a string. I
> want to now manipulate this as a number. How?

Just Do It. If you use it as if it were a number, Lo! it IS a number. For
example...

   $foo = "3.14159 is a string";
   $bar = 0 + $foo;     # $bar is a number.
   print "$bar\n";    # ...but it can become a string for output.
   $foo = 0 + $foo;     # Now $foo is a number, too.
   $foo += 0;           # An easier way to do that last line.
   print $foo, "\n";  # Yep, that's a number all right.

Note that -w should warn you when you do something as suspicious as
converting a string with trailing non-whitespace into a number. Hope this
helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/

Randal Schwartz Case:     http://www.lightlink.com/fors/



Tue, 27 Jul 1999 03:00:00 GMT  
 Simple simple question

Quote:

> Okay, this is bugging the hell out of me, and it really shouldn't be that
> hard. Problem is, neither of the books I have tells me how to do it...
> I'm reading a number from a file. Only thing in the file is that number.
> No problem. Except when I use 'chop' to read it, it becomes a string. I
> want to now manipulate this as a number. How? The command must exist -
> hell, even AppleBASIC had the val()_ command.
> So, how do I convert a number to a string and vice versa?

When I want to convert a String into a Int, I do

$Int = $String + 0;

When I want to convert Integer to String I do :

$String = "$Integer";

Good Luck!



Sun, 01 Aug 1999 03:00:00 GMT  
 Simple simple question

Quote:

> > Okay, this is bugging the hell out of me, and it really shouldn't be that
> > hard. Problem is, neither of the books I have tells me how to do it...
> > I'm reading a number from a file. Only thing in the file is that number.
> > No problem. Except when I use 'chop' to read it, it becomes a string. I
> > want to now manipulate this as a number. How? The command must exist -
> > hell, even AppleBASIC had the val()_ command.
> > So, how do I convert a number to a string and vice versa?

Perl does it for you automatically depending on the context.  For instance:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$num = "58";                # A string...
$other_num = $num + 67;     # turns into a number in a numeric context
$third_num = $num + 5.67;

print ("Numbers: $num, $other_num, $third_num\n");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And the output is....

Numbers: 58, 125, 63.67
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is one of the cool features of Perl - I used it to solve a math
problem once, which was this: find all triples of three digit numbers such
that the sum
of the first two numbers equals the third number, AND each of the digits
1..9 only appear once in the three numbers.

       Examples: 783     729
                +162    +135
                 ---     ---
                 945     864

Using Perl's easy transition from integers to strings made the program
that solved this problem pretty easy.

The other cool part of this problem (in case anyone's interested in this
kind of thing around here): what can you say about the sum of the digits
in the answer?  For instance, 9+4+5 = 18, and 8+6+4 = 18.

-Ken Williams



Tue, 03 Aug 1999 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. simple, simple question

2. XS - Simple answer to simple question?

3. Simple Question from a simple chap!

4. Simple Question - LWP::Simple not accessing the internet

5. Simple Unix Question, ( Simple for you maybe )

6. Simple TCP client using io::socket::inet not so simple

7. A simple problem befuddling my simple head

8. simple, simple regex Q

9. Help with simple simple perl script...

10. Graphics::Simple 0.03 - C-64-like simple drawing

11. simple, simple LABEL Q

12. simple reference question

 

 
Powered by phpBB® Forum Software