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

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; }

Replies are listed 'Best First'.
Re: Re: Re: Re: reading certain lines
by harry34 (Sexton) on Jun 26, 2003 at 13:42 UTC
    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.
        sorry I read it wrong
        but can I automatically open the file (using code below) and read in the strings using the pattern you have provided ?
        e.g using
        $in_filename = "test.out"; open (IN,"$in_filename") or die "Can't open $in_filename:$!\n";
        I need code which will be general, I do not wish to write the stings in my program.
        thanks harry