Sponsored Content
Top Forums Shell Programming and Scripting Perl Hash:Can not keep hash data in the same order that it was inserted Post 302406595 by jgfcoimbra on Tuesday 23rd of March 2010 10:57:28 AM
Old 03-23-2010
Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below.

Code:
#!/usr/bin/perl
use warnings;
use Tie::IxHash;
use strict;

tie (my %programs, "Tie::IxHash");

while (my $line = <DATA>) {
    chomp $line;
    my( $company, $site, $program, $product, $price) = split('--', $line);
    push @{ $programs{$program}{$product} }, $price;
}

for my $program (keys %programs) {
    print "$program\n";
    print "list of products: ", join(", ", keys %{$programs{$program}}), "\n";
    print "list of prices: ",
          join(", ", map { @{$programs{$program}{$_}} } keys %{$programs{$program}}), "\n";
}

__DATA__
COMPANY1--SITE1--PROGRAM1--PRODUCT1--PRICE1
COMPANY1--SITE1--PROGRAM2--PRODUCT1--PRICE1
COMPANY1--SITE1--PROGRAM1--PRODUCT2--PRICE3
COMPANY2--SITE1--PROGRAM1--PRODUCT1--PRICE2


Last edited by Franklin52; 03-23-2010 at 12:12 PM.. Reason: Please use code tags!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

2. Shell Programming and Scripting

PERL: reading 2 column data into Hash file

I am trying to read in a 2 column data file into Perl Hash array index. Here is my code. #!/usr/bin/perl -w use strict; use warnings; my $file = "file_a"; my @line = (); my $index = 0; my %ind_file = (); open(FILE, $file) or die($!); while(<FILE>) { chomp($_); if ($_ eq '') { ... (1 Reply)
Discussion started by: subhap
1 Replies

3. Shell Programming and Scripting

perl-extract data from hash values

Hello, I have parsed an xml file using perl to get the hash values and the output looks like this $VAR1 = { 'RT' => { 'List' => { 'String' => ... (1 Reply)
Discussion started by: userscript
1 Replies

4. Shell Programming and Scripting

Assigning a hash to another hash key

Hello, I have a hash in hsh. I need to assign it to another hash globalHsh. I think the below statement does not work $globalHsh{$id} = %hsh; What is the right way to assign it? Thanks (3 Replies)
Discussion started by: rsanjay
3 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Remove default data hash sorting in perl script?

Hi, I have a datahash with 'n' number of values in perl script. I am writing a xml file from the datahash. I am getting output with sorting(Field sorting). My question is that i don't want any default sorting.whatever i am inserting into datahash it should give same xml file. Any help? ... (0 Replies)
Discussion started by: solo123
0 Replies

7. Shell Programming and Scripting

Sorting values of hash in ascending order using Perl

I want to sort values of a hash in ascending order. my %records; for my $value (sort values %records){print $value,"\n";} When I use the above code I get values in this order: 1,10,11,2,3,4,5,6,7,8,9. But, I need values in my output in this order: 1,2,3,4,5,6,7,8,9,10,11. Can Someone... (1 Reply)
Discussion started by: koneru_18
1 Replies

8. 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

9. Shell Programming and Scripting

Dynamically parse BibTeX and create hash of hash

Hello gurus, Iam trying to parse following BibTex file (bibliography.bib): @book{Lee2000a, abstract = {Abstract goes here}, author = {Lee, Wenke and Stolfo, Salvatore J}, title = {{Data mining approaches for intrusion detection}}, year = {2000} } @article{Forrest1996, abstract =... (0 Replies)
Discussion started by: wakatana
0 Replies

10. Shell Programming and Scripting

Need to print hash of hash in table format

Hi, I have a hash of hash where it has name, activities and count i have data like this - $result->{$name}->{$activities} = $value; content of that are - name - robert tom cat peter activities - running, eating, sleeping , drinking, work i need to print output as below ... (3 Replies)
Discussion started by: asak
3 Replies
Tie::RefHash(3pm)					 Perl Programmers Reference Guide					 Tie::RefHash(3pm)

NAME
Tie::RefHash - use references as hash keys SYNOPSIS
require 5.004; use Tie::RefHash; tie HASHVARIABLE, 'Tie::RefHash', LIST; tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST; untie HASHVARIABLE; DESCRIPTION
This module provides the ability to use references as hash keys if you first "tie" the hash variable to this module. Normally, only the keys of the tied hash itself are preserved as references; to use references as keys in hashes-of-hashes, use Tie::RefHash::Nestable, included as part of Tie::RefHash. It is implemented using the standard perl TIEHASH interface. Please see the "tie" entry in perlfunc(1) and perltie(1) for more information. The Nestable version works by looking for hash references being stored and converting them to tied hashes so that they too can have references as keys. This will happen without warning whenever you store a reference to one of your own hashes in the tied hash. EXAMPLE
use Tie::RefHash; tie %h, 'Tie::RefHash'; $a = []; $b = {}; $c = *main; $d = "gunk"; $e = sub { 'foo' }; %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5); $a->[0] = 'foo'; $b->{foo} = 'bar'; for (keys %h) { print ref($_), " "; } tie %h, 'Tie::RefHash::Nestable'; $h{$a}->{$b} = 1; for (keys %h, keys %{$h{$a}}) { print ref($_), " "; } THREAD SUPPORT
Tie::RefHash fully supports threading using the "CLONE" method. STORABLE SUPPORT
Storable hooks are provided for semantically correct serialization and cloning of tied refhashes. RELIC SUPPORT
This version of Tie::RefHash seems to no longer work with 5.004. This has not been throughly investigated. Patches welcome ;-) MAINTAINER
Yuval Kogman <nothingmuch@woobling.org> AUTHOR
Gurusamy Sarathy gsar@activestate.com 'Nestable' by Ed Avis ed@membled.com SEE ALSO
perl(1), perlfunc(1), perltie(1) perl v5.12.1 2010-04-26 Tie::RefHash(3pm)
All times are GMT -4. The time now is 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy