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

Hello,

I have this very wide spread sheet that I need to change it from horizon to vertical. To give you a rough idea, the original data layout looks like this:

|||01/01/2007|01/02/2007|01/03/2007|...|06/29/2007|

|JOHN DOE|Event1|PARTY|CLASS|WORK|...|VACATION|

|JOHN D0E|Event2|CLASS|CLASS|WORK|...|PARTY|

|JANE DOE|Event1|PARTY|CLASS|WORK|...|VACATION|

|JANE DOE|Event2|CLASS|CLASS|WORK|...|PARTY|

...

I would change the layout to the below format:

|JOHN DOE|01/01/2007|EVENT1|PARTY|

|JOHN DOE|01/02/2007|EVENT1|CLASS|

|JOHN DOE|01/03/2007|EVENT1|WORK|

|JOHN DOE|06/29/2007|EVENT1|VACATION|

...

|JOHN DOE|01/01/2007|EVENT2|CLASS|

|JOHN DOE|01/02/2007|EVENT2|CLASS|

|JOHN DOE|01/03/2007|EVENT2|WORK|

|JOHN DOE|06/29/2007|EVENT2|PARTY|

...

Is there an easy way to do it?

I'd greatly appreciate if you could please help me out.

  • Comment on How to Change Data Layout from Horizon to Vertical

Replies are listed 'Best First'.
Re: How to Change Data Layout from Horizon to Vertical
by shmem (Chancellor) on Jan 16, 2007 at 23:22 UTC
    Please elaborate on what's horizontal and what's vertical for you.

    From your sample data, I conclude that

    • the first line with dates isn't a header line, but some sorting criteria
    • the transformation of your data consists in sorting rows according to some column value
    • in the final output some columns are swapped

    but I may be utterly wrong in my assumptions. Please read I know what I mean. Why don't you?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How to Change Data Layout from Horizon to Vertical
by rodion (Chaplain) on Jan 17, 2007 at 00:30 UTC
    Is what you had in mind something like this?
    use warnings; use strict; my $horiz_data = <<"HORIZ" ; |||01/01/2007|01/02/2007|01/03/2007|06/29/2007| |JOHN DOE|Event1|PARTY|CLASS|WORK|VACATION| |JOHN D0E|Event2|CLASS|CLASS|WORK|PARTY| |JANE DOE|Event1|PARTY|CLASS|WORK|VACATION| |JANE DOE|Event2|CLASS|CLASS|WORK|PARTY| HORIZ my @horiz = split "\n", $horiz_data; my @dates = split '\|', shift @horiz; shift @dates; shift @dates; shift @dates; for my $line (@horiz) { my @flds = split '\|', $line; shift @flds; my $name = shift @flds; my $event = shift @flds; my @dates_copy = @dates; for my $date (@dates_copy) { print "|$name|$date|$event|",shift(@flds),"|\n"; } } # for $line
    which produces
    |JOHN DOE|01/01/2007|Event1|PARTY| |JOHN DOE|01/02/2007|Event1|CLASS| |JOHN DOE|01/03/2007|Event1|WORK| |JOHN DOE|06/29/2007|Event1|VACATION| |JOHN D0E|01/01/2007|Event2|CLASS| |JOHN D0E|01/02/2007|Event2|CLASS| |JOHN D0E|01/03/2007|Event2|WORK| |JOHN D0E|06/29/2007|Event2|PARTY| |JANE DOE|01/01/2007|Event1|PARTY| |JANE DOE|01/02/2007|Event1|CLASS| |JANE DOE|01/03/2007|Event1|WORK| |JANE DOE|06/29/2007|Event1|VACATION| |JANE DOE|01/01/2007|Event2|CLASS| |JANE DOE|01/02/2007|Event2|CLASS| |JANE DOE|01/03/2007|Event2|WORK| |JANE DOE|06/29/2007|Event2|PARTY|
Re: How to Change Data Layout from Horizon to Vertical
by NetWallah (Canon) on Jan 17, 2007 at 05:42 UTC
    Assuming you are using Win32 Excel, and OLE, you should be able to accomplish what you want by converting this code into the equivalent perl:
    Sub TransposeIt() Dim ROr As Range Set ROr = Sheets("Hoja2").Range("A1:C5") '5x3 Table {1,2,3;4,5,6;7,8 +,9;10,11,12;13,14,15} Sheets("Hoja3").Select Sheets("Hoja3").Range(Cells(1, 1), Cells(ROr.Columns.Count, ROr.Rows +.Count)).FormulaArray = "=TRANSPOSE(Hoja2!" & ROr.Address & ")" End Sub
    From : Mr. Excel
    Note: The TRANSPOSE function must be entered as an Array function.

         "A closed mouth gathers no feet." --Unknown

Re: How to Change Data Layout from Horizon to Vertical
by ww (Archbishop) on Jan 17, 2007 at 14:19 UTC
    Is this even a Perl-ish question?
    If so, where's your code
    In other words, what have you tried?
    What documentation have you read?

    Some other replies raise these and other questions. In their totality, they add up to a suggestion that you read How do I post a question effectively?

    BTW, on the suspicion that you're using M$, the ever-so-nice people at Microsoft offer this, about using something called the "transpose button" to rearrange rows as columns and vice versa, in Excel 2003's local help:

    Copy the data in one or more columns or rows.
    Before you paste the copied data, right-click your first destination cell (the first cell of the row or column into which you want to paste your data), and then click Paste Special.
    In the Paste Special dialog box, select Transpose, and then click OK.
    You'll find the Transpose check box in the lower-right corner of the dialog box:
    The search phrase for the above, BTW, is -- oddly enough -- "Convert columns to rows, or rows to columns" (though, /me wonders about the "or" as I fail to see how to do one without also doing the other :-{}
Re: How to Change Data Layout from Horizon to Vertical
by sailortailorson (Scribe) on Jan 16, 2007 at 23:11 UTC
    Is your spreadsheet in MS Excel?
A reply falls below the community's threshold of quality. You may see it by logging in.