while (<$file>){ chomp; my @items = split m/;/, $_; if (defined($items[1])){ push @{$prices{$items[0]}, $items[1]; } else{ # add a ";" when there is a value void $prices{$items[0]} = "$prices{$items[0]}".";"; } }