Gossamer Forum
Home : General : Perl Programming :

Returning Hash References

Quote Reply
Returning Hash References
 
HI All ,

Below is a quite simple code , which reads data from a configuration file and loads it into hash.
The Hash loading is being done in the function load_profile_hash and accessed from the main function,

The configuration file is of the type
A=100
B=300


Code:
#!/usr/bin/perl
use Data::Dumper;

#Execute the main function


my $profile_mapper_file="/tmp/varun/profile_map.txt";
sub load_profile_hash()
{
my %profile_mapping;
# Read the mapping file for loading the Hash
open(PROFILE_MAPPER, "<$profile_mapper_file") || die "I/O Success - Cannot process $profile_mapper_file file";
my %profile_mapping = map { chomp; split /=/ } <PROFILE_MAPPER>;
print Dumper(\%profile_mapping);
return (\%profile_mapping);
}

sub main()
{
my $profile_mapping_ref;
#load the CDB profile - ICC profile
$profile_mapping_ref=load_profile_hash();
print "@@@@\n";
print $$profile_mapping_ref{"A"};
print "@@@@\n";
}
main();

However , in the main function , the value of 'A' fails to print.


Thanks
Varun
Quote Reply
Re: [gpta_varun] Returning Hash References In reply to
This works (few minor tweaks);

Code:
#!/usr/bin/perl

use Data::Dumper;

#Execute the main function


my $profile_mapper_file="./test.cfg";
sub load_profile_hash()
{
my %profile_mapping;
# Read the mapping file for loading the Hash
open(PROFILE_MAPPER, "<$profile_mapper_file") || die "I/O Success - Cannot process $profile_mapper_file file";
my %profile_mapping = map { chomp; split /=/ } <PROFILE_MAPPER>;
return \%profile_mapping;
}

sub main()
{
my $profile_mapping_ref;
#load the CDB profile - ICC profile

$profile_mapping_ref = load_profile_hash();
print "@@@@\n";
print $$profile_mapping_ref{'A'};
print "@@@@\n";
}
main();

...gives:

Code:
linkssql@undev ~ $ perl test.cgi
@@@@
100@@@@

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Returning Hash References In reply to
Quote:
Don't know why but in my case I just get

@@@@
@@@@

:(
Quote Reply
Re: [gpta_varun] Returning Hash References In reply to
Mmm, what happens when you do a Dumper of the values:

$profile_mapping_ref = load_profile_hash();
print Dumper($profile_mapping_ref);

?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Returning Hash References In reply to
 
Code:

$VAR1 = {
', 'A' => '100
' 'B' => '200
};
Quote Reply
Re: [gpta_varun] Returning Hash References In reply to
What is the exact value of your .cfg file? My test one was:

Code:
A=100
B=300

..the output from that dump makes me think yours is different?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Returning Hash References In reply to
A=100
B=200
Quote Reply
Re: [gpta_varun] Returning Hash References In reply to
Mmm, my dump looks like:

Code:
FOO: $VAR1 = {
'A' => '100',
'B' => '300'
};


Not sure where the bits in red are coming from with your dump:

$VAR1 = {
', 'A' => '100
' 'B' => '200
};

The code I gave you defiantly works (I just re-tested it) ...

Code:
#!/usr/bin/perl

use Data::Dumper;

#Execute the main function


my $profile_mapper_file="./test.cfg";
sub load_profile_hash()
{
my %profile_mapping;
# Read the mapping file for loading the Hash
open(PROFILE_MAPPER, "<$profile_mapper_file") || die "I/O Success - Cannot process $profile_mapper_file file";
my %profile_mapping = map { chomp; split /=/ } <PROFILE_MAPPER>;
# print "FOO 1". Dumper(\%profile_mapping) . "\n\n";
return \%profile_mapping;
}

sub main()
{
my $profile_mapping_ref;
#load the CDB profile - ICC profile

$profile_mapping_ref = load_profile_hash();
print "@@@@\n";
print $$profile_mapping_ref{'A'};
print "@@@@\n";
}
main();

..and in test.cfg:

Code:
A=100
B=300

So must be something you are doing wrong Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!