i have figured out how to read a file a paragraph at a time :D lol. in this example i will be reading a file a paragraph at a time and then getting only certain data back.

this will return everything AFTER the pattern up until a newline char.works very quickly for me but prob could be done faster by a seasoned coder. you can modify it to suit your needs hopefully.
tested with active perl and win7 ultimate x64
$/ = ""; #set to paragraph mode while(<$file>){ if ($_ =~ /$first_match/ && $_ =~ /$second_match/ && $_ =~ /$third +_match/){ print "$_\n"; my ($needed_data_0) = (/^data_here(.+)/, $_); my ($needed_data_1) = (/^more_data(.+)/, $_); my ($needed_data_2) = (/^other_data(.+)/, $_); print "data0 is: $needed_data_0\n"; print "data1 is: $needed_data_1\n"; print "data2 is: $needed_data_2"; } }
any input or pointers are welcome.
:)
  • Comment on read file a paragraph at a time and return paragraph OR get specific data from paragraph
  • Download Code

Replies are listed 'Best First'.
Re: read file a paragraph at a time and return paragraph OR get specific data from paragraph
by GrandFather (Saint) on Nov 03, 2014 at 00:19 UTC

    It is good practice to use local with changes to special variables (such as $/) so that other code isn't affected by unexpected changes to such variables.

    A related consideration is limiting the use of $_ to places where its use is unavoidable such as map, grep and statement modifiers.

    Perl is the programming world's equivalent of English
Re: read file a paragraph at a time and return paragraph OR get specific data from paragraph
by AnomalousMonk (Archbishop) on Nov 03, 2014 at 04:53 UTC

    Of course,you don't show any representative data, but if you're reading data in  $/ = ""; paragraph or paragrep (another search term) mode, it's likely there are one or more embedded newlines in the strings you're getting from input. Then you should be aware that the regex metacharacter/operator  ^ (carat) behaves differently according to the  /m regex modifier (and likewise $). See  ^ (and $) and  /m in perlre and perlretut.

    c:\@Work\Perl\monks>perl -wMstrict -le "my $s = qq{foo fee fie\nbar bell\nbaz biz boz}; print qq{[[$s]]}; print '--------'; ;; my ($x) = $s =~ m{ ^ foo (.+) }x; my ($y) = $s =~ m{ ^ bar (.+) }x; my ($z) = $s =~ m{ ^ baz (.+) }x; print qq{'$x' '$y' '$z'}; print '--------'; ;; ($x) = $s =~ m{ ^ foo (.+) }xm; ($y) = $s =~ m{ ^ bar (.+) }xm; ($z) = $s =~ m{ ^ baz (.+) }xm; print qq{'$x' '$y' '$z'}; " [[foo fee fie bar bell baz biz boz]] -------- Use of uninitialized value $y in concatenation (.) or string at -e lin +e 1. Use of uninitialized value $z in concatenation (.) or string at -e lin +e 1. ' fee fie' '' '' -------- ' fee fie' ' bell' ' biz boz'

Re: read file a paragraph at a time and return paragraph OR get specific data from paragraph
by GotToBTru (Prior) on Nov 03, 2014 at 00:00 UTC

    You are trying to demonstrate a simple concept with a very complicated, very specific example. This use of a changed value for $/ is covered in perlop where it is used to read in not paragraphs but the entirety of a file.

    1 Peter 4:10