Beefy Boxes and Bandwidth Generously Provided by pair Networks
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 ( #862146=note: print w/replies, xml ) Need Help??

in reply to Re^2: transforming XY data to X and Multiple Y column data?
in thread transforming XY data to X and Multiple Y column data?

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (None)
    As of 2022-01-26 04:54 GMT
    Find Nodes?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:

      Results (69 votes). Check out past polls.