in reply to use of diamond operator for multiple files

You are asking for the shortest amount of code needed to gather into two arrays from two files all lines that don't start with a > character, and that do end in a newline character? So if the last line in the file doesn't have a newline character at the end, should it be ignored?

Does the code you posted do what you want in the first place? Whether optimizing for computational time, code length, memory, or aesthetic beauty, first the problem must be well defined, second the algorithm must work, third, the goal of the optimization must be known. None of those criteria have been made clear to us.


Dave

Replies are listed 'Best First'.
Re^2: use of diamond operator for multiple files
by aeqr (Novice) on Apr 23, 2014 at 18:20 UTC
    Hey, thanks for the answer.

    Yes the code works, but I wanted to see other solutions. I would say it is to optimize code length.

    Yes I am asking exactly this: The shortest amount of code needed to gather into two arrays from two files all lines that don't start with a > character, and that do end in a newline character.

    Thank you!

      This isn't what I would consider a golfy solution, but it is more brief:

      my @data; for my $file (qw(this.txt that.txt)) { open my $fh, '<', $file or die $!; push @data, []; while( <$fh> ) { push @{$data[-1]}, $_ if /\n\z/ && ! /^>/; } }

      If you want that in the form of a subroutine:

      @data = read_files( qw(this.txt that.txt) ); sub read_files { my @data; for my $file ( @_ ) { open my $fh, '<', $file or die $!; push @data, []; while( <$fh> ) { push @{$data[-1]}, $_ if /\n\z/ && ! /^>/; } return @data; }

      (Untested)


      Dave