By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,177 Members | 976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,177 IT Pros & Developers. It's quick & easy.

How to gvie data type to particular variable

P: 19
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
Oct 27 '08 #1
Share this Question
Share on Google+
5 Replies


nithinpes
Expert 100+
P: 410
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.
Oct 27 '08 #2

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

Thanks
Raghavendra
Oct 27 '08 #3

nithinpes
Expert 100+
P: 410
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
Oct 27 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
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.
Oct 27 '08 #5

P: 19
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
Oct 29 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.