hey guys

Posted on 2009-04-20
Last Modified: 2012-05-06
i want to loop over hash1 and check the keys of this hash and compare them to the keys of hash 2.. if hash1 does not have a key from hash 2 i need to copy this key to hash1 with its value.
Question by:cucugirl
    LVL 39

    Accepted Solution


    foreach (keys %hash2) {
        next if exists($hash1{$_});

    Open in new window

    LVL 84

    Expert Comment

    %hash1 = (%hash2,%hash1);
    LVL 84

    Expert Comment

     %hash1 = (%hash2,%hash1)
    performs the same function, albeit without an explicit loop.
    if you prefer an explicit loop, and you have perl5.10 or later, you might use
      $hash1{$_} //= $hash2{$_} for keys %hash2;  #assuming existing values are defined
    prior to 5.10, if the values in %hash1 are all true, you could do
       $hash1{$_} ||= $hash2{$_} for keys %hash2;
    otherwise you could
      exists $hash1{$_} or $hash1{$_} = $hash2{$_} for keys %hash2;

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now