Help On Hashes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    Help On Hashes

    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

    [CODE=perl]my %status=('Payme nt Received'=>PR,' Order Under processing'=>'U nder processing','Sh ipped From WareHouse'=>SWH ,Courier=>C,Del ivered=>D);

    or $key(keys %status)
    {
    print "$key \t $status{$key}";
    print "\n";
    }
    [/CODE]
    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
  • mehj123
    New Member
    • Aug 2007
    • 55

    #2
    Originally posted by rajiv07
    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

    [CODE=perl]my %status=('Payme nt Received'=>PR,' Order Under processing'=>'U nder processing','Sh ipped From WareHouse'=>SWH ,Courier=>C,Del ivered=>D);

    or $key(keys %status)
    {
    print "$key \t $status{$key}";
    print "\n";
    }
    [/CODE]
    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
    Code:
    1=>PR,
    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

    Comment

    • rajiv07
      New Member
      • Jun 2007
      • 141

      #3
      Thank U mehj123.Let me try and i will report the status to u.

      regards
      Rajiv

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by rajiv07
        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

        [CODE=perl]my %status=('Payme nt Received'=>PR,' Order Under processing'=>'U nder processing','Sh ipped From WareHouse'=>SWH ,Courier=>C,Del ivered=>D);

        or $key(keys %status)
        {
        print "$key \t $status{$key}";
        print "\n";
        }
        [/CODE]
        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

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          use an array to store the hash keys in the order you want to display them:

          Code:
          my @keys = ('Payment Received', 'Order Under processing', 'Shipped From WareHouse', 'Courier', 'Delivered')
          
          my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D); 
          
          foreach my $key (@keys) {
             print "$key = $status{$key}\n";
          }

          Comment

          • mehj123
            New Member
            • Aug 2007
            • 55

            #6
            Originally posted by KevinADC
            use an array to store the hash keys in the order you want to display them:

            Code:
            my @keys = ('Payment Received', 'Order Under processing', 'Shipped From WareHouse', 'Courier', 'Delivered')
            
            my %status=('Payment Received'=>PR,'Order Under processing'=>'Under processing','Shipped From WareHouse'=>SWH,Courier=>C,Delivered=>D); 
            
            foreach my $key (@keys) {
               print "$key = $status{$key}\n";
            }
            Wow... this is clean and simple to understand also..... A lot better than my 1,2,3.... :) Thanks

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by mehj123
              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.

              Comment

              • ntraj
                New Member
                • Dec 2007
                • 1

                #8
                hey
                first u say clearly wat u want?

                y u confused?

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by ntraj
                  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.

                  Comment

                  Working...