in reply to the variable is not getting assigned

Add use warnings; use strict; to the start of your code. After you have resolved the errors that that generates, and if you still have a problem, ask the question again with the modified code.

My guess is that $tier and $code1 are going out of scope so when you get to the assignment new variables are created and have undefined values.

Note that in addition to the scoping problem if($code1 = 0) should probably be if($code1 == 0). For purposes of testing a value for @contents would be nice. _$tier_$code has to be written _${tier}_$code or Perl will think the variable is %tier_.

use strict; use warnings; my $temp; my $code; my $info; my $count = 0; my @contents = ('WEB'); my $length = scalar@contents; for(my $i=0;$i<=$length;$i++) { my $code1; my $tier; my $severity; chomp($contents[$i]); if($contents[$i] =~ /\w+=(\d+)/){ $code = $1; $code1 = $code; chomp($contents[$i+1]); $info = $contents[$i+1]; substr($info,0,5) = ""; $temp = substr($code ,0,1); #print "$temp --> code\n"; if($temp == 1) { $tier = "WEB"; } if($temp == 2) { $tier = "APP"; } if($temp == 3) { $tier = "DB"; } if($code1 != 0) { $severity = 1; } if($code1 == 0) { $severity = 5; } #print "$code1 --> $tier\n"; my $message = "<event id=\"ALERT_${tier}_$code1\" sour +ce=\"APP\">"; print "$message --messgae\n"; } }

Updated to provide sample code


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: the variable is not getting assigned
by blazar (Canon) on Jan 11, 2006 at 09:50 UTC
    Note that in addition to the scoping problem if($code1 = 0) should probably be if($code1 == 0).

    Curiously enough this node (in an interesting thread) about mistyping = for == in if clauses was cited a few hours before.