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

How to display first line of each record using the question code

by SatisfyMyStruggles (Initiate)
on May 18, 2013 at 17:12 UTC ( #1034111=perlquestion: print w/ replies, xml ) Need Help??
SatisfyMyStruggles has asked for the wisdom of the Perl Monks concerning the following question:

Give the code data file content of the data records, I'm unable to print the first line of each record. However, it does print the first line of the record as in: 1aaaaaaaaaaaaa Note: I'm sure there are many ways to do this using the split function, but in my case, I need a solution that follows the code question.... I think the problem is that after chomp the input record separator of __Data__, the first record gets process correctly but the first line of the other records is a blank line, and therefor, the output shows a blink line as the first line. I try using a input record separator of __Data__\n and did not chomp

Data file content of records 1aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa __Data__ 1bbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbb __Data__ 1cccccccccc ccccccccccc ccccccccccc __Data__
{ local $/="__Data__"; open my $fh,"<","logA.txt" or die "Unable to open file"; while(<$fh>) { # remove the input record separator value of __Data__ chomp; # display the line of each record if(/([^\n]*)\n(.*)/sm) { print "$1\n"; } } close ($fh); }

Comment on How to display first line of each record using the question code
Select or Download Code
Re: How to display first line of each record using the question code
by LanX (Canon) on May 18, 2013 at 17:28 UTC
    TIMTOWTDI ... but this works for me:

    use strict; use warnings; use feature 'say'; local $/="__data__\n"; while (my $record=<DATA>){ # say "<$record>"; my @lines = split /\n/, $record; say $lines[0]; } __DATA__ 1aaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa __data__ 1bbbbbbbbbb bbbbbbbbbbb bbbbbbbbbbb __data__ 1cccccccccc ccccccccccc ccccccccccc __data__

    output

    1aaaaaaaaaa 1bbbbbbbbbb 1cccccccccc

    > I need a solution that follows the code question...

    what does this mean?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      My problem is I can't get $/="__Data__\n" to work using my code

        > I can't get $/="__Data__\n" to work

        My code works, so yours must be wrong! :)

        Try to trek down the problem by replacing the "code in question" bit by bit.

        Did you use double-quotes?

        Does "\n" really immediately follow "__Data__" in your data?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: How to display first line of each record using the question code
by Laurent_R (Vicar) on May 18, 2013 at 18:49 UTC
    Hi,

    I just tried Rolf's code, and it did not seem to work properly for me, although it looks correct.

    Then I realized that copying and pasting from Windows to Cygwin was probably the problem. So I changed Rolf's code as follows:

    local $/="__data__\r\n";

    And then it works and does what is expected

    Maybe it is all what you need to do.

      Prior to starting this thread, the OP had posted multiple questions on stackoverflow for this exact issue and was given multiple working alternatives. It's unclear to my why this tread was started.

      It appears that each of the solutions broke the OP's cardinal rule by not using his broken code.

        > It's unclear to my why this tread was started.

        His style reminds me of a troll like regular here who constantly shifts identities and denies collaboration.

        Beware of your hand while feeding! =)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034111]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-07-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (158 votes), past polls