473,385 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help On Hashes

rajiv07
141 100+
Hi to all,

How do i print the hashes in the stored manner.because when i print the hash value it print random order by default. But i want the value by the stored
manner.

Here i am using this hash

Expand|Select|Wrap|Line Numbers
  1. my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D);
  2.  
  3.  or $key(keys %status)
  4.  {
  5.   print "$key \t $status{$key}";
  6.   print "\n";
  7.  }
  8.  
The output will be

Delivered D
Order Under processing Under processing
Payment Received PR
Courier C
Shipped From WareHouse SWH

But I want

Payment Received PR
Order Under processing Under processing
Shipped From WareHouse SWH
Courier C
Delivered D


Thanks

Regards
Rajiv
Dec 10 '07 #1
8 1632
mehj123
55
Hi to all,

How do i print the hashes in the stored manner.because when i print the hash value it print random order by default. But i want the value by the stored
manner.

Here i am using this hash

Expand|Select|Wrap|Line Numbers
  1. my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D);
  2.  
  3.  or $key(keys %status)
  4.  {
  5.   print "$key \t $status{$key}";
  6.   print "\n";
  7.  }
  8.  
The output will be

Delivered D
Order Under processing Under processing
Payment Received PR
Courier C
Shipped From WareHouse SWH

But I want

Payment Received PR
Order Under processing Under processing
Shipped From WareHouse SWH
Courier C
Delivered D


Thanks

Regards
Rajiv
Hi Rajiv,
Why dont you use keys like 1,2,3 etc and let these keys point to the values.i.e
Expand|Select|Wrap|Line Numbers
  1. 1=>PR,
  2. 2=>Under processing etc
and in your code replace '1' by Payment Received.. I think you can maintain the order of addition by this way only.
To make it simpler you can use the Tie::IxHash module. See the example here
Dec 10 '07 #2
rajiv07
141 100+
Thank U mehj123.Let me try and i will report the status to u.

regards
Rajiv
Dec 10 '07 #3
numberwhun
3,509 Expert Mod 2GB
Hi to all,

How do i print the hashes in the stored manner.because when i print the hash value it print random order by default. But i want the value by the stored
manner.

Here i am using this hash

Expand|Select|Wrap|Line Numbers
  1. my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D);
  2.  
  3.  or $key(keys %status)
  4.  {
  5.   print "$key \t $status{$key}";
  6.   print "\n";
  7.  }
  8.  
The output will be

Delivered D
Order Under processing Under processing
Payment Received PR
Courier C
Shipped From WareHouse SWH

But I want

Payment Received PR
Order Under processing Under processing
Shipped From WareHouse SWH
Courier C
Delivered D


Thanks

Regards
Rajiv
What you have to realize is that a hash is NOT an array. It is a list of key=>value pairs with no specific order. How you added to the list does not impact how the values are stored or returned to you.

You can sort a hash, but it won't be in the order you added them. Here is one link that can tell you a little about hashes, but I would seriously consider getting the "Learning Perl" book (Llama book) and reading its information as it is quite helpful.

Regards,

Jeff
Dec 10 '07 #4
KevinADC
4,059 Expert 2GB
use an array to store the hash keys in the order you want to display them:

Expand|Select|Wrap|Line Numbers
  1. my @keys = ('Payment Received', 'Order Under processing', 'Shipped From WareHouse', 'Courier', 'Delivered')
  2.  
  3. my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D); 
  4.  
  5. foreach my $key (@keys) {
  6.    print "$key = $status{$key}\n";
  7. }
Dec 11 '07 #5
mehj123
55
use an array to store the hash keys in the order you want to display them:

Expand|Select|Wrap|Line Numbers
  1. my @keys = ('Payment Received', 'Order Under processing', 'Shipped From WareHouse', 'Courier', 'Delivered')
  2.  
  3. my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D); 
  4.  
  5. foreach my $key (@keys) {
  6.    print "$key = $status{$key}\n";
  7. }
Wow... this is clean and simple to understand also..... A lot better than my 1,2,3.... :) Thanks
Dec 11 '07 #6
KevinADC
4,059 Expert 2GB
Wow... this is clean and simple to understand also..... A lot better than my 1,2,3.... :) Thanks
Using numbered hash keys is really sort of using the wrong tool for the job, you may as well just use an array and use the numbers as the index value of each element of the array and then you automatically have an ordered list. Then call each array element using its index number instead of a numbered hash key. But you could use numbers for the hash keys if you really wanted to.
Dec 11 '07 #7
ntraj
1
hey
first u say clearly wat u want?

y u confused?
Dec 14 '07 #8
KevinADC
4,059 Expert 2GB
hey
first u say clearly wat u want?

y u confused?
Clearly? Look at what you posted, it's about as clear as mud. If you wish to particpate and help, please do, but posting dumb comments written like you are talking to your myspace "friends" are not welcome.
Dec 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Carl S. in 't Veld | last post by:
I am trying to generate tiger tree hashes the same way as directconnect does, but I am failing. I checked the output from php with the reference vectors from...
0
by: Jonathan Hayward | last post by:
I'm trying to have a client open a socket, send two pickled hashes, and receive a pickled object (I'm also writing the server, to listen, read two pickled hashes, and send a pickled object). I've...
8
by: Ben Holness | last post by:
Hi All, I want to create a hash array, based on values in a database. Basically, I want a hash array for each database key and I want to use a sub to get the hash array, but I am having a great...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
8
by: MuZZy | last post by:
Hi, Why for god sake they change implementation of String.GetHashCode() from ..NET 1 to .NET 2? We were storing some user passwords in hashcode, now we can't upgrade those clients with .NET 2...
2
by: MartyNg | last post by:
I am running a system that has both Classic ASP applications and a smattering of ASP.NET applications. We want to store passwords on a SQL Server table as their MD5 hashes. What is the safest...
5
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight...
7
numberwhun
by: numberwhun | last post by:
Hello all! I am working on trying to figure out how to print out a hash of hashes. How the hash in the code is defined below is very similar to how the hash is defined in the script I am...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.