Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: filling in the zeros...please help

by jaredor (Curate)
on May 15, 2012 at 18:16 UTC ( #970689=note: print w/ replies, xml ) Need Help??


in reply to filling in the zeros...please help

If you can hold everything in memory, this should come close to doing what you want. It has the following assumptions/behviors:

  • Each row has the same number of whitespace delimited fields (columns).
  • Zeroes starting a column or ending a column are meant to be zeroes after patching.
  • #!/usr/bin/env perl use Modern::Perl; use Data::Dump qw(pp); my %cols; my @patch_cols = qw(2 3); while (<DATA>) { my @row = split; push @{$cols{$_}}, $row[$_] for (0..$#row); } for my $col (@patch_cols) { my @fwd = @{$cols{$col}}; my @bwd = reverse @fwd; for my $i (1..$#fwd) { $fwd[$i] = $fwd[$i-1] if $fwd[$i] == 0; $bwd[$i] = $bwd[$i-1] if $bwd[$i] == 0; } @bwd = reverse @bwd; $fwd[$_] == $bwd[$_] and $cols{$col}->[$_] = $fwd[$_] for (0..$#fw +d); } say pp(\%cols); say pp(\@patch_cols); __DATA__ 1 100 1 2 101 200 1 2 201 300 1 0 301 400 1 0 401 500 0 2 501 600 0 2 601 700 0 0 701 800 1 1 801 900 1 2

    Produces:

    { "0" => [1, 101, 201, 301, 401, 501, 601, 701, 801], "1" => [100, 200, 300, 400, 500, 600, 700, 800, 900], "2" => [1, 1, 1, 1, 1, 1, 1, 1, 1], "3" => [2, 2, 2, 2, 2, 2, 0, 1, 2], } [2, 3]


Comment on Re: filling in the zeros...please help
Select or Download Code
Re^2: filling in the zeros...please help
by david_lyon (Sexton) on May 15, 2012 at 22:21 UTC
    Thanks jaredor very much it helped me a lot.
    
    
    Do you know or anyone else know how to output the results in the same format at the input file ie tab delimited?
    
    Thanks so much again
    Dave
    

      Dave,

      You have a hash of arrays; each array is the same length; each array represents a column; the hash key for an array is the number of the column (starting at zero) that it represents. Because of this nice regularity, you can use a for loop like above and print out each row (starting from 0) with a simple print statement. (Actually, it would be even simpler to have an array of arrays, but not that much simpler for it to be worthwhile for me to go back and edit code from my mobile device.)

      And since I'm not using a "real" keyboard at the moment, I'm not going to write out any lines of code. However, I probably shouldn't: You need to have the perl skills to do this relatively simple task. If you can't do this, then you probably don't understand what the above code is doing. You need to understand that first.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-12-20 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls