Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 118,498 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,066 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



load hash from file doesnt work, inline does - why?

 
Reply to this topicStart new topic

load hash from file doesnt work, inline does - why?, load hash from file

AWhig
post 16 Jun, 2008 - 03:26 AM
Post #1


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 2

I want to look up multiple values from one key. I create two hashes loaded from a separate file. The test code that follows shows - I think - that two hashes are loaded because I can print them from a foreach structure - but I cannot query the hashes directly - Perl prints "Use of uninitialized value in concatenation (.) or string at testhash.pl line 22." , and lines 25 and 28.

I believe that I could use a single hash where keys pointed to array references. But I won't try that before I can make this apparently simple case work. Thanks for any suggestions.

CODE

# external file "test1"
# file values for hash
key1    value1 A    value1 B
key2    value2 A    value2 B
key3    value3 A    value3 B
# end file test1

# <<get data from file>>
    $parameter_file = "test1";
    open (INFILE, $parameter_file) || die "Couldn't open $parameter_file";
    LINE: while (<INFILE>)
    {
        next LINE if /^#/;
        chomp;
        @test = split/\t/;
        $test_hashA {$test[0]} = $test[1];
        $test_hashB {$test[0]} = $test[2];
    }
    close INFILE;
# <<show data is loaded>>
    print "\n\nforeach from hash\n";
    print "key\tkey\tvalue\n";
    foreach $key (sort keys %test_hashA) {
        print "$key\t$test_hashA{$key}\t$test_hashB{$key}\n";
    }
# <<try to access hashes>>
    # retrieve value for a known key
    print "lookup values\n";
    print "test1:\t$testhash1{key1}\n";
    # does it work for a key as scalar?
    $nn = "key1";    
    print "$nn\ttest_scalar:\t$test1{$nn}\n";
    # problem with print?  assign value to string
    $test_print = $test1{$nn};
    print "print-test:\t$test_print\n";
    print "existentce test\n";
    if (exists $test1{key1}) {print "found\n";} else {print "not found\n"};
User is offlineProfile CardPM

Go to the top of the page


KevinADC
post 16 Jun, 2008 - 10:20 AM
Post #2


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 200



Thanked 2 times

Dream Kudos: 50
My Contributions


it appears that you create two hashes from reading the file:

%test_hashA
%test_hashB

but later in the code you are using %testhash1 and %test1
User is offlineProfile CardPM

Go to the top of the page

AWhig
post 16 Jun, 2008 - 10:59 AM
Post #3


New D.I.C Head

*
Joined: 16 Jun, 2008
Posts: 2

QUOTE(KevinADC @ 16 Jun, 2008 - 10:20 AM) *

it appears that you create two hashes from reading the file:

%test_hashA
%test_hashB

but later in the code you are using %testhash1 and %test1



Yes ... was trying to make the simplest code to illustrate the problem & tripped myself. I don't believe the name mismatch was in original. Sorry to waste your critical thinking time. Will see if I can repost a working example. 'use strict' would have caught this, I guess. Thanks for your reply.
User is offlineProfile CardPM

Go to the top of the page

KevinADC
post 16 Jun, 2008 - 03:13 PM
Post #4


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 200



Thanked 2 times

Dream Kudos: 50
My Contributions


if you use the correct hash names it looks like it should work. A problem might be the \t delimiter. \t (tabs) don't seem to work very consistently, I would change that delimiter if you can.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/11/08 07:35AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month