Dear Monks,

I am trying to process text folders as a user input. It is important for me the order of the files as they will be printed to be the same as the input.

So far and based on your help I manage to find a possible solution using two different ways 1) ARRAYS OF ARRAYS and 2) HASHES OF HASHES.

Based on what I have read online, sort should be more than enough and should keep the correct order of my keys.

Sample of working code is provided under:

#!/usr/bin/perl use strict; use warnings; use Text::Table; use Tie::IxHash; use Fcntl qw( :flock ); use List::Util qw( max ); use POSIX qw( strftime ); use Data::Dumper qw( Dumper ); use constant ARGUMENTS => scalar 1; $| = 1; my ($value , $table); my %hash = (); sub hash_sub { foreach my $arg (@_) { open ( READ, "<" , $arg ) or die ("Could not open: ".$arg." - $!\n"); flock( READ , LOCK_EX ) or die "Could not lock '".$arg."' - $!\n"; if (-z "".$arg."") { print "File '".$arg."' is empty!\n"; # -z File has zero size (is empty). } my @doc_read = <READ>; chomp @doc_read; foreach $_ (@doc_read) { my @result = split (':', $_); if (/^\s*$/) { # /^\s*$/ check for "blank" lines may contain s +paces or tabs next; } push (@$value , $result[3]); } $hash{$arg} = $value; $value = (); # emptying the string_ref for the next ARGV close (READ) or die ("Could not close: ".$arg." - $!\n"); } my $max_idx = max map $#$_ , values %hash; my $tb = Text::Table->new; foreach my $i (0 .. $max_idx) { $tb->add( $i + 1 . ' ' , map $hash{$_}[$i] , sort keys %hash ); } return $tb; } $table = hash_sub(@ARGV); print $table;

But for example when I give the file order: sample.txt sample_2.txt sample_3.txt

File samples:

Text file sample of sample.txt:

Line_1:Line_1_1:Line_1_2:Line_1_3:Line_1_4 Line_2:Line_2_1:Line_2_2:Line_2_3:Line_2_4

Text file sample of sample_2.txt:

Line_3:Line_3_1:Line_3_2:Line_3_3:Line_3_4

Text file sample of sample_3.txt:

Line_4:Line_4_1:Line_4_2:Line_4_3:Line_4_4 Line_5:Line_5_1:Line_5_2:Line_6_3:Line_5_4 Line_6:Line_6_1:Line_6_2:Line_6_3:Line_6_4 Line_7:Line_7_1:Line_7_2:Line_7_3:Line_7_4

The output is as expected:

1 Line_1_3 Line_3_3 Line_4_3 2 Line_2_3 Line_5_3 3 Line_6_3 4 Line_7_3

But in case that I change the input file sequence, for example: sample_2.txt sample_3.txt sample.txt

I would expect to see:

1 Line_3_3 Line_4_3 Line_1_3 2 Line_5_3 Line_2_3 3 Line_6_3 4 Line_7_3

Unfortunately this is not happening, because the order of the keys.

I went online and I found that there is a module Tie::IxHash that could help me with the shorting process.

I tried to use in my code with the syntax like:

my $t = Tie::IxHash->new(%hash); $t->SortByKey; print Dumper(\$t);

But again the output was not in correct order.

At this point I am wondering, what I am doing wrong. Any suggestions or ideas would be much appreciated.

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

In reply to How to keep the order of keys in hash by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.