in reply to SOLVED:problems with pattern match

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 ...

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 Print/) { push @residue , $r; } } printf "legami idrofobici: %s\n" , join '' , @hydrophobic; printf "legami idrogeno: %s\n" , join '' , @residue; __DATA__ ... % 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 ... % 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 ...

Replies are listed 'Best First'.
Re^2: problems with pattern match
by paola82 (Sexton) on Apr 21, 2009 at 11:49 UTC

    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*