Connecting Tech Pros Worldwide Forums | Help | Site Map

How to gvie data type to particular variable

Newbie
 
Join Date: Oct 2008
Posts: 19
#1: Oct 27 '08
Hello friends,
I am new to perl ,How to give data types to particular variable:
For example:
If x is a scalar variable of type signed integer or unsigned interger how to represent it?


Thanks
Raghavendra

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Oct 27 '08

re: How to gvie data type to particular variable


Quote:

Originally Posted by raghavendrap

Hello friends,
I am new to perl ,How to give data types to particular variable:
For example:
If x is a scalar variable of type signed integer or unsigned interger how to represent it?


Thanks
Raghavendra

There is no need of declaring datatype in Perl. The datatype will be automatically defined while assigning values. e.g. you can define
Expand|Select|Wrap|Line Numbers
  1. $a=12;
  2. $b="This is a line";
  3. $c='a';
  4. $d= 12.4567;
  5.  
without the need of declaring them as int, string, char or float.
Newbie
 
Join Date: Oct 2008
Posts: 19
#3: Oct 27 '08

re: How to gvie data type to particular variable


Thanx alot,
my question is how to differentiate signed & unsgined? & how to generate random negative vlues of 32 bit integer.

Thanks
Raghavendra
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#4: Oct 27 '08

re: How to gvie data type to particular variable


Quote:

Originally Posted by raghavendrap

Thanx alot,
my question is how to differentiate signed & unsgined? & how to generate random negative vlues of 32 bit integer.

Thanks
Raghavendra

In Perl, there are only three basic datatypes- scalars, arrays and hashes. So, a variable $x can be used to assign a signed or unsigned integer or both in a script. Both signed and unsigned integers will be considered as scalar datatype. Please clarify, if your question is different.

To generate random negative values of 32 bit integer, you may use the following approach:

Expand|Select|Wrap|Line Numbers
  1. $range = 2**31; # 2^(n-1): n=32
  2. $min = -2**31; #lowest integer
  3. $val= int(rand($range)) + $min;
  4. print "$val\n";
  5.  
- Nithin
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,571
#5: Oct 27 '08

re: How to gvie data type to particular variable


Quote:

Originally Posted by nithinpes

In Perl, there are only three basic datatypes- scalars, arrays and hashes. So, a variable $x can be used to assign a signed or unsigned integer or both in a script. Both signed and unsigned integers will be considered as scalar datatype. Please clarify, if your question is different.

To generate random negative values of 32 bit integer, you may use the following approach:

Expand|Select|Wrap|Line Numbers
  1. $range = 2**31; # 2^(n-1): n=32
  2. $min = -2**31; #lowest integer
  3. $val= int(rand($range)) + $min;
  4. print "$val\n";
  5.  
- Nithin

I have to agree with nithinpes. You have to remember, Perl isn't C. Some things are just handled.
Newbie
 
Join Date: Oct 2008
Posts: 19
#6: Oct 29 '08

re: How to gvie data type to particular variable


Quote:

Originally Posted by numberwhun

I have to agree with nithinpes. You have to remember, Perl isn't C. Some things are just handled.

Yes i got it Nithin, Thank you very much.


-Raghavendra
Reply


Similar Perl bytes