in reply to help...separating strings into arrays!

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];

Replies are listed 'Best First'.
Re: Re: help...separating strings into arrays!
by cchampion (Curate) on May 18, 2002 at 10:18 UTC
    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