Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Debugging help!

by aitap (Curate)
on Aug 31, 2012 at 14:01 UTC ( [id://991011]=note: print w/replies, xml ) Need Help??


in reply to Debugging help!

Variables $1, $2, etc. are locally-scoped and they can be invalid after pattern matching. Try rewriting your matches like this:

$1 => $2 if /(.+)\s+\|\s+(PF.{5})/
so usage of these variables should be inside the if(){} block, not near the match.

If you are not sure whether your regexps are right, use Regexp::Debugger or YAPE::Regex::Explain to check them.

Sorry if my advice was wrong.

Replies are listed 'Best First'.
Re^2: Debugging help!
by invaderzard (Acolyte) on Aug 31, 2012 at 14:14 UTC
    So I've rewritten my code and it looks like this:
    #!/usr/bin/perl use Modern::Perl; use File::Slurp qw/read_file write_file/; my $uniprot = 'uniprot-sfinal.txt'; my $activin = 'Activator-PFAM.txt'; my $antioxin = 'AntiOxidant-PFAM.txt'; my $toxinin= 'Toxin-PFAM.txt'; my $activout = 'ActivACNPF.txt'; my $antioxout= 'AntioxACNPF.txt'; my $toxinout= 'ToxinACNPF.txt'; my @activline; my @antioxline; my @toxinline; my %activ = map { s/\.\d+//g; $1=>$2 if/(.+)\s+\|\s+(.+)/; } read_file + $activin; my %antiox = map { s/\.\d+//g; $1=>$2 if/(.+)\s+\|\s+(.+)/; } read_fil +e $antioxin; my %toxin = map { s/\.\d+//g; $1=>$2 if/(.+)\s+\|\s+(.+)/; } read_file + $toxinin; for ( read_file $uniprot ) { /(.{6})\s+.+=([^\s]+)/; push @activline, "$1 | $2 | $activ{$1}\n" if $activ{$1}; push @antioxline, "$1 | $2 | $antiox{$1}\n" if $antiox{$1}; push @toxinline, "$1 | $2 | $toxin{$1}\n" if $toxin{$1}; } write_file $activout, @activline; write_file $antioxout, @antioxline; write_file $toxinout, @toxinline;
    The error codes changed. Now they say this.
    Odd number of elements in hash assignment at ARP//positivedatasetextra +ctor.pl line 18. Odd number of elements in hash assignment at ARP//positivedatasetextra +ctor.pl line 20. Odd number of elements in hash assignment at ARP//positivedatasetextra +ctor.pl line 23. Odd number of elements in hash assignment at ARP//positivedatasetextra +ctor.pl line 24. Odd number of elements in hash assignment at ARP//positivedatasetextra +ctor.pl line 25.

    The lines with the errors are still the same.

      { s/\.\d+//g; $1=>$2 if/(.+)\s+\|\s+(.+)/; }
      This will supply 2 elements to properly populate the hash if the match succeeds.

      However, on match failure, the return value is undef() (I think), but the important thing is that a single (ODD NUMBER) value is returned, so the hash is not happy.

      Update:
      I would correct it as :

      { s/\.\d+//g; /(.+)\s+\|\s+(.+)/ ? ($1=>$2) : () }

                   I hope life isn't a big joke, because I don't get it.
                         -SNL

      Do your files have empty lines which don't match your regexps? Try returning empty list (()) in case the regexp didn't match (map will omit the element in this case):
      $ perl -w -MData::Dumper -E'%h = map {if(/(.+)\s+\|\s+(.+)/){$1=>$2}el +se{()}} (<>); print Dumper \%h' Q6GZX4 | PF04947.9 Q96355 | PF01486.12 PF00319.13 Q96356 | PF01486.12 PF00319.13 Q39371 | PF01486.12 PF00319.13 DSFSDF ASD SSSSSSSSSS ^D $VAR1 = { 'Q96355' => 'PF01486.12 PF00319.13', 'Q96356' => 'PF01486.12 PF00319.13', 'Q6GZX4' => 'PF04947.9', 'Q39371' => 'PF01486.12 PF00319.13' };
      Edit: return an empty list
      Sorry if my advice was wrong.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-03-19 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found