in reply to Re: Using my and library....
in thread Using my and library....

Thanks, Zaxo!

I tried to do what you advised but I seem to be missing something. I've pasted the code here and hope you can point out to me where I have gone wrong:

# hash.lib # in the library sub gethash { my $hash_ref; open(FILE, "data.txt") || die "Can't open data - $!\n"; # dick:98 # robert:76 # mary:68 chomp(my @lines = <FILE>); close(FILE); foreach (@lines) { my ($name,$score) = split /:/, $_; $hash_ref->{ 'name' } = $name; $hash_ref->{ 'score' } = $score; } return( $hash_ref ); } # script1.pl # some code in another script eval { ($0 =~ m,(.*)/[^/]+,) && unshift (@INC, "$1"); require 5.001; require "hash.lib"; }; my $key_value = gethash(); # I want to be able to print the values here....
I hope you can enlighten me :)

kiat

Replies are listed 'Best First'.
Re: Using my and library....
by tadman (Prior) on Aug 17, 2002 at 13:15 UTC
    It's probably better to name your libraries '.pm' instead of '.lib'. Not only can they then be 'use'd properly, but it's really better to conform to existing standards than to invent your own.

    One of the problems with eval is that it can hide errors. Don't forget to check $@ for anything you might have missed.

    Also, in the context of your gethash function you can use an actual hash, and return a reference to it. Saves having to dereference repeatedly. With a tiny bit of tweaking, you get:
    # hash.lib # in the library sub gethash { my %hash; open(FILE, "data.txt") || die "Can't open data - $!\n"; # dick:98 # robert:76 # mary:68 chomp(my @lines = <FILE>); close(FILE); foreach (@lines) { my ($name,$score) = split /:/, $_; $hash{name} = $name; $hash{score} = $score; } return \%hash; }
    More tweaking produces:
    # hash.lib # in the library sub gethash { open(FILE, "data.txt") || die "Can't open data - $!\n"; my %hash = map { (split(/:/))[0,1] } chomp(<FILE>); close(FILE); return \%hash; }
Re: Re: Re: Using my and library....
by Anonymous Monk on Aug 17, 2002 at 13:01 UTC
    Always check $@ after an eval.

    A million and one things can go wrong, and you would never know it because eval traps errors. Odds are that you need to add "1;" at the end of your library, and that lack is causing your require to fail, but you didn't get an error message so you didn't get any clues about that.

Re: Re: Re: Using my and library....
by smalhotra (Scribe) on Aug 17, 2002 at 14:04 UTC
    From your code, it looks like you are only getting the last entry in your file:
    foreach (@lines) { my ($name,$score) = split /:/, $_; $hash_ref->{ 'name' } = $name; # replaces the old 'name' entry $hash_ref->{ 'score' } = $score; # replaces the previous 'score' + entry }
    I might suggest using an array ref for each entry, or maybe an array of hashrefs or a hash of hashrefs. Examples:
    ##Array of Hashrefs: my @entries; for (@lines) { # as someone once said ~if you plan to explicitly use $_ everywhere, + you might as well give it a more meaningful name or not use it at al +l.~ my ($name,$score) = split /:/; $hash_ref->{ 'name' } = $name; $hash_ref->{ 'score' } = $score; push (@entries, $hash_ref); } return( @entries); } ##Hash of Hashrefs: for (@lines) { my ($name,$score) = split /:/; $hash_ref->{ $name }{'name'} = $name; $hash_ref->{ $name }{'score'} = $score; } return($hash_ref); } ##Hash of Arrayrefs: for (@lines) { my ($name,$score) = split /:/; $hash_ref->{ $name } = [$name,$score]; } return($hash_ref); }
    I could come up with several more data structures to store the same data but you get the idea. TMTOWTDI and as I like to always say, see whatever works best with your program and go with that.
      Hi smalhotra,

      Thanks for pointing that out. I didn't occur to me that the old 'name' and 'score' would be replaced. I was looking for something like your hash of hashes solution. Thanks :)

      kiat