in reply to from txt file to array

Into an array:

chomp (my @data = <DATA>); print "$_\n" for @data; __DATA__ one two three

Iterating over it as a file handle without saving into an array:

while (<DATA>){ chomp; print "$_\n"; } __DATA__ one two three

Replies are listed 'Best First'.
Re^2: from txt file to array
by locked_user sundialsvc4 (Abbot) on Jun 21, 2017 at 22:04 UTC

    And with saving it to an array is easy too:

    my @myarray; while (<DATA>) { chomp; push @myarray, $_; }

    (As you probably know, or that perldoc perlvar will tell you, $_ is a predefined Perl variable that implicitly refers to the most-recently entered source line and that is often used in situations like this for purposes of brevity.)

    (Personally, I prefer to write code "pedantically," like this, because it is easy to understand and to modify later.)

      And with saving it to an array is easy too:

      Ummm, first three lines of my post that you replied to:

      Into an array:
      chomp (my @data = <DATA>); print "$_\n" for @data;

      That saves the whole thing to an array without an explicit loop, *and* uses the default var $_ to show the implicit nature when looping over data...

        Stevie, I tried to add a parenthetical comment to your excellent post without taking away from it.   Maybe I didn’t succeed.   My apologies.

      If you see being "pedantic" (i.e. explicit) as something good (I sometimes do too), then you simply should avoid punctuation variables and implicit arguments altogether:
      my @myarray; while (my $item = <DATA>) { chomp $item; push @myarray, $item; }
      But here my taste would be a more functional idiom:
      my @myarray = map { chomp; $_ } <DATA>;

        More untested func to avoid empty slots:

        my @myarray = map { chomp; $_ } grep { $_ ne "" } <DATA>;

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        Furthermore I consider that Donald Trump must be impeached as soon as possible