in reply to Re: from txt file to array
in thread from txt file to array

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.)

Replies are listed 'Best First'.
Re^3: from txt file to array
by stevieb (Canon) on Jun 21, 2017 at 23:48 UTC
    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.

        Your style of overwhelming with biblical-like nonsense all over the place is known well around here, so to me, it appeared as though you bit off of a post without reading it thoroughly, then repeated what it contained in a different way.

        You don't need to apologize to me; I don't care. To be honest, after I responded, I realized that it was one of the very first times that I had seen you post actual Perl code, and felt that perhaps that was the start of something nice.

        No offense taken. Keep posting actual code.

Re^3: from txt file to array
by morgon (Priest) on Jun 21, 2017 at 23:11 UTC
    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