my %courses; my $mnemonic; # this is the correct spelling :) while () { # let's use $_, shall we? next unless ( /\S/ ); # skip blank lines; my @fields; my ($id, $rest) = unpack("xA5xA*", $_); # break line into 2 pieces if ( $id =~ /^\d{5}$/ ) { # it's the start of a record ($mnemonic,@fields = unpack("A4xA4xA4xA2xA2xA28A*", $rest); # work out what to do with @fields; $mnemonic will retain # it's current value till the next one is encountered, # so sub-records after this one will be added to the # correct hash element. } elsif ( $rest =~ /^\d+-\d+/ ) { # it's a sub-record my ($time,$days,$bldg,$room,$end) = unpack("A9xA6xA4xA4xA*", $rest); # you need to work out what to do with $end, # and push stuff into the current $courses{$mnemonic} structure } else { # do something else with (or ignore) other stuff } }