Your use of my leads me to hope that you are putting use strict; and use warnings; at the top of your scripts. It is also a good idea to indent your code (my preference is four spaces) to give a visual indication to control flow.

I'm not sure why you are using IO::File when three-argument open anf lexical filehandle will serve as well. I have used your data but have added a bogus line to demonstrate how to handle lines that don't fit your expectations. I use a regex capture to get the variable name (captured in $1) and print it out; obviously, you can assign it to a variable for later use. Once you know what a line is and have dealt with it, use next to get the next line rather than testing further. Here is the input file

.rem ***************************************************************** +***** .rem * + * .rem * deb123.rpt + * .rem ***************************************************************** +***** .rem .rem ------------------------ book_tab variables --------------------- .rem .declare book_no a14 .declare l_code a10 .burble nonsense directive here .declare l_book a12 .declare book_date a13

the code

use strict; use warnings; my $inFile = q{spw623147.txt}; open my $inFH, q{<}, $inFile or die qq{open: $inFile: $!\n}; while ( <$inFH> ) { if ( m{\A\.rem} ) { print qq{ Comment Line: $_}; next; } if ( m{\A\.declare\s+(\S+)} ) { print qq{ Data Line: $_}; print qq{ Variable is: $1\n}; next; } print qq{Not recognised: $_}; } close $inFH or die qq{close: $inFile: $!\n};

and the output

Comment Line: .rem ************************************************* +********************* Comment Line: .rem * + * Comment Line: .rem * deb123.rpt + * Comment Line: .rem ************************************************* +********************* Comment Line: .rem Comment Line: .rem ------------------------ book_tab variables ----- +---------------- Comment Line: .rem Data Line: .declare book_no a14 Variable is: book_no Data Line: .declare l_code a10 Variable is: l_code Not recognised: .burble nonsense directive here Data Line: .declare l_book a12 Variable is: l_book Data Line: .declare book_date a13 Variable is: book_date

I hope thisis of use.

Cheers,

JohnGG

Update: Fixed typo


In reply to Re^3: Extract the variable by johngg
in thread Extract the variable by denzil_cactus

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.