Sorting the alpha numeric results of Hash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting the alpha numeric results of Hash
# 1  
Old 10-11-2010
Sorting the alpha numeric results of Hash

Hi All

I've got a perl script that I'm having a problem with when it prints the output of a hash. Some background. I'm trying to merge two file with a similar structure but with different data. Here is a portion of the script I'm using.

Code:
       while (<INPUT>) {
                my $sLine = $_;
                @saFields = split ',' , $sLine;
                if ($saFields[2] ne "") {
                        $sScenario = $saFields[2] ;
                        $sScenario =~ s/"//g;
                        $shHash->{$sScenario}->{'Line'} = $sLine;
                        next;
                }
                $sRF = $saFields[5] if $saFields[5] ne "";
                $sTime = $saFields[6] if $saFields[6] ne "";
                push @{$shHash->{$sScenario}->{$sTime}->{$sRF}} , $sLine;
        }

        close INPUT;
}

foreach $sScenario (sort keys %{$shHash}) {
        print $shHash->{$sScenario}->{'Line'};
        foreach $sTime (sort {$a <=> $b} keys %{$shHash->{$sScenario}}) {
                foreach $sRF (sort {$a <=> $b} keys %{$shHash->{$sScenario}->{$sTime}}) {
                        print @{$shHash->{$sScenario}->{$sTime}->{$sRF}};
                }
        }

My input file looks like this with their relevant hashes hierarchy

SSMC_1 - This is $sScenario
,,,0,, - These are $sTime
,,,1,,
,,,3,, -
SSMC_2 -> Sequence repeated again - This is $sScenario
,,,0,, - These are $sTime
,,,1,,
,,,3,, -
SSMC_10
..
SSMC_11

But when I print the the output, it looks like this

SSMC_1
,,,0,,
,,,1,,
,,,3,, -
SSMC_10
,,,0,,
,,,1,,
,,,3,, -
SSMC_11
,,,0,,
,,,1,,
,,,3,, -
SSMC_2
,,,0,,
,,,1,,
,,,3,, -

Sorting the time hash works fine but the scenario, (line underlined in bold above) doesn't. I'm not sure if it's because it's an alpha-numeric hence why the sort doesn't work. Tried googling but to no avail and I'm at a loss

Thanks in advance



---------- Post updated at 04:05 PM ---------- Previous update was at 11:41 AM ----------

This problem has been solved. Thanks for those who had a look.

I simply removed the characters
$sScenario =~ s/SSMC_//g;

And then sorted it. As simple as that.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting the numeric vs alpha values in a column to distinct columns

How could i take an input file and split the numeric values from the alpha values (123 vs abc) to distinc columns, and if the source is blank to keep it blank (null) in both of the new columns: So if the source file had a column like: Value: |1 | |2.3| | | |No| I would... (7 Replies)
Discussion started by: driftlogic
7 Replies

2. Shell Programming and Scripting

sorting left-justified numeric values

I have a file which looks roughly like this: 996 mmmmmmm 996 xxxxxxxxxxxxx 99600 ssssssssss 9964 fffffffffffff and would like to sort it numerically on the first field. I tried: sort -nr --key=1 .... The output I get is: 99600 ssssssssss 9964 ... (3 Replies)
Discussion started by: rovf
3 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

Perl: Sorting a hash value that is a list.

Hi Folks I am very much a newbie at perl but picking it up and I'm hoping you can help. I have a file input that details all the /etc/group files in our enterprise in the following format: "<host>:<group>:<gid>:<users>" I want to parse this data display it as the following:... (9 Replies)
Discussion started by: g_string
9 Replies

5. Shell Programming and Scripting

Sorting keys of a hash in perl

hi all, i have a small problem regarding sorting the keys in a hash. my %hash; for($i=0;$i<19;$i++) { $hash{$i}=$i; } foreach $c (sort keys %hash) { print "\n $hash{$c}"; } (1 Reply)
Discussion started by: niteesh_!7
1 Replies

6. Shell Programming and Scripting

sorting numeric array

Hi, I would like to do the following sorting, but the output is not what i expected. Why 222 and 2222 are not at the last two elements of array? awk 'BEGIN{a="22";a="2222";a="33";a="44";a="222";a="11";a="22";a="33";asort(a); for (i=1;i<=8;i++) print a}' 11 22 22 222 2222 33 33 44... (1 Reply)
Discussion started by: phoeberunner
1 Replies

7. Shell Programming and Scripting

Count the alpha numeric

Hi all. This is one of my interview question. input : apple output : a - 1 p - 2 l - 1 e - 1 i written the code like this echo "apple" | fold -w1 | sort | uniq -c | awk '{print $2 "-->" $1}' is any other way to do ? (5 Replies)
Discussion started by: itkamaraj
5 Replies

8. Shell Programming and Scripting

Sorting with multiple numeric keys

Data I want to sort :- 1 10 jj Y 2 100 vv B 19 5 jj A 1 11 hq D 3 8 op X 44 78 ds GG 1 8 hq D and want to sort based on the first 2 columns - which hold numeric values. Am using : cat filename | sort -nk 1,2 But the result is :- 1 10 jj Y 1 11 hq D (1 Reply)
Discussion started by: sinpeak
1 Replies

9. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies

10. Shell Programming and Scripting

how to set a variable to accept alpha-numeric characters?

I am working on a shell program that needs to accept alpha-numeric input (i.e., P00375); when I use a simple 'read' statement to read in the input (i.e., read LOG), I receive the message "p00375: bad number". How must I adjust my input statement to accept alpha-numerics? Thanks! Brent (3 Replies)
Discussion started by: bcaunt
3 Replies
Login or Register to Ask a Question