Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The key is to figure out how you would do this without code. What things did you iterate through in what order when you formulated your desired output? Take careful note and then codify that process. This gives your desired output:

#!/usr/bin/perl use warnings; use strict; my $id; my %data; my %seen; while (<main::DATA>) { chomp; next unless /\S/; if (/\A#(id\d+)\z/) { $id = $1; $seen{'id'}{$id} = 1; } elsif (/\A(\d+)\s+(\d+)\z/) { die "no #id found before line: '$_'" unless $id; $seen{'key'}{$1} = 1; $data{$1}{$id} ||= []; push(@{$data{$1}{$id}}, $2); } else { die "unrecognized line: '$_'"; } } my ($width) = (sort {$b <=> $a} map { length } map { keys %$_ } values + %seen); my @ids = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { /id(\d+)/; [$_, $1] } keys %{$seen{'id'}}; print join(' ', map { sprintf "%${width}s", $_ } '', @ids), "\n"; for my $i (sort keys %{$seen{'key'}}) { while (keys %{$data{$i}}) { print join(' ', map { sprintf "%${width}s", $_ } $i, map { exi +sts($data{$i}{$_}) && @{$data{$i}{$_}} ? shift @{$data{$i}{$_}} : '' } keys %{$seen{'id'}}), "\n"; for my $id (keys %{$data{$i}}) { delete $data{$i}{$id} unless @{$data{$i}{$id}}; } } } __DATA__ #id1 1 90 2 80 3 70 #id2 1 70 2 40 2 40 3 20 4 5 #id3 0 0 0 0 0 0

In reply to Re: transforming XY data to X and Multiple Y column data? by perlpie
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 studying the Monastery: (6)
As of 2022-01-17 11:16 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.