Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Help with pushing into a hash

by jemswira (Novice)
on Aug 31, 2012 at 11:48 UTC ( #990987=note: print w/replies, xml ) Need Help??


in reply to Re^3: Help with pushing into a hash
in thread Help with pushing into a hash

Thank again Kenosis (bows)

Well I used the code you gave me and tweaked it abit so it could do multiple files at one time so I didnt have to load the hash everytime I wanted to do multiple files. But it returns the errors:

Use of uninitialized value in list assignment at C:\Users\Jems\Desktop +\Perl\test\test2script.plx line 20. Use of uninitialized value in list assignment at C:\Users\Jems\Desktop +\Perl\test\test2script.plx line 22. Use of uninitialized value in list assignment at C:\Users\Jems\Desktop +\Perl\test\test2script.plx line 26. Use of uninitialized value in list assignment at C:\Users\Jems\Desktop +\Perl\test\test2script.plx line 27. Use of uninitialized value in list assignment at C:\Users\Jems\Desktop +\Perl\test\test2script.plx line 28.

Also, the when I run it in Padre, it gives the popup message

line 39: Substitute(s///) doesnt return the changed value even if map.  Continue? Y/N.

What is wrong with my code?

#!/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; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $activin; my %antiox = map { s/\.\d+//g; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $antioxin; my %toxin = map { s/\.\d+//g; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $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 input format is still the same as before, but just more input.

Replies are listed 'Best First'.
Re^5: Help with pushing into a hash
by Kenosis (Priest) on Aug 31, 2012 at 15:16 UTC

    You're most welcome, jemswira! (And, if you need to bow, bow only to Perl... :)

    The errors suggest a failed regex in one or more of the map statements. Here are the lines where the errors occurred:

    my %activ = map { s/\.\d+//g; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $activin; # Line 20 my %antiox = map { s/\.\d+//g; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $antioxin; # Line 21 my %toxin = map { s/\.\d+//g; /(.+)\s+\|\s+(.+)/; $1 => $2 } read_fil +e $toxinin; # Line 22 for ( read_file $uniprot ) { # Line 23 /(.{6})\s+.+=([^\s]+)/; # Line 24 push @activline, "$1 | $2 | $activ{$1}\n" if $activ{$1}; # Line 2 +6 push @antioxline, "$1 | $2 | $antiox{$1}\n" if $antiox{$1}; # Lin +e 27 push @toxinline, "$1 | $2 | $toxin{$1}\n" if $toxin{$1}; # Line 2 +8 }

    Sounds like there may be lines in the files with differently-formatted data that the regex fails to match. To see if this is the case, try the following:

    for my $file (qw/Activator-PFAM.txt AntiOxidant-PFAM.txt Toxin-PFAM.tx +t/){ for(read_file $file){ say "No Match in File: $file; Line: $_" if !/(.+)\s+\|\s+(.+) +/; } }

    This will go through each file and display any line the regex doesn't match. If lines with data on them show, the regex will need to be adjusted. If empty lines show, e.g.,:

    No Match in File: test2.txt; Line:

    Try adding a grep before the file read that allows only non-blank lines to pass. For example:

    my %data = map {s/\.\d+//g; /(.+)\s+\|\s+(.+)/ and $1 => $2 } grep /\S +/, read_file $test2;

    If no lines show, I'm not sure what the issue may be. In any case, however, please get back to me or the Monks...

      Thanks again! I ran the check and had some blank lines:

      No Match in File: Activator-PFAM.txt; Line: Q8UPQ0 | No Match in File: Toxin-PFAM.txt; Line: Q306M5 |

      So i added the grep before the file read, and the error changed.

      Odd number of elements in hash assignment at C:\Users\Jems\Desktop\Per +l\test\test2script.plx line 20. Odd number of elements in hash assignment at C:\Users\Jems\Desktop\Per +l\test\test2script.plx line 22. Odd number of elements in hash assignment at C:\Users\Jems\Desktop\Per +l\test\test2script.plx line 26. Odd number of elements in hash assignment at C:\Users\Jems\Desktop\Per +l\test\test2script.plx line 27. Odd number of elements in hash assignment at C:\Users\Jems\Desktop\Per +l\test\test2script.plx line 28.

      Still same lines. And somehow line 21, which was for the file that did not give the error still did not give the error. Also Padre is warning me that the substitution will not replace anything.

      thanks!

        Ah! Excellent, jemswira!! Here's what's happening... The regex trys to capture everything past " | " for use as a value. However, in the lines above, there are no values, so $2 is not initialized for those lines which creates errors. To fix this, change the grep to:

        grep /\|\s+\S+/

        You'll notice that the first part of the grep's regex is used in the map statement. The above adds \S+, which means that there need to be non-whitespace characters (i.e., values) beyond " | " for the line to pass. I'm assuming, here, that we can safely ignore lines that don't have anything past " | ". If this is the case, the above grep should resolve the errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990987]
help
Chatterbox?
[stevieb]: LOL you can easily replace "Helsinki" with "Alberta"
[perldigious]: I used to live at the top of a very steep hill stevieb, I can relate to the video. Literally used to have to build up momentum and try to force my vehicle to make it up the hill to park in my driveway properly... didn't always make it.
[choroba]: Why don't they use tire chains?
[perldigious]: First snow, probably not prepared right away choroba.
[stevieb]: it's Quebec... they do weird stuff there :) In the mountains, it is *mandatory* for trucks to use tire chains. Many passenger vehicles do as well (but it's not mandatory for them)
[perldigious]: That stuff can hit fast... like what we just got where I'm at. Saw a few people who had slid OUT of the roundabout I have to drive through to get to work, not used to the ice yet, take awhile to get the hang of it again and make vehicle adjustments.
[stevieb]: man, when the roads are glare ice, I don't even bother going to work or out... unless I absolutely have to, or was already out in the first place
[choroba]: Tire chains are mandatory here in mountains, and the only unprepared each year are gritters
[stevieb]: I have a 3 day winter survival kit in my vehicle in the event I get snowed in in the mountains (which has happened before due to avalanches closing the roads (and once in the summer due to a massive forest fire that trapped us
[perldigious]: a handful of people in my work area did not make it, but I live pretty close and it's flat the whole way, so I didn't have any trouble. The roundabout is the worst thing I have to negotiate.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2016-12-06 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (112 votes). Check out past polls.