Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I see that you are happy with the flip-flop operator as demo'ed by haukex. The flip-flop operator in Perl keeps the state of whether or not you are within the beginning or closing statements of some data record. I like that operator, but it may not be the best in all situations.

In a language without the flip-flop operator, another method is to call a subroutine when the beginning of record is seen. Use that subroutine to process the record. This handles the "state information" of whether or not you are inside the record without having to have a flag value. Of course adjustments are necessary depending upon whether the first or last values of the record need to be included or not.

Here is some possible code:

use warnings; use strict; while (<DATA>) { process_record ($_) if /\@user_info_start/; } sub process_record #include first and last line { my $start_of_record = shift; my @userinfo; push @userinfo, $start_of_record; my $line; do { $line =<DATA>; push @userinfo, $line; } until $line =~ /\@user_info_end/; print @userinfo; print "\n"; } =Prints xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end =cut __DATA__ xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end d987sd66bd xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end somebus here or blank lines? whatever xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Or perhaps.
use warnings; use strict; while (<DATA>) { process_record() if /\@user_info_start/; } sub process_record { my $line; print $line while (defined ($line =<DATA>) and $line !~ /\@user_info +_end/); print "\n"; } =Prints xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx =cut __DATA__ xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end d987sd66bd xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end somebus here or blank lines? whatever xxxxxxxxxxxxxxxxxxxxxxxxxxxx
Update:

of course the first example could avoid a push...In general, Don't "push" when you can "print"!

use warnings; use strict; while (<DATA>) { process_record ($_) if /\@user_info_start/; } sub process_record #include first and last line { my $start_of_record = shift; print $start_of_record; my $line; do { $line =<DATA>; print $line; } until $line =~ /\@user_info_end/; print "\n"; } =Prints xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end =cut __DATA__ xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end d987sd66bd xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end somebus here or blank lines? whatever xxxxxxxxxxxxxxxxxxxxxxxxxxxx
I guess yet another way...
use warnings; use strict; while (<DATA>) { process_record ($_) if /\@user_info_start/; } sub process_record # include first and last line { my $start_of_record = shift; print $start_of_record; my $line; # a bit of strangeness caused by using <DATA> handle while (defined ($line=<DATA>) and $line !~ /\@user_info_end/) { print $line; } print $line; #the last line of record print "\n"; } =Prints xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end =cut __DATA__ xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : xxxxx xxxx*@Filetype : txt xxxx*@Version : 0001 xxxx*@Create_Date : 20190407 xxxx*@Product : xxxx xxxx*@user_info_end d987sd66bd xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxx xxxx*@user_info_start xxxx*@Title : Mr xxxx*@Username : yy xxxx*@Filetype : txt xxxx*@Version : 0005 xxxx*@Create_Date : 43 xxxx*@Product : xxxx xxxx*@user_info_end somebus here or blank lines? whatever xxxxxxxxxxxxxxxxxxxxxxxxxxxx

In reply to Re: processing file content as string vs array by Marshall
in thread processing file content as string vs array by vinoth.ree

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found