in reply to Tring to write script to add and get employee detials to hash

Have a look at "hashes of arrays" in perldsc, that gives lots of code examples on how to access data structures like that, and see also perlreftut. For example:

my %database; my $employee_id = 961; my $employee_name = "Anonymous Monk"; my $employee_salary = "fun"; my $employee_ph = 5.5; my @employee_details = ($employee_name,$employee_salary,$employee_ph); $database{$employee_id} = \@employee_details; print "$database{961}[0]: $database{961}[1]\n"; __END__ Anonymous Monk: fun

(Why is "\n" part of the employee's details in your code?)

But then again, this kind of data seems more appropriately stored in a hash of hashes (also described in perldsc):

my %database; my $employee_id = 961; $database{$employee_id} = { name => "Anonymous Monk", salary => "fun", ph=> 5.5 }; print "$database{961}{name}: $database{961}{ph}\n"; __END__ Anonymous Monk: 5.5

Replies are listed 'Best First'.
Re^2: Tring to write script to add and get employee detials to hash
by yedukondalu (Acolyte) on Mar 27, 2015 at 13:58 UTC

    Thanks for the reply. How can i store this hash into a text file and how to access the hash from text file using file handle

      Does the text file need to be in a particular format? If not, you're free to choose any serialization format you like, be it XML, YAML, JSON, or something else. Personally, I might recommend YAML, as it maps to Perl data structures better than JSON and is less verbose than XML. See for example YAML::XS.

        "[YAML] maps to Perl data structures better than JSON ..."

        I was not aware of that. I do recall having some issues with de-serializing anonymous subs into Perl data from JSON text, but i would like for you to give more details as to why you have this opinion, please. In the meantime, i found this link which has more interesting perspectives on which to use: http://stackoverflow.com/questions/1876735/should-i-use-yaml-or-json-to-store-my-perl-data

        I myself have opted to use JSON for feeding information to programs (robots) and YAML for feeding information to humans. :) I also try to keep my data simple, which means not having to worry about anonymous sub references. I strive to keep my data in my data and my code in my code.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)