while (<>) { $l = $_; chomp $; # you probably want to chomp $l, or possibly $_ (but you no longer use $_), but not $ @vals = split /;/, $l; # you split your line into @vals, but no longer use that variable. Besides, # declaring @vals with my would be good practice if ($l =~ /Query/) { # you could use something like: if $vals[0] eq "Query" %pairs{$l[1]}{$l[2]} = $l[3]; # where are $l[1], $l[2] and $l[3] coming from? Also, %pairs{...} is probably a syntax error. } elsif {$l =~ /Answer/) { # again, you could use: if $vals[0] eq "Answer". Also, "elsif {..." is a syntax error. %pairs{$l[1}{$l[2]} = $l[3]; # again, where are $l[1], $l[2] and $l[3] coming from? Also a syntax error. $json = encode_json $pairs{$l[1]}; # given the previous code, I doubt that you really want to encode $pairs{$l[1]} print $json."\n"; # is you intent to print to the screen? delete $pairs{$l[1]}; # not sure it's needed, since you just reuse the same variable in the next iteration } }