|Welcome to the Monastery|
Re^3: transforming XY data to X and Multiple Y column data?by graff (Chancellor)
|on Sep 27, 2010 at 02:51 UTC||Need Help??|
In the OP, you say:
I tried using hashes, but didn't seem to work as the first column has duplicates.
But just above that, you show lines that you are "trying to output" and those have duplicates in the first column. Either you actually do want some duplicates (apparently, when the same "column 1" value appears more than once under a single "id" value), or else you made a mistake in showing us what you are "trying to output".
Apart from that, I have a question about your input data. Your code assumes that the columns in your "Data.txt" file are separated by tabs, but the sample data you posted appears to be separated by spaces.
On top of that, I have to complain about your strange use of indentation. It runs, but it's hard to read.
The output I got using the OP data (with spaces instead of tabs) looked like this:
First suggestion: since you are using a data structure, try using Data::Dumper together with the perl debugger perldebug -- e.g. you use the debugger "b" command to set a break-point at a specific line (like, inside a "for" loop), use the "c" command to continue execution (which will then stop at the break-point), and use a command like "p Dumper(\%phage)" to see what your data structure contains at that point.
When I did that on (a properly indented version of) your code, I noticed the "tab-vs-space" issue, changed the "split" regex, and got something closer to what you seem to want (updated to convert tabs to spaces):
Of course, this does not have any repeated values in the first column, which makes it different from your OP output sample. So... do you really want some things repeated and not others?
BTW, here's a cleaned-up version of your code, fixing the indentation and the split regex, adding Data::Dumper, moving some uses of "my" into their proper scope, and simplifying a few other things (also removed "use integer" -- it would have no effect on the code as-is, and if you ever do division, it's better to use the "int()" function as needed):
(updated to replace indenting tabs with 8-space strings)