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

Hi! I have one column with 14 rows and I need to split it in half and turn it into two columns with 7 rows. I'm a newbie and can't figure out what to use to accomplish this. Any suggestions? Thanks!

Replies are listed 'Best First'.
Re: splitting one column into two
by Fletch (Bishop) on Jul 16, 2008 at 19:31 UTC

    The problems right on line 23 where you've reversed the polarity of the neutron flow through the Jeffries tube on deck 37. Take care of that and things will split fine.

    Or read How (Not) To Ask A Question, post what you've tried along with sample data and you might get something approaching a helpful reply instead.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: splitting one column into two
by ikegami (Patriarch) on Jul 16, 2008 at 20:06 UTC
    my $mid = int( (@array+1)/2 ); for my $i (0 .. $mid-1) { if ($mid+$i < @array) { print($array[$i], "\t", $array[$mid+$i], "\n"); } else { print($array[$i], "\n"); } }

    There's gotta be a module on CPAN that does it better...

Re: splitting one column into two
by TGI (Parson) on Jul 16, 2008 at 23:52 UTC

    One column of what?

    Fourteen rows of what?

    Split what in half?

    Are you trying to rearrange a table?

    If so how should the items in the table be rearranged?

    Your Possible Table Results ----- ----- ----- A A B A H B C D B I C E F C J D G H D K E I J E L F K L F M G M N G N H I J K L M N

    Here's some perl code that prints out simple tables from an array.

    use strict; use warnings; my @stuff = 1..14; # Print 14 rows in one column. print "\n\n14 Rows\n"; print join "\n", @stuff; # Print 7 rows two columns print "\n\n7 Rows One Way\n"; print map { "$stuff[2*$_]\t$stuff[2*$_+1]\n" } 0..$#stuff/2; print "\n\n7 Rows Another Way\n"; print map { "$stuff[$_]\t$stuff[$#stuff/2+$_+1]\n" } 0..$#stuff/2;

    You won't be able to write a program in any language unless you can clearly describe what you want to do in any suitable human language (such as English).

    Start your program by writing comments:

    # Open a text file # Read the file # Print file contents out like this # LINE_1 LINE_2 # LINE_3 LINE_4

    Then start filling in the Perl between the comments.

    If you get stuck, try posting a clear question with your code so far, and you'll have much better luck.

    Happy hacking. When I was starting out, I found How to RTFM to be a very useful post. Note that perldoc.com is long gone, so use http://perldoc.perl.org to find perldoc on the web.


    TGI says moo

Re: splitting one column into two
by leocharre (Priest) on Jul 16, 2008 at 20:36 UTC

    Seriously, what are you talking about? Is this database stuff?? Some people can drink/smoke and code.

    Then there's me, I for one cannot- I can't even turn on my laptop when I party. You should ask yourself which one are you?

    A+ for vagueness :-)

    Ahh.. I'm sorry. What I really meant to say was 727626762yh2nznvni4kj2h 23h 23525288vyLJjjlJLJLJ##Hntnmt24fni j325j5 2k3k.

Re: splitting one column into two
by apl (Monsignor) on Jul 16, 2008 at 21:49 UTC
    If this was a task you were doing by hand (rather than by computer), what would you do?

    If you can't state the solution in simple English, you can't program a solution.