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.
|
|---|
| 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 | |
by thanos1983 (Parson) on Jul 09, 2014 at 19:44 UTC | |
by Cristoforo (Curate) on Jul 09, 2014 at 20:54 UTC | |
by thanos1983 (Parson) on Jul 09, 2014 at 22:11 UTC | |
|
Re: How to keep the order of keys in hash
by perlfan (Parson) on Jul 09, 2014 at 17:47 UTC | |
by thanos1983 (Parson) on Jul 09, 2014 at 19:46 UTC |