http://www.perlmonks.org?node_id=857322

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Writing data into columns
by psini (Deacon) on Aug 25, 2010 at 22:34 UTC
Re: Writing data into columns
by toolic (Bishop) on Aug 25, 2010 at 22:39 UTC
    • Read perlintro
    • Write some code which tries to do what you want.
    • If you still have problems, post the code you have tried.
    • Clarify the description of your problem. I do not understand how you get that output from that input.
Re: Writing data into columns
by graff (Chancellor) on Aug 25, 2010 at 22:51 UTC
    To expand a bit on that last point raised by toolic above: I have reason to suspect that your "Desired output" might contain some typos (missing numbers, bad spacing, etc). But it would be unwise for me to guess at what the correct form should be, because I have no way of knowing that. I'm just reacting to some apparent discrepancies in your "data" that aren't explained in your "description".

    Also, when you say "... the numbers ... could have letters in them ... the numbers/entryes ... could have letters and hyphens in them ...", it really is impossible for us to make any sense of this, unless you actually show some relevant examples of actual input and (correct) desired output.

    Apart from that, you will need to show some perl code that you have tried.

Re: Writing data into columns
by davido (Cardinal) on Aug 26, 2010 at 01:51 UTC

    This feels like one of those IQ tests where you have to look at a sequence and figure out what comes next. Unlike those tests, the more I look at your desired output, the less I understand how it relates to the input.


    Dave

Re: Writing data into columns
by dasgar (Priest) on Aug 26, 2010 at 02:52 UTC

    As others have eluded to, you haven't given enough information for others to understand what you're trying to do and/or where you're encountering problems. I'll venture a guess that you're wanting formatted output. If I'm right, you'll want to check out sprintf. However, if you need more help than that, you've got to take the first step and provide more information.

Re: Writing data into columns
by JavaFan (Canon) on Aug 26, 2010 at 09:52 UTC
    Assuming your example contains some typos, you may want to go for:
    use 5.010; use strict; use warnings; use List::Util 'max'; my (%data, %all, @keys); while (<DATA>) { chomp; my @chunks = split; my $key = shift @chunks; push @keys, $key unless exists $data{$key}; foreach my $chunk (@chunks) { $data{$key}{$chunk}++; $all{$chunk} = 1; } } my @fields = sort {$a <=> $b} keys %all; my $l = max map {length} @fields; foreach my $key (@keys) { while (keys %{$data{$key}}) { print $key, " "; foreach my $field (@fields) { if ($data{$key}{$field}) { printf "%${l}s ", $field; delete $data{$key}{$field} unless --$data{$key}{$field +}; } else { printf "%${l}s ", "-"; } } print "\n"; } } __DATA__ 0. 1 1 7 2 2 9 4 6 8 10 2 1. 10 1 6 3 4 9 2. 5 8 59 77 81 3 3. 16 17 19 23 3
    The result:
    0. 1 2 - 4 - 6 7 8 9 10 - - - - - - - 0. 1 2 - - - - - - - - - - - - - - - 0. - 2 - - - - - - - - - - - - - - - 1. 1 - 3 4 - 6 - - 9 10 - - - - - - - 2. - - 3 - 5 - - 8 - - - - - - 59 77 81 3. - - 3 - - - - - - - 16 17 19 23 - - -
    Changing the sorting of fields if you have non-numeric fields is left as an exercise to the reader.
    A reply falls below the community's threshold of quality. You may see it by logging in.