in reply to problem with variable scoping

The code you've posted does not display the error to describe. If I run the following (lightly modified)

#!/usr/bin/perl use strict; use warnings; &populatemessagedefs(undef, '1,2,3,4,5,6'); sub populatemessagedefs() { my ($ref_to_msg_defs,$Everythingelse) = @_; my $temp_value; my $temp_key; my @Interface_Message_List = split (',',$Everythingelse); my $i=0; my %ref_to_msg_defs; foreach my $field(@Interface_Message_List) { print "$i\n"; #In next iteration $i is ‘0’ again if ($i == 0) { $temp_key = $field; $i++; #Value of $i is 1 now } elsif ($i == 1) { $temp_value = $field; if (exists $ref_to_msg_defs->{$temp_key}) { if ($field > $ref_to_msg_defs->{$temp_key}) { $ref_to_msg_defs{$temp_key} = $field; $i--; } } else { $ref_to_msg_defs{$temp_key} = $field; $i--; } } } }

I get

0 1 0 1 0 1

which I'd expect. Two items of note for you:

  1. You are using Prototypes incorrectly by declaring your subroutine with sub populatemessagedefs(). You expect two scalar arguments, not zero as you've written there. Unless you know what prototypes are and what they are not, you should really replace that line with sub populatemessagedefs.
  2. You never use $ref_to_msg_defs, which I assume is a hash reference. Rather, in your code you use $ref_to_msg_defs{$temp_key}, which accesses the hash %ref_to_msg_defs, which is not declared in your code. In order to run under strict, I had to declare the appropriate local hash.