hellohello1 has asked for the wisdom of the Perl Monks concerning the following question:

After I split my data, when I try to print the output to a tab delimited textfile, I have problem getting the output I want.

Output that I want:

A776 762.81 0.76 1 1 1 1 1 1 1 A872 762.91 1.23 2 2 2 2 2 2 2 . . .

What I get is :
A776 762.81 0.76 1 A776 762.81 0.76 1 A776 762.81 0.76 1 A776 762.81 0.76 1 A776 762.81 0.76 1 A776 762.81 0.76 1 A776 762.81 0.76 1 A872 762.91 1.23 2 A872 762.91 1.23 2 A872 762.91 1.23 2 A872 762.91 1.23 2 A872 762.91 1.23 2 A872 762.91 1.23 2 A872 762.91 1.23 2
Here's part of my code:
while(my $header_line = <CURINFILE>) { my @headers = split/\s+/, $header_line; push @full_data , [split /\s+/, $_] while (<CURINFILE>); for my $arr_ref1 (@full_data) { for my $arr_ref2(@full_data) { for my $index (3..$#headers) { my $ratio1 = $$arr_ref2[$index]/$$arr_ref1[$index]; # + this is the part where 1 and 2 is outputted. my $ratio = sprintf("%.4f", $ratio1); print OUT1 "$$arr_ref2[0]\t$$arr_ref1[0]\t$$arr_ref2[1]\t$$ar +r_ref2[2]\t\t$ratio1\n"; } } } }
How do I get $ratio1 to be formatted in tab instead of newline? Advice appreciated!

Replies are listed 'Best First'.
Re: Split and join
by kcott (Archbishop) on Mar 07, 2014 at 04:34 UTC

    G'day hellohello1,

    Your main problem is that you print once only in the innermost loop. You need to print the parts before the ratios (without a newline) before that loop. Then print the ratios (still without a newline) inside that loop. Then print just a newline after that loop.

    The output you say you're getting is not generated by the code you've posted! I'm not going to spend any time trying to guess which is right and what you really want.

    Here's a rough approximation of the required code structure. I'll leave you to work out which fields you actually want to print and whether or not you're going to use the formatting you generate for the ratios.

    #!/usr/bin/env perl use strict; use warnings; my @data; while (<DATA>) { next if $. == 1; push @data, [split]; } for my $i (@data) { for my $j (@data) { print "Ratio ($j->[0]:$i->[0]): "; printf ' %.4f', $j->[$_] / $i->[$_] for 3 .. $#$j; print "\n"; } } __DATA__ ID Record Time A B C D E F G A776 762.81 0.76 2 2 2 2 2 2 2 A872 762.91 1.23 4 4 4 4 4 4 4

    Output:

    Ratio (A776:A776): 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Ratio (A872:A776): 2.0000 2.0000 2.0000 2.0000 2.0000 2.0000 2.0000 Ratio (A776:A872): 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 0.5000 Ratio (A872:A872): 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000

    -- Ken

      Thanks Kcott! I thought the output that I am getting got something to do with how I code the $ratio1 and I might be missing something since I split the data and have no idea how to make into the output I want. I will look through your code structure and try to make sense of it and work my way around. I'll reply here again if I encounter problems.

      I am learning Perl as I go along and this forum has been very helpful in my learning process :) Thanks guys...it has been very stimulating to read the suggestions and advices from you!

        I manage to get it working. Thanks a lot guys!
Re: Split and join
by davido (Cardinal) on Mar 07, 2014 at 03:12 UTC

    I'm sure it's not impossible for us to reverse engineer what your input data looks like, but it is too much to ask. Would you mind saving us the time by showing us an example?


    Dave

      My input data is like this:
      ID Record Time A B C D E F G A776 762.81 0.76 2 2 2 2 2 2 2 A872 762.91 1.23 4 4 4 4 4 4 4
      What my code does is to take A column and divide row against row, same for the rest of B to G. And the list continue down...(I have thousands of IDs). This process refer to this code as stated in my first post:
      for my $arr_ref1 (@full_data) { for my $arr_ref2(@full_data) { for my $index (3..$#headers) { my $ratio1 = $$arr_ref2[$index]/$$arr_ref1[$index]; # + this is the part where 1 and 2 is outputted.
      I hope it helps in the understanding of my question?
Re: Split and join
by LanX (Saint) on Mar 07, 2014 at 03:22 UTC
    This code seems very messed up, like drag-and-dropped from different sources.

    you have nested loops over <CURINFILE> AND @full_data !?!

    you say join but never use it, using hardcoded indices.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I am not sure if I should use the join function to fix the problem I am having :) Sorry about the misleading title!

      I just want to get the $ratio1 to be "join" back as tabs instead of breaking into new line.

      The nested loop is to allow the division for row against row. So take the whole column and put as array. $arr_ref2(@full_data) and $arr_ref2(@full_data)

      So that I can do : my $ratio1 = $$arr_ref2$index/$$arr_ref1$index;

      I hope it is more clear now? - E :)