in reply to Adding to array

foreach $DIR qw(TEST_DIR WINS_DIR) { if (defined($ENV{"$DIR"})) { $filename = $ENV{"$DIR"} . "/common/wins.ini"; if (-s $filename) { open (IN,$filename); push(@list,<IN>); chomp($list[$#list]); close(IN); } } }

Replies are listed 'Best First'.
Re^2: Adding to array
by Anno (Deacon) on Feb 28, 2007 at 20:41 UTC
    Both you and GrandFather follow the original code in calling chomp() on each partial list. Your solution only works if each file contributes only one line.

    Surely a single chomp() after the loop is simpler and more efficient.

    Anno

      In the context of file i/o the (most likely) very minor time difference between using chomp on the list for each file or once for the total list is completely irrelevant.

      As a style issue it seems sensible to have the chomp close to the place where the lines being chomped are generated so that it is clear why the chomp is there. It's a pretty minor issue however and the distance between the i/o and a single chomp would be small in this case any way.


      DWIM is Perl's answer to Gödel
        In the context of file i/o the (most likely) very minor time difference between using chomp on the list for each file or once for the total list is completely irrelevant.

        I mentioned efficieny to point out that there's no penalty. I am aware it won't matter in an IO context.

        As a style issue it seems sensible to have the chomp close to the place where the lines being chomped are generated so that it is clear why the chomp is there.

        You have a point there. It is offset by the intermediate array you (more or less have to) use, which is a distraction, not a clarification in this case.

        It's a pretty minor issue however and the distance between the i/o and a single chomp would be small in this case any way.

        Right.

        Anno