Superman has asked for the wisdom of the Perl Monks concerning the following question:

i have a flat file with the following data in lines...
ABC header line ABC line 1 ABC line 2 ABC line 3 ABC header line ABC line 1 ABC line 2 ABC line 3
how can i write this data do that each time i get to a new header line i push the lines following it into a separate array so that array1 contains....
ABC header line ABC line 1 ABC line 2 ABC line 3
and array2 contains....
ABC header line ABC line 1 ABC line 2 ABC line 3
thanks 4 the wisdom!

2002-05-18 Edit by Corion : Added CODE tags

Replies are listed 'Best First'.
Re: help...separating strings into arrays!
by Superman (Acolyte) on May 17, 2002 at 13:33 UTC
    just to refinethe above query...
    this is what i have

    ABC header line A
    ABC line 1
    ABC line 2
    ABC line 3
    ABC header line B
    ABC line 1
    ABC line 2
    ABC line 3

    and thisa is what i need using the A and B to define the array
    @A= ABC header line A
    ABC line 1
    ABC line 2
    ABC line 3
    @B=ABC header line A
    ABC line 1
    ABC line 2
    ABC line 3
Re: help...separating strings into arrays!
by munchie (Monk) on May 17, 2002 at 13:30 UTC
    I'm not quite sure if I understand correctly, but here goes.

    use strict; my $file; open FILE, "<test.txt"; { local $/ = undef; $file = <FILE>; } close FILE; my @ab = split /ABC header line/, $file; my @array1 = split /\n/, @ab[0]; my @array2 = split /\n/, @ab[1];
      Your script will store the contents of the file in memory twice.
      This one will create the necessary arrays and fill them directly while reading the file.
      #!/usr/bin/perl -w use strict; # create three empty array references my %arrays = map {$_, []} qw ( A B C ); my $ar_ref = undef; while (<DATA>) { if (/ABC header line (\w)/) { die "unrecognized header <$1>\n" unless exists $arrays{$1}; $ar_ref = $arrays{$1}; } else { die "no current array\n" unless $ar_ref; chomp; push @$ar_ref, $_; } } for (keys %arrays) { print "$_ => ( @{$arrays{$_}} )\n"; } __DATA__ ABC header line A A line 1 A line 2 A line 3 A line 4 ABC header line B B line 1 B line 2 B line 3 ABC header line C C line 1 C line 2 C line 3 C line 4
      And this is the result:
      A => ( A line 1 A line 2 A line 3 A line 4 ) B => ( B line 1 B line 2 B line 3 ) C => ( C line 1 C line 2 C line 3 C line 4 )
      HTH

      cchampion
Re: help...separating strings into arrays!
by munchie (Monk) on May 17, 2002 at 13:50 UTC
    Thank you. My code will work with your given specification.