harry34 has asked for the wisdom of the Perl Monks concerning the following question:

if I have a file conatining the following:

1555009 1555001 1555008 1555021 1465010 1465006 1465005 1465004 1465003 1465002 1465001 1465008 1465022 Ring 16 Size 11 1555001 1555001 1555001 1465001 1465001 1465001 1465001 1465001 1465001 1465001 1465001 .. H22 -- N8 -- H21 .. O10 == C6 -- C5 -- C4 -- C3 -- C2 -- C1 == O9 1555022 1555008 1555021 1465010 1465006 1465005 1465004
1465003 1465002
1465001 1465009
Initial graph set matrix : period 1 patterns
1 2 3 4
----------
C 1, 1( 9) C 1, 1( 4) C 1, 1( 9) C 1, 1( 4)
----------
Initial graph set matrix : period 2 patterns
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)

how can I tell perl to just read the following pattern (can have any letters or numbers) /C1, 2(11)
do i need to use grep ?
cheers harry

Replies are listed 'Best First'.
Re: reading certain lines
by davorg (Chancellor) on Jun 26, 2003 at 11:11 UTC

    I think you might want to try to rephrase your question. You want to "read" a certain pattern, but then tell us that it can contain "any letters or numbers".

    How you we recognise the data that you are interested in? And what do you want to do with it?

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: reading certain lines
by helgi (Hermit) on Jun 26, 2003 at 11:48 UTC
    I don't get it.

    You don't have any lines with '/C1, 2(11)'.

    Do you mean 'C1, 2(11)'?

    my $pattern = 'C1, 2(11)'; while (<DATA>) { next if not /$pattern/; # do something with the line if it has $pattern }

    Or does the line have to start with 'C1, 2(11)'?

    next if not /^$pattern/;


    --
    Regards,
    Helgi Briem
    helgi DOT briem AT decode DOT is

      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
        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; }
Re: reading certain lines
by Jasper (Chaplain) on Jun 26, 2003 at 12:30 UTC
    push @array, /\w+\s\d+,\s\d+\(\d+\)/g while <FILE>
    ?
    Jasper
    oops, forgot spaces