Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: reading certain lines

by helgi (Hermit)
on Jun 26, 2003 at 11:48 UTC ( #269196=note: print w/ replies, xml ) Need Help??


in reply to reading certain lines

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


Comment on Re: reading certain lines
Select or Download Code
Re: Re: reading certain lines
by harry34 (Sexton) on Jun 26, 2003 at 11:55 UTC
    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; }
        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://269196]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-07-13 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls