How can I set several values to hash in loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhavanirayala
    New Member
    • Jul 2007
    • 25

    How can I set several values to hash in loop

    Hi,

    I have to set some values based on some conditions to hash object.
    How can i set?
    I am using the following code.

    [code=perl]
    if($attrs{"Coll Type"} eq "A")
    {
    %A_hash = ($attrs{"State" },$attrs{"Team" });
    }
    [/code]

    I am parsing one xml file and based on the CollType I am assigning the state and team values to A hash.
    But finally i get the last key value only.and its size is always 1.
    Hash is not growing as needed.
    How can i achieve this?

    Thanks in advance.

    Regards,
    Bhavani
    Last edited by numberwhun; Oct 3 '07, 11:57 AM. Reason: add code tags
  • mehj123
    New Member
    • Aug 2007
    • 55

    #2
    Hi Bhavani,

    You have to use something like this
    [code=perl]
    $A_hash{some_va r} = ($attrs{"State" },$attrs{"Team" });
    [/code]

    use a unique key each time....
    Last edited by numberwhun; Oct 3 '07, 11:57 AM. Reason: add code tags

    Comment

    • bhavanirayala
      New Member
      • Jul 2007
      • 25

      #3
      Hi MehJ,

      As you told I modified my code like below.

      [code=perl]
      if($attrs{"Coll Type"} eq "A")
      {
      ## %A_hash = ($attrs{"State" },$attrs{"Team" });
      $A_hash{$attrs{ "State"}} = ($attrs{"State" },$attrs{"Team" });
      }
      [/code]

      Then I am getting the following error.
      Useless use of hash element in void context at test_report.pl line 43.

      Please give any advice.
      Last edited by numberwhun; Oct 3 '07, 11:56 AM. Reason: add code tags

      Comment

      • mehj123
        New Member
        • Aug 2007
        • 55

        #4
        HI Bhavani,

        U r getting that error because you are using the same variable as key and value.. what i mean is suppose
        Code:
         $attrs{"State"} = "abc" 
        $attrs{"Team"} = "def"
        now when you use this statement
        Code:
         $A_hash{$attrs{"State"}} = ($attrs{"State"},$attrs{"Team"});
        what happens is
        Code:
        $A_hash{abc}={abc,def}
        there is no need for this as you are storing data repeatidely... you can just give
        Code:
        $A_hash{$attrs{"State"}} = $attrs{"Team"};
        hope this helps you..

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          With the exception of the last post in this thread, I have had to edit the other posts to add code tags around the code.

          In the future, please alleviate the work done by the Moderators by putting code tags around your code in your posts.

          If you are unsure of how to use them, please see the "REPLY GUIDELINES" to the right of the message window when replying or posting.

          Regards,

          Jeff

          Comment

          • bhavanirayala
            New Member
            • Jul 2007
            • 25

            #6
            Hi MehJ,

            you are right.Thankyou very much.
            Now it is working fine.

            And I have some other requirement also.
            that is
            I have one hash which contains letters as key and team as value.
            means
            a_hash={"ABCDEF GH","Team1","De fault","Team2"}
            Means if the first letter of a customer starts with any one of ABCDEFGH then the team is Team1
            otherwise the team is Team2.
            My problem is how can i check the first name letter with the key?
            For example:
            Customer name is:Bhavani
            then the team is Team1.
            If Customer name is:Vijaya
            then the team is Team2.

            how can we get the first letter of customer name and how can we compare with the key to get the team value?

            regards,
            Bhavani.

            Comment

            • mehj123
              New Member
              • Aug 2007
              • 55

              #7
              Hi..
              I dont know if i m understanding ur requirements clearly ..try this..
              you can get the first letter of a string by splitting like
              Code:
              @arr = split(//,$el);
              ...
              then you can compare like this.....
              Code:
               foreach(keys %a_hash) 
              {
              if(key =~ /$arr[0]/)
              {
              	 $team = $a_hash{key};
              }
              }
              and get the appropriate team value

              Comment

              Working...