Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Include Delimiter in Split Output

by Saved (Beadle)
on Jul 30, 2009 at 15:50 UTC ( #784650=perlquestion: print w/replies, xml ) Need Help??
Saved has asked for the wisdom of the Perl Monks concerning the following question:

The Script...
#!/usr/bin/perl #<""> Split File undef $/; $OPTION=$ARGV[0]; $SEARCH=$ARGV[1]; $FLNAME=$ARGV[2]; $pid = open(XFILE, $FLNAME) or die "Couldn't fork: $!\n"; @finfo=split(/(\n\n)/, <XFILE>); $tRecsInfo = @finfo; @MATCHING = (); if ( $OPTION eq "with" ) { @MATCHING = grep /$SEARCH/i, @finfo; } else { @MATCHING = grep !/$SEARCH/i, @finfo; } $tMATCHING = @MATCHING; print "@MATCHING"; #print "\nI read ", scalar(@finfo), " RECORDS\n"; print "\nI read ", $tRecsInfo, " RECORDS\n"; print "I Selected ", $tMATCHING, " RECORDS\n";
The File ...
rrliupau:*:885:999:RRID - Paul Liu:/home/support/rrliupau:/usr/bin/ksh rrliupau ^rr in ID rrliupau RRID in Desc rrliupau Hard Link to Profile rrliupau Hard Link to Standard RRID Profile rrliupau Size 1078 Standard RRID Profile rrliupau RRID MultiSelect -r || exit 1 rrliupau 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrliupau/.profile rrliupau slogint=Mon Jul 27 19:33:39 2009, lockout=0000000 rrdanilh:*:892:999:RRID - Nataliya Danilchenko:/home/support/rrdanilh: +/usr/bin/ksh rrdanilh ^rr in ID rrdanilh RRID in Desc rrdanilh Hard Link to Profile rrdanilh Hard Link to Standard RRID Profile rrdanilh Size 1078 Standard RRID Profile rrdanilh RRID MultiSelect -r || exit 1 rrdanilh 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrdanilh/.profile rrdanilh slogint=Tue Jul 21 15:51:07 2009, lockout=0000000 rrtrthki:*:904:999:RRID - Kim Thanh-Tran:/home/support/rrtrthki:/usr/b +in/ksh rrtrthki ^rr in ID rrtrthki RRID in Desc rrtrthki Hard Link to Profile rrtrthki Hard Link to Standard RRID Profile rrtrthki Size 1078 Standard RRID Profile rrtrthki RRID MultiSelect -r || exit 1 rrtrthki 2974 -r-xr-xr-x 113 root support 1078 Jun 23 +2007 /home/support/rrtrthki/.profile rrtrthki slogint=Tue Jul 21 19:58:15 2009, lockout=0000000
I want the output to include the blank line. Thanx much

Replies are listed 'Best First'.
Re: Include Delimiter in Split Output
by kennethk (Abbot) on Jul 30, 2009 at 16:06 UTC
    If you'd like the output of a split to keep the delimiter in the output string, you can use look ahead or look behind assertions (Looking ahead and looking behind), depending on which side you want the delimiter to stick to. For example:

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { my @line = split/(?<=x)/; print join "\n", @line; } __DATA__ 1234x456x789

    However, this means that one of the records will be inconsistently formatted as compared to the others, which is generally something to be avoided. It usually makes a lot more sense to just append the new lines after the split (perhaps using .=) or include the formatting in your print statement.

      Indeed. If the goal, as stated in the OP, is "output to include the blank line", then you probably shouldn't put it in the element, you should put it in your output--print/say/whatever--because otherwise your last element will look different than the rest when you print them out (and any regex mucking about you do on them may behave differently on the last element).
Re: Include Delimiter in Split Output
by jwkrahn (Monsignor) on Jul 30, 2009 at 18:44 UTC

    Perhaps using paragraph mode.   Something like this:

    #!/usr/bin/perl #<""> Split File use warnings; use strict; my ( $OPTION, $SEARCH, $FLNAME ) = @ARGV; open XFILE, '<', $FLNAME or die "Couldn't open '$FLNAME': $!\n"; local $/ = ''; # Set paragraph mode while ( <XFILE> ) { if ( $OPTION eq 'with' && /$SEARCH/i ) { print; } elsif ( $OPTION ne 'with' && !/$SEARCH/i ) { print; } }
Re: Include Delimiter in Split Output
by Marshall (Abbot) on Jul 30, 2009 at 18:52 UTC
    You appear to have record oriented data and want to get a subset of info out of those records...or so it appears to me.. There is another discussion going on about the range operator (/abc/.../xyz/), see:

    This is a different approach than your splitting on \n\n. And can also process huge files as this is done line by line, record by record without requiring the whole file to be read into a single scalar by redefining the record separator.

    I am still considering some of the issues in this thread myself, but I think you will find it interesting and probably useful.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://784650]
Approved by kennethk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2017-08-23 04:36 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (345 votes). Check out past polls.