my $current_mirna; while(<>){ chomp; $current_mirna = $1 if /^(miRNA\w+) - results/; die unless defined $current_mirna; # now you always have the miRNA ID of the current block # conitnue here to parse the actual data and insert into the # database for that specific miRNA }