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

.I'll paste only the part of the script that I need to parse..I want to extract the residue that do hydrophobic and hydrogen bond..

%!PS-Adobe-3.0 %%Creator: Ligplot v.4.1 %%DocumentNeededResources: font Times-Roman Symbol %%BoundingBox: (atend) %%Pages: 1 %%EndComments %%BeginProlog .... #a lot of settings of the background .... % Hydrophobic contact Hydrophname_col Zero_linewidth W G Background_col 189.17 329.15 189.17 317.71 235.30 317.71 235.30 329.15 Pl4 212.23 323.43 moveto (Trp 401(A)) Hydrophnam_size Center (Trp 401(A)) Hydrophnam_size Print % Hydrophobic contact Hydrophname_col Zero_linewidth W G Background_col 404.14 338.82 404.14 327.38 450.28 327.38 450.28 338.82 Pl4 427.21 333.10 moveto (Val 440(A)) Hydrophnam_size Center (Val 440(A)) Hydrophnam_size Print % Hydrophobic contact Hydrophname_col Zero_linewidth W G Background_col 280.94 601.38 280.94 589.94 327.08 589.94 327.08 601.38 Pl4 304.01 595.66 moveto (Gly 463(A)) Hydrophnam_size Center (Gly 463(A)) Hydrophnam_size Print .... #a lot of setting .... % Residue names gsave Ligresname_col 244.91 185.50 moveto (Atp 1) Ligresname_size Center (Atp 1) Ligresname_size Print Nligresname_col 139.01 612.49 moveto (Lys 464(A)) Nligresnam_size Center (Lys 464(A)) Nligresnam_size Print Nligresname_col 133.08 424.57 moveto (Thr 465(A)) Nligresnam_size Center (Thr 465(A)) Nligresnam_size Print Nligresname_col 448.11 410.46 moveto (Ser 466(A)) Nligresnam_size Center (Ser 466(A)) Nligresnam_size Print Nligresname_col 447.88 472.27 moveto (Gly 461(A)) Nligresnam_size Center (Gly 461(A)) Nligresnam_size Print Nligresname_col 395.22 557.76 moveto (Thr 460(A)) Nligresnam_size Center (Thr 460(A)) Nligresnam_size Print Nligresname_col 271.30 555.87 moveto ( MG 2) Nligresnam_size Center ( MG 2) Nligresnam_size Print Nligresname_col 112.85 502.47 moveto (Ala 462(A)) Nligresnam_size Center (Ala 462(A)) Nligresnam_size Print grestore .... ....

paste the correct code to help beginners like me

#!/usr/local/bin/perl use strict; use warnings; use LWP::Simple; open (DATA, "</your_path/your_file.pl") || die "impossibile apr +ire il file"; my ( @hydrophobic , @residue ); while ( my $r = <DATA> ) { if ( $r=~ m/Hydrophnam_size Center/ .. $r =~ /Hydrophnam_size/ ) { push @hydrophobic , $r; next; } if( $r=~ m/Nligresnam_size Center/ .. $r =~ /Nligresnam_size/) { push @residue , $r; } } printf "legami idrofobici: %s\n" , join '' , @hydrophobic; printf "legami idrogeno: %s\n" , join '' , @residue; __DATA__

Replies are listed 'Best First'.
Re: problems with pattern match
by linuxer (Curate) on Apr 21, 2009 at 09:29 UTC

    Hi,

    please insert the closing </code> in your post.

    additionally please put that code section into <readmore> tags. See Markup in the Monastery for details.

    Your description is unclear. So you want to parse the Postscript data and extract some information. But you didn't tell us, what you want to extract.

    Can you please add more details?

      sorry but I've done the update and in the preview it seems ok....what happend I put my code between "code" script "/code" and the the comment between "p" and "/p"
Re: problems with pattern match
by parv (Parson) on Apr 21, 2009 at 11:07 UTC

    Could you please adopt a style, any style, for your programs? See perlstyle for some guidance & search around here on the topic.

    Problem is that you seem to have data on multiple lines that you are trying to match, but you are reading & matching only one line at a time. Try this ...

      Thank you, but I tried but it doesn't work...It only print legami idrofobici: legami idrogeno: so the script runs but it doesn't do what I want...

      #!/usr/local/bin/perl use strict; use warnings; use LWP::Simple; open (IN, "</home/nerone/Desktop/lig/1xmj.pl") || die "impossibile apr +ire il file"; my ( @hydrophobic , @residue ); while ( my $r = <DATA> ) { if ( $r=~ m/Hydrophnam_size Center/ .. $r =~ /Hydrophnam_size/ ) { push @hydrophobic , $r; next; } if( $r=~ m/Nligresnam_size Center/ .. $r =~ /Nligresnam_size/) { push @residue , $r; } } printf "legami idrofobici: %s\n" , join '' , @hydrophobic; printf "legami idrogeno: %s\n" , join '' , @residue; __DATA__

      I don't know well that synthax....but it doesn't work

        [...] I tried but it doesn't work...It only print legami idrofobici: legami idrogeno: so the script runs but it doesn't do what I want.

        ... open (IN, "</home/nerone/Desktop/lig/1xmj.pl") || die "impossibile apr +ire il file"; my ( @hydrophobic , @residue ); while ( my $r = <DATA> ) { ... }

        I used DATA file handle so that portion of your own input could be appended to the program itself to form self contained example. Given your changes, the program expectedly did not print anything. Please read about open function and DATA file handle (under "Special Literals" subsection).

        You need to replace DATA file handle with IN (not the other way around) and try again.

        After seeing "SOLVED" & posted code in OP: emphasised the replacement & added parenthetical clarification for good measure. *sigh*