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

Hey! I am something that I need help with. I have a list(file) of strings that I need to search a text file for and print out the string and the lines corresponding to that string. Unfortunately every string has a different number of lines to it but they are separated by the symbol >stringname Anyone please help me. Tha Pete

Replies are listed 'Best First'.
Re: String print
by toolic (Bishop) on Mar 04, 2010 at 00:02 UTC
    • Show a small sample of your list file.
    • Show a small sample of one of your text files.
    • Show the Perl code that is causing you trouble.
    • Show the actual output (or error messages) you get.
    • Show the output you expect.

    In other words... what the heck are you talking about?

    And please use code tags for your code and data when you do post more info.

Re: String print
by rovf (Priest) on Mar 04, 2010 at 10:09 UTC
    You mean: You have a file, where the "pieces" are not separated (as usual) by a newline, but by a special separator string? In this case, setting the input record separator ($/) to this separator string could solve your problem.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: String print
by Utilitarian (Vicar) on Mar 04, 2010 at 12:57 UTC
    If I understand you correctly something based on the following should resolve your issue
    #!/usr/bin/perl -w use strict; use warnings; my $strings = ">fruit Oranges are not the only fruit Jeanette Winterson 1985 >dreams Canal dreams Iain Banks 1989 "; $strings=~s/>.+\n/|/g; $strings=substr($strings,1,); $strings="(:?$strings)"; my $data; { local $/=undef; $data=<DATA>; } my @matches = $data=~m/$strings/g; print join("****\n",@matches); __DATA__ Oranges are not the only fruit Jeanette Winterson 1985 Equal Rites Terry Pratchett 1987 Canal dreams Iain Banks 1989 Small Wonder Barbara Kingsolver 1995 American gods Neil Gaiman 2001 __END__ Oranges are not the only fruit Jeanette Winterson 1985 **** Canal dreams Iain Banks 1989

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."