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;
In reply to Re: Adding to array
by GrandFather
in thread Adding to array
by pglenski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |