http://qs321.pair.com?node_id=11100004


in reply to processing file content as string vs array

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