in reply to Re: How to concatenate a generic array of arrays and an array of hashes
in thread How to concatenate a generic array of arrays and an array of hashes

Hello boftx,

Well up to this point I got it, the problem is after. I am trying to print them in columns based on each array separately with spaces in between and simultaneously in case that the files have different sizes.

Ok this sounds really complicated so I will try to draw it, based on the sample files that I have posted here.

Line_1_3 Line_3_3 Line_4_3 Line_2_3 Line_5_3 Line_6_3 Line_7_3

This is the expected printed format. Well I am trying to get access to each array elements individually after reading them from the argument input, because I want to apply mathematical calculation on array[0] and array3 for example. It dose not matter if they have same number or lines but it does matter when it comes to printing on the screen or in a file.txt etc.

This is the reason that initially solve it with 2 dimension arrays and then with array and hashes. In order to get the best possible output out of them.

Apologies if my description it is really confusing so far.

Again thanks for your time and effort to assist me with my problem.

Seeking for Perl wisdom...on the process...not there...yet!
  • Comment on Re^2: How to concatenate a generic array of arrays and an array of hashes
  • Download Code

Replies are listed 'Best First'.
Re^3: How to concatenate a generic array of arrays and an array of hashes
by AppleFritter (Vicar) on Jul 07, 2014 at 11:02 UTC

    OK, here's some code that does what you want. I've not included the part that reads the data.

    #!/usr/bin/perl use warnings; use strict; use List::Util qw/max/; my $VAR1 = { 'sample.txt' => [ 'Line_1_3long', 'Line_2_3' ], 'sample_2.txt' => [ 'Line_3_3longer' ], 'sample_3.txt' => [ 'Line_4_3', 'Line_5_3', 'Line_6_3evenlonger', 'Line_7_3', ] }; my %maxlength = (); foreach my $key (sort keys %$VAR1) { $maxlength{$key} = max map { length } @{$VAR1->{$key}}; } my $maxelements = max map { $#$_ } values %$VAR1; foreach my $line (0..$maxelements) { foreach my $key (sort keys %$VAR1) { if($line >= @{$VAR1->{$key}}) { print " " x ($maxlength{$key} + 1); } else { print $VAR1->{$key}->[$line]; print " " x ($maxlength{$key} - length($VAR1->{$key}->[$line]) + + 1); } } print "\n"; }

    This prints:

    $ perl test.pl Line_1_3long Line_3_3longer Line_4_3 Line_2_3 Line_5_3 Line_6_3evenlonger Line_7_3 $

    Notes:

    1. If you know your pieces of data are always going to be the same length, you can leave out the %maxlength computation, and replace $maxlength{$key} by this known value.
    2. You might be able to use formats (see perlform), but I have literally zero experience with them and couldn't say.

    I hope this helps. My bill for an hour (rounded up) of paid consultant work's in the mail. :)

      Could use Text::Table for the output. Using an array of arrays, (also could use hash of arrays with a few adjustments to the code).
      #!/usr/bin/perl use strict; use warnings; use Text::Table; use List::Util 'max'; my @final = ( [ 'Line_1_3', 'Line_2_3' ], [ 'Line_3_3' ], [ 'Line_4_3', 'Line_5_3', 'Line_6_3', 'Line_7_3' ] ); my $max_idx = max map $#$_, @final; my $tb = Text::Table->new; for my $i (0 .. $max_idx) { $tb->add( map $final[$_][$i], 0 .. $#final); } print $tb

      The hash method.

      #!/usr/bin/perl use strict; use warnings; use Text::Table; use List::Util 'max'; my %final = ( 'sample.txt' => [ 'Line_1_3long', 'Line_2_3' ], 'sample_2.txt' => [ 'Line_3_3longer' ], 'sample_3.txt' => [ 'Line_4_3', 'Line_5_3', 'Line_6_3evenlonger', 'Line_7_3', ] ); my $max_idx = max map $#$_, values %final; my $tb = Text::Table->new; for my $i (0 .. $max_idx) { $tb->add( map $final{$_}[$i], sort keys %final); } print $tb
        Wow, nice, thanks for the pointer. That's a module I'll have to remember.

        Dear Christoforo,

        Respect, I was starting to get the impression that my problem can not be solved. I tried infinite possible solutions. I am not expert on perl, maybe not even moderate operator in comparison to so many people here. I have been trying like crazy 5 days to solve it, and so far not successful.

        Thanks a lot, I was about to give up.

        Seeking for Perl wisdom...on the process...not there...yet!

      Hello AppleFritter,

      Hmmm this is interesting, I will have ago and see what I can make out of it.

      Thank you for your time and effort to assist me with my question.

      Seeking for Perl wisdom...on the process...not there...yet!
        *tips hat* You're welcome!