Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Default to ")" if undefined 3

Status
Not open for further replies.

nfaber

Technical User
Oct 22, 2001
446
US
I've been playing with some code to assign a zero to a variable if undefined but it is not working. This is excerts from a larger program.

Code:
my ($changes,$telco,$application,$server,$other) = 0;

foreach $res_anal_code (@res_code) {
	if ($res_anal_code =~ /customer/i){$changes++;}
	elsif ($res_anal_code =~ /telco/i) {$telco++;}
	elsif ($res_anal_code =~ /power/i){$power++;}
	elsif ($res_anal_code =~ /server/i){$server++;}
	elsif 	($res_anal_code =~ /application/i) 	{$application++;}
	else {$other++;}
		
}

my @array = ("$changes","$server","$telco","$application","$other");
foreach (@array) {
	unless (defined($_)) {
		$_ = 0;
	}
}
print OUT "problemType chart:\n";
print (OUT "type, number\n"	);
print (OUT "changes,$changes\n"	);
print (OUT "server,$server\n"	);
print (OUT "power,$power\n"	);
print (OUT "telco,$telco\n"	);
print (OUT "application,$application\n"	);
print (OUT "other,$other\n\n"	);

Here's the output I am getting:

Code:
problemType chart:
type, number
changes,635
server,
power,119
telco,334
application,
other,484

Any help is appriciated.

Nick

I got a Biz Degree! How the h*ll did I get here?
 
You are anyway assigning them to 0 at the beggining of your code. I dont think you need to have the 'defined' code just to make them 0's again. But where is it defaulting to ')'?
 
')' was a misspell. I thought the same thing but the counting must be setting them to undefined.

I got a Biz Degree! How the h*ll did I get here?
 
Try to print out the variables before you wrote the 'defined' code. It should give an idea what it is actually giving out.
 
Code:
my ($changes,$telco,$application,$server,$other) = 0;

I might be losing it here, but this is setting an array = 0, n'est pas?
--Paul


cigless ...
 
Instead of

Code:
my ($changes,$telco,$application,$server,$other) = 0;

Do this

Code:
my ($changes,$telco,$application,$server,$other) = (0,0,0,0,0);
 
Oops.. at the same time.. you are right Paul, it sets only $changes to 0;
 
Yup, that was it Paul. I tried to get cute and initialize all my counters in the same line. Sometimes we should leave well enough alone.

my $changes = 0;
my $power = 0;
my $telco = 0;
my $application = 0;
my $server = 0;
my $other = 0;


I got a Biz Degree! How the h*ll did I get here?
 
Initializing variables:

$changes=$power=$telco=$application=$server=$other=0;

It works.....

dmazzini
GSM System and Telecomm Consultant

 
Note that
Code:
my ($changes,$telco,$application,$server,$other) = [b](0) x 5[/b];
would work to initialize all at once, as would the longer form
Code:
my ($changes,$telco,$application,$server,$other) = [b](0,0,0,0,0)[/b];
given by varakal.

This
Code:
my @array = ("$changes","$server","$telco","$application","$other");
foreach (@array) {
    unless (defined($_)) {
        $_ = 0;
    }
}
doesn't work because @array contains copies of the variable values, not the original variables. You are changing the copies, not the originals.

HTH

 
The @array idea would work if the array held references to the originals instead of copies, e.g.:
Code:
#!perl
use strict;
use warnings;

my ($changes,$telco,$application,$server,$other);

my @array =  [b]map {\$_}[/b] ($changes, $server, $telco, $application, $other);
foreach (@array) {
    unless (defined([b]$$_[/b])) {
        [b]$$_[/b] = 0;
    }
}

print <<EOF;
  \$changes: $changes
  \$telco: $telco
  \$application: $application
  \$server:  $server
  \$other: $other
EOF
Output:
Code:
  $changes: 0
  $telco: 0
  $application: 0
  $server:  0
  $other: 0

 
YATIMTOWTDI (Yet Another TIMTOWDTI), using the aliasing properties of the for loop:
Code:
$_ = defined $_ ? $_ : 0 for($changes,$server,$telco,$application,$other);
 
Thanks folks,

Great ideas Mike and ishnid, as usual. Mike guess you were [sleeping2] again?
 
Mike, ishnid

Would I use your code in a foreach loop? I do not see how $_ is getting defined.

 
Also, like varakal said, if I initialize my counters to zero, how could they get undefined?

Here is the code that counts:

Code:
		if ($close_month == $rpt_month) {
			
			$close_cnt++; # Tickets Closed during the current month
			print CLOSE4 "$_\n";
			#
			# Count up the Problem types at resolution
			#
			if 	($res_anal_code =~ /customer/i) 	{$changes++;}
			elsif 	($res_anal_code =~ /telco/i) 		{$telco++;}
			elsif 	($res_anal_code =~ /power/i) 		{$power++;}
			elsif 	($res_anal_code =~ /server/i) 		{$server++;}
			elsif 	($res_anal_code =~ /application/i) 	{$application++;}
			else {$other++;}
		}
 
`for' and `foreach' are exactly the same, except that `foreach' takes 4 additional character to type!

That code loops through each of the scalar variables you've listed, setting each temporarily to the special variable $_. Note that this doesn't mean that $_ holds a copy of the value of each scalar. Instead, $_ is an alias of the scalar, so any changes to $_ are also being made to the scalar. In case it isn't clear, the following code ..
Code:
$_ = defined $_ ? $_ : 0;
.. is the same as ..
Code:
if ( defined $_ ) { 
   $_ = $_; # i.e. don't change its value at all
}
else { 
   $_ = 0;
}
Hope that makes more sense.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top