in reply to Adding to array
Read the new lines into a new list then push the new lines onto the end of the master list:
use strict; use warnings; my @list; foreach my $DIR qw(TEST_DIR WINS_DIR) { if (defined($ENV{"$DIR"})) { my $filename = $ENV{"$DIR"} . "/common/wins.ini"; if (-s $filename) { open (IN,$filename); my @newList = <IN>; close (IN); chomp @newList; push @list, @newList; } } }
Note too the use of strictures: use strict; use warnings;. All your code should use strictures!
Update: You may find that using next cleans up the code by reducing nesting thus making it easier to see the important program flow:
use strict; use warnings; my @list; foreach my $DIR qw(TEST_DIR WINS_DIR) { next unless defined($ENV{"$DIR"}); my $filename = $ENV{"$DIR"} . "/common/wins.ini"; next unless -s $filename; open (IN,$filename); my @newList = <IN>; close (IN); chomp @newList; push @list, @newList; }
Oh, and you should use the three parameter open too. ;)
open IN, '<', $filename;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding to array
by pglenski (Beadle) on Feb 28, 2007 at 20:55 UTC | |
by GrandFather (Saint) on Feb 28, 2007 at 21:05 UTC |