Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: How to extract certain range of text and display it?

by CountZero (Bishop)
on Oct 13, 2013 at 12:25 UTC ( #1058069=note: print w/replies, xml ) Need Help??

in reply to How to extract certain range of text and write into another file?

Tell Perl that your records are terminated by ".EON\n\n".
use Modern::Perl; { local $/ = ".EON\n\n"; while ( my $record = <DATA> ) { say "I found: $record"; # ... now do something with $record ... } } __DATA__ $ NAME : corry $$.Inc s d $$.Oc s $$.TO G1 ty n1 EE EE M T1 T2 $$SRU G2 n1 y OO OO M T3 T4 $$SRU .EON $ NAME : patrick $$.Inc c d $$.Oc c $$.TO G1 td n3 EE EE M T5 T6 $$SRU G2 n3 y OO OO M T7 T8 $$SRU .EON


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: How to extract certain range of text and display it?
by annel (Novice) on Oct 14, 2013 at 01:43 UTC
    Hi, thanks for your response. I did it as following but nothing display of the output.
    use warnings; use strict; my $test; my @words; open(INFO,"<","")||die"Can't open file:$!\n"; chomp (@words=<INFO>); close(INFO); local $/ = ".EOM\n\n"; while ( my $record = @words) { print $record; }
    Where did I make mistake?
      CountZero's advice is more than just "setting $/". You have to
      1. define $/ before reading your file
      2. of course, spell your record separator correctly, as noted by jakeease
      3. change your logic accordingly, because a "record" now contains multiple lines (those belonging to one name)
        Thanks for your response. I have updated my code and question. Can you give me some idea where to define $/?

      CountZero's suggestion is

      local $/ = ".EON\n\n";

      you simply have a typo:".EOM...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1058069]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-06-25 10:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.