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::Hash(3pm)						 Perl Programmers Reference Guide					    Tie::Hash(3pm)

NAME
Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes SYNOPSIS
package NewHash; require Tie::Hash; @ISA = qw(Tie::Hash); sub DELETE { ... } # Provides needed method sub CLEAR { ... } # Overrides inherited method package NewStdHash; require Tie::Hash; @ISA = qw(Tie::StdHash); # All methods provided by default, define only those needing overrides # Accessors access the storage in %{$_[0]}; # TIEHASH should return a reference to the actual storage sub DELETE { ... } package NewExtraHash; require Tie::Hash; @ISA = qw(Tie::ExtraHash); # All methods provided by default, define only those needing overrides # Accessors access the storage in %{$_[0][0]}; # TIEHASH should return an array reference with the first element being # the reference to the actual storage sub DELETE { $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1]) } package main; tie %new_hash, 'NewHash'; tie %new_std_hash, 'NewStdHash'; tie %new_extra_hash, 'NewExtraHash', sub {warn "Doing U$_[1]E of $_[2]. "}; DESCRIPTION
This module provides some skeletal methods for hash-tying classes. See perltie for a list of the functions required in order to tie a hash to a package. The basic Tie::Hash package provides a "new" method, as well as methods "TIEHASH", "EXISTS" and "CLEAR". The Tie::StdHash and Tie::ExtraHash packages provide most methods for hashes described in perltie (the exceptions are "UNTIE" and "DESTROY"). They cause tied hashes to behave exactly like standard hashes, and allow for selective overwriting of methods. Tie::Hash grandfathers the "new" method: it is used if "TIEHASH" is not defined in the case a class forgets to include a "TIEHASH" method. For developers wishing to write their own tied hashes, the required methods are briefly defined below. See the perltie section for more detailed descriptive, as well as example code: TIEHASH classname, LIST The method invoked by the command "tie %hash, classname". Associates a new hash instance with the specified class. "LIST" would represent additional arguments (along the lines of AnyDBM_File and compatriots) needed to complete the association. STORE this, key, value Store datum value into key for the tied hash this. FETCH this, key Retrieve the datum in key for the tied hash this. FIRSTKEY this Return the first key in the hash. NEXTKEY this, lastkey Return the next key in the hash. EXISTS this, key Verify that key exists with the tied hash this. The Tie::Hash implementation is a stub that simply croaks. DELETE this, key Delete the key key from the tied hash this. CLEAR this Clear all values from the tied hash this. SCALAR this Returns what evaluating the hash in scalar context yields. Tie::Hash does not implement this method (but Tie::StdHash and Tie::ExtraHash do). Inheriting from Tie::StdHash The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by "tied(%tiedhash)". Thus overwritten "TIEHASH" method should return a hash reference, and the remaining methods should operate on the hash referenced by the first argument: package ReportHash; our @ISA = 'Tie::StdHash'; sub TIEHASH { my $storage = bless {}, shift; warn "New ReportHash created, stored in $storage. "; $storage } sub STORE { warn "Storing data with key $_[1] at $_[0]. "; $_[0]{$_[1]} = $_[2] } Inheriting from Tie::ExtraHash The accessor methods assume that the actual storage for the data in the tied hash is in the hash referenced by "(tied(%tiedhash))->[0]". Thus overwritten "TIEHASH" method should return an array reference with the first element being a hash reference, and the remaining methods should operate on the hash "%{ $_[0]->[0] }": package ReportHash; our @ISA = 'Tie::ExtraHash'; sub TIEHASH { my $class = shift; my $storage = bless [{}, @_], $class; warn "New ReportHash created, stored in $storage. "; $storage; } sub STORE { warn "Storing data with key $_[1] at $_[0]. "; $_[0][0]{$_[1]} = $_[2] } The default "TIEHASH" method stores "extra" arguments to tie() starting from offset 1 in the array referenced by "tied(%tiedhash)"; this is the same storage algorithm as in TIEHASH subroutine above. Hence, a typical package inheriting from Tie::ExtraHash does not need to overwrite this method. "SCALAR", "UNTIE" and "DESTROY" The methods "UNTIE" and "DESTROY" are not defined in Tie::Hash, Tie::StdHash, or Tie::ExtraHash. Tied hashes do not require presence of these methods, but if defined, the methods will be called in proper time, see perltie. "SCALAR" is only defined in Tie::StdHash and Tie::ExtraHash. If needed, these methods should be defined by the package inheriting from Tie::Hash, Tie::StdHash, or Tie::ExtraHash. See "SCALAR" in perltie to find out what happens when "SCALAR" does not exist. MORE INFORMATION
The packages relating to various DBM-related implementations (DB_File, NDBM_File, etc.) show examples of general tied hashes, as does the Config module. While these do not utilize Tie::Hash, they serve as good working examples. perl v5.16.2 2012-10-11 Tie::Hash(3pm)
All times are GMT -4. The time now is 12:20 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy