in reply to regex question: store multiple lines as a string

Just use split with a a separator /\n\n+/, and store the result in an array.

See also: perlintro, perlretut.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: regex question: store multiple lines as a string
by nurulnad (Acolyte) on Oct 12, 2010 at 09:18 UTC
    I tried:
    #!/usr/bin/perl open (data, 'data.txt') or die "die"; @words = split (/\n\n+/, <data>); print $words[0]; exit;

    and the output is only

    line=ULMNm  3  1fdy_07      N-ACETYLNEURAMINATE LYASE           user   1           3

    can you tell me what I'm doing wrong?

      split evaluates its argument in scalar context, so you only get one line. Splitting one line by two newlines doesn't make much sense.
      use strict; use warnings; use autodie; open my $f, '<', 'data.txt'; my @words = split /\n\n+/, do { local $/; <$f> }; close $f;
      Perl 6 - links to (nearly) everything that is Perl 6.