#!/usr/bin/perl -w use strict; die "Usage: save-into-arrays.pl input-file.txt\n" unless @ARGV == 1; my @description; # where we'll store contents of Description section my @data; # where we'll store contents of Data section my %sections = ('Description:' => \@description, 'Data:' => \@data); open(INPUT_FILE, "<$ARGV[0]") or die "Unable to open $ARGV[0]: $!\n"; my $array_ref; # keeps reference to array currently in use while (my $line = ) { chomp $line; if (defined($sections{$line})) { # is this is a section head? $array_ref = $sections{$line}; # yes, so point to the array for this section } else { push @$array_ref, $line if $array_ref; # no, save this line in the active array } } close(INPUT_FILE);