Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) 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:

#id1 #id2 #id3 0 0 1 90 1 70 2 80 2 40 3 70 3 20 4 5

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):

#id1 #id2 #id3 0 0 1 90 70 2 80 40 3 70 20 4 5
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):

#!/usr/bin/perl use strict; use Data::Dumper 'Dumper'; my $input = shift @ARGV || 'Data.txt'; my $output = shift @ARGV || 'Output.txt'; print $input, "\n"; open(DATA, $input) || die "cannot open $input for reading"; open(OUT, ">", $output) || die "cannot open $output for writing"; my $genome; my %phage=(); my @pg=(); my @id=(); while (<DATA>) { chomp; if ( /#/ ) { $genome = $line; push( @pg, $genome ); } else { my ( $id, $abd ) = split( ' ', $line ); push(@id, $id); $phage{$id}{$genome}=$abd; } } my %hash = map { $_ => 1 } @id; my @sorted_id = sort { ( $a <=> $b) } keys %hash; print OUT join( "\t", '', sort @pg ), "\n"; for my $sorted_id ( @sorted_id ) { print OUT $sorted_id, "\t"; for my $pg ( @pg ) { print OUT "$phage{$sorted_id}{$pg}\t"; } print OUT "\n"; }
(updated to replace indenting tabs with 8-space strings)
There's still room for improvement... (especially if you really want it to do something different).

In reply to Re^3: transforming XY data to X and Multiple Y column data? by graff
in thread transforming XY data to X and Multiple Y column data? by ihperlbeg

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2022-01-17 15:46 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.