Re: subroutine returning lines of text

2002-01-16 Thread Briac Pilpré

Martin A. Hansen wrote:
> im wondering how i can make a subroutine that will return all text
> lines between certain marks such as START and STOP:

Here's simple way to do it. It uses the '..' operator (see perldoc
perlop for more info about it).

Note that if START and STOP are in the same line, it will only print
this line. If you want to force START and STOP to be on different lines,
try using the '...' operator.

#!/usr/bin/perl -w
use strict;

my $start = 'START';
my $stop  = 'STOP';

while (){
print if /$start/ .. /$stop/;
}

__DATA__
text
is here
and there
START
text
is here
and there
is
a
lot of it
STOP
text
is here
and there
__END__

-- 
briac
 << dynamic .sig on strike, we apologize for the inconvenience >>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: subroutine returning lines of text

2002-01-16 Thread Gary Hawkins

> im wondering how i can make a subroutine that will return all text
> lines between certain marks such as START and STOP:
>
> text file:
>
> START
> text
> is here
> and there
> is
> a
> lot of it
> STOP
>
> so i would like to call the subroutine with the arguments START and
> STOP (because i may need more starts and stops later) and have the
> subroutine return the lines between START and STOP in a $.
>
> i guess it should be pretty simple, however its out of my reach still :P
>
> any help will be appreciated :) !
>
> martin

Hi Martin,

Once upon a time (hmmm, last July) on another list I asked a similar question.
Japhy's answer might scramble some grey matter at first, but it works and you
might find it useful:

>What's a good way to split up the contents of a file into separate arrays?
>
>## Ignore   ##
>stuff to ignore
>## Preamble ##
>Mary
>## Body ##
>had
>a
>## Epilog   ##
>little lamb

Here's a cool trick:  use the $/ variable to change how much Perl reads in
when you use .

  my (@preamble, @body, @epilog);

  {
local $/ = "\n## Preamble ##\n";
;  # skip everything up to the beginning of the preamble

$/ = "\n## Body ##\n";
chomp (my $pre_str = );  # get everything to the end of pre.
@preamble = split /\n/, $pre_str;

$/ = "\n## Epilog   ##\n";
chomp (my $bod_str = );  # get everything to the end of body
@body = split /\n/, $bod_str;

$/ = undef;  # get everything else
chomp (my $epi_str = );
@epilog = split /\n/, $epi_str;
  }

--


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]