in reply to Re: reading certain lines
in thread reading certain lines

sorry for not being clear.
the pattern that i want to match from the file is /[A-Z]\sd+,\sd+\(d+\)/
e.g. C 1, 1(11)

I want to store them in an array once matched.
harry

Replies are listed 'Best First'.
Re: Re: Re: reading certain lines
by chunlou (Curate) on Jun 26, 2003 at 12:27 UTC
    Do you mean something like this?
    use strict; use warnings; my $str = qq/ 1 2 3 4 ---------- C 2, 2(13) R 2, 2( 8) C 2, 2(13) ---------- C 2, 2(11) C 2, 2(13) R 2, 2(18) ---------- C 2, 2(18) C 1, 2(11) C 2, 2(13) ---------- C 1, 2(11) C 2, 2(18) C 2, 2(11) /; my @match = ($str =~ /([A-Z]\s\d+,\s\d+\(\s*\d+\))/g); print "$_\n" for @match; __END__ C 2, 2(13) R 2, 2( 8) C 2, 2(13) C 2, 2(11) C 2, 2(13) R 2, 2(18) C 2, 2(18) C 1, 2(11) C 2, 2(13) C 1, 2(11) C 2, 2(18) C 2, 2(11)
    To read file into a string you can do:
    my $str = slurp('file.txt'); sub slurp { open(F, shift); local $/ = undef; # undef record seperator my $txt = <F>; close(F); return $txt; }
      I am wanting to read the strings of interest from a file e.g.
      $in_filename = "test.out"; open (IN,"$in_filename") or die "Can't open $in_filename:$!\n";
      They are not already stored in an array so I cannot use
      @array = qw (stuff);
      as you have displayed in the start of your solution.
      but yes, the output that you've shown is exactly what I want.
      cheers
        It was $str = qq/ stuff /; a string in double quote.