#!/usr/bin/perl -w use strict; use Data::Dumper; my $currec = undef; my @recs =(); while () { if ( /^Record\s+(\d+)/) { $currec = $1 } elsif ( /^ (\S+) # non spaces. This is the key name \s+ # some spaces follows : # and a colon \s+ # more spaces [^\[]+ # any non bracket character \[ # an opening bracket ( [^\]]* # any more characters before a closing bracket ) \] # a closing bracket /x) { $recs[$currec]{$1} = $2; } } print Data::Dumper->Dump([ \@recs ], ['original']); my @twelves = grep { $recs[$_]->{Ref} == 12 } 0.. $#recs; print Data::Dumper->Dump( [ \@{[ @recs[@twelves] ]} ], ['twelves']); __DATA__ ================================================= Record 0 Name : John [John] I/C : >123< [123] Sex : 'Male' [Male] Ref : >12< [12] ================================================== Record 1 Name : Pretty [Pretty] I/C : >125< [125] Location : 'Female' [Female] Ref : >12< [12] ================================================== Record 2 Name : Kim [Kim] I/C : >124< [124] Location : 'Male' [Male] Ref : >11< [11] ================================================== #### $original = [ { 'I/C' => '123', 'Ref' => '12', 'Sex' => 'Male', 'Name' => 'John' }, { 'I/C' => '125', 'Ref' => '12', 'Location' => 'Female', 'Name' => 'Pretty' }, { 'I/C' => '124', 'Ref' => '11', 'Location' => 'Male', 'Name' => 'Kim' } ]; $twelves = [ { 'I/C' => '123', 'Ref' => 12, 'Sex' => 'Male', 'Name' => 'John' }, { 'I/C' => '125', 'Ref' => 12, 'Location' => 'Female', 'Name' => 'Pretty' } ];