huzefa has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: perl task3
by ELISHEVA (Prior) on Mar 07, 2009 at 21:43 UTC | |
There are a few other issues with regular expressions, variable name selection and your methods for formatting output, that deserve some attention. I've posted a revised version of your code with annotations - including how to choose an alternate record separator - see the documentation for $/ in perldoc:://perlvar for more information. Hopefully, you can use this to avoid parsing problems in future code.
Best, beth | [reply] [d/l] [select] |
|
Re: perl task3
by CountZero (Bishop) on Mar 07, 2009 at 09:16 UTC | |
If it are the splits in the foreach loop, did you check if $value and @elements contain what you expect them to contain? As we do not know what is in %sequences it is difficult to guess, but perhaps should be OP changed the code after I wrote my answer. CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James | [reply] [d/l] [select] |
|
Re: perl task3
by ELISHEVA (Prior) on Mar 07, 2009 at 17:21 UTC | |
The first thing I notice looking at your code is that your method of populating the hash %sequences would appear to be inconsistent with your routines to parse the contents of the hash. When you read in the file, you count any line without spaces as a header and skip any lines composed entirely of spaces. That would mean that only lines with a mix of spaces and non-spaces would count as values. If you have no such lines, then your hash keys will always have undefined values. Consequently when you visit each key-value pair of the hash in the foreach statement) all of your split routines will be splitting an undefined value. Now I do note that you expect spaces within the peptide chains portion of non-header lines. But is each and every header row followed by a row with a non-empty value for the peptide chains with at least one space? Also it is a bit confusing that you are first splitting the non-header line ($value) using a colon ":" and then splitting the same non-header line using a pipe "|". Which is the actual field separator? Or were you intending to split the non-header line into fields using a colon and then split each field into subfields using a pipe? If so you need to move the line splitting each field to inside the foreach loop. Of course, without any sample data it is hard to what non-header values are like. Perhaps you could provide some sample lines? A further side note: you would do well to use different names for $value in your inner and outer loops. This makes reading your code confusing. It is also possible that you yourself might sometimes intend the meaning in the outer loop (the entire non-header row terminated by ";") and sometimes the meaning in the inner loop (the subfields created by splitting the row using ":"). Best, beth | [reply] [d/l] [select] |
|
Re: perl task3
by graff (Chancellor) on Mar 07, 2009 at 16:46 UTC | |
You should have also made a comment about whether his suggestion made any difference in the problem. (Since you are now showing the line numbers in the code that cause the "uninitialized values" warnings, I guess you are still having a problem.) Another nice gesture would be to remove stuff from the OP code that has nothing to do with the problem -- e.g. we don't need to see those nonsense lines at the end that are commented out, and the "%s2a" hash that you declare is never used, so why have it there? Providing some sample data would help a lot, too. Instead of showing the file name that you open for reading (which doesn't help us at all, since we don't have that file), you could use the "DATA" file handle, and include just enough data at the end of your posted code, like this: As for your lines that are causing the warnings (in the version of the OP that I saw), here are some hints: So the point is, the various warnings are all related, and the source of the problem is probably in your data (or in your untested assumptions about the data). | [reply] [d/l] [select] |
|
Re: perl task3
by CountZero (Bishop) on Mar 07, 2009 at 16:35 UTC | |
You have significantly changed your code since you posted your original question. Now the answer I gave does not make much sense anymore. But as you are a new member, we will forgive you for this. Note however that in order to get the most out of your experience in the Monastery, updating your question or code without indicating what you changed is generally frowned upon (and for good reason). Also try to limit your code to the smallest part that exhibits the problem and do it in such a way that it will run on its own without having to rely on external files. For all we know, your "P1GroupCExercise2_trypsin.txt" is empty or might contain one of Shakespeare's sonnets. Adding some sample data at the end of your code in a __DATA__ section will be much better and adding a comment at the end of the lines where the errors / warnings occur will be considered "good style" and will get you much XP. If you can do that we will gladly look once more at your problem. CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James | [reply] [d/l] |
|
Re: perl task3
by Bloodnok (Vicar) on Mar 07, 2009 at 12:33 UTC | |
huzefa, I take did you didn't check the format of the submitted posting...try putting a </code> tag at the end of the code...you might then gain the benefit of the wisdom of a few more monks - once they can read the code.
A user level that continues to overstate my experience :-))
| [reply] |