Perl Style: Use Hashes of Records, not Parallel Arrays

  • Learn to use hashes of records, and maintain array or hashes of these records, rather than using parallel arrays. Don’t do this:

        $age{"Jason"} = 23;
        $dad{"Jason"} = "Herbert";
    

    When you should do:

        $people{"Jason"}{AGE} = 23;
        $people{"Jason"}{DAD} = "Herbert";
    

    Or even: (note use of for here)

        for $his ($people{"Jason"}) {
            $his->{AGE} = 23;
            $his->{DAD} = "Herbert";
        }
    

    But think very carefully before writing this:

        @{ $people{"Jason"} }{"AGE","DAD"} = (23, "Herbert");
    

Forward to Use $_ in Short Code
Back to Use Hashes for the First Time
Up to index

Copyright © 1998, Tom Christiansen All rights reserved.

Tags

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub