CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to set variables to 1.

 



cuboidgraphix
User

Feb 23, 2012, 1:37 PM

Post #1 of 5 (877 views)
How to set variables to 1. Can't Post

Hi guys,
Is there a simpler way to set variables to 1? Rather than having something like this...


my $a = 1;
my $b = 1;
my $c = 1;
my $d = 1;
my $e = 1;
my $f = 1;
my $g = 1;
my $h = 1;
my $i = 1;

Looking for a simpler method of setting 1 to all those variables. Thanks for any help.


BillKSmith
Veteran

Feb 23, 2012, 8:04 PM

Post #2 of 5 (862 views)
Re: [cuboidgraphix] How to set variables to 1. [In reply to] Can't Post


Code
my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = (1)x9;



You probably should not be using this many variables. Consider a hash.



Code
my %hash = ( 
a => 1,
b => 1,
c => 1,
d => 1,
e => 1,
f => 1,
g => 1,
h => 1,
i => 1,
);

Good Luck,
Bill


naven8
Novice

Feb 24, 2012, 6:33 AM

Post #3 of 5 (856 views)
Re: [BillKSmith] How to set variables to 1. [In reply to] Can't Post

Is there any way to change default value of variable?

Thanks


rovf
Veteran

Feb 24, 2012, 7:45 AM

Post #4 of 5 (854 views)
Re: [naven8] How to set variables to 1. [In reply to] Can't Post

Fortunately not! This would break immediately all library code....


BillKSmith
Veteran

Feb 24, 2012, 11:34 AM

Post #5 of 5 (850 views)
Re: [naven8] How to set variables to 1. [In reply to] Can't Post

When a variable is declared, it is assigned a "value" of undef. When that variable is defined, it is assigned the specified value. You would not want to change this behavior.

When we define a variable, we can specify a default value to use if a value is not avaiable from the usual source. (e.g. Specify an input file name to use if the user does not specify one on the command line.)


Code
 my $file_name = $ARGV[0] || 'default_input.txt';



Refer to the chapter "Establishing a Default Value" in the book "Perl Cookbook" for a detailed discussion of this topic.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives