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

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!

Replies are listed 'Best First'.
Re: How to keep the order of keys in hash
by Cristoforo (Curate) on Jul 09, 2014 at 17:04 UTC
    To keep the order of file names from the command line in your table creation, you would need to replace sort keys %hash with @_.

    Chris

      Hello Christoforo,

      Once more, you are absolutely right. If you do not mind can you point me out to the correct direction to understand what is the array that is loaded at @_. Because it dose not make sense to me at least so far why the sort keys %hash was not working correctly.

      Never the less thank you again your time and effort.

      Seeking for Perl wisdom...on the process...not there...yet!
        @_ is initialized when you call the function with the parameter @ARGV.

        Instead of using @_, you could use @ARGV if you wanted to.

Re: How to keep the order of keys in hash
by perlfan (Parson) on Jul 09, 2014 at 17:47 UTC
    I think you want to use Tie::Hash::Indexed, maybe.

    I don't think that Data::Dumper is going to show you want you want since it's the internal implementation of the hash's accessors via the Tie::Hash interface that actually provide the ordering you need.

    If you need to maintain some sorted order not related to insert order, then you might need to look as some sort of Perl based heap implementation like, Heap::Simple.

      Hello perlfan,

      I will read the Heap::Simple CPAN module that you suggested. It is always nice to learn more things.

      Thank you for your time and effort replying and assisting me on my question.

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