Here is one possible solution.

#!/usr/bin/perl -w use strict; use Data::Dumper; my $currec = undef; my @recs =(); while (<DATA>) { 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] ==================================================

And the output:

$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' } ];

This should be more than enough for a start.


In reply to Re: Print selected records only? by rnahi
in thread Print selected records only? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.