in reply to (jeffa) Re: How to open sub FH when filename is passed to sub?
in thread How to open sub FH when filename is passed to sub?


Thank you so much jeffa!


I do make my sub hash, it gave me all the result, but it still tell me :
Use of uninitialized value at ./lx line 97.
Use of uninitialized value at ./lx line 97.
total 10 times Use of above context.
my sub like:


sub create_hash {
my $myhashname=shift;
my %myhashname=%_;
my @keys=@_;


   unless(open(FH, ">$myhashname") ) {
       print STDERR "Cannot open file\"$myhashname\"\n\n";
        exit;}


   @myhashname{@keys}=@mydnahash{@keys};
    while (($k, $v)=each %myhashname){
    print FH "$k=>$v, "};


    close FH;
    return %myhashname;
}


and I call sub like:

my %myeditedhash= create_hash('myeditedhash',@editedsites, @mydnahash{@editedsites});


print FH "$k=>$v, "}; is line 97.what is wrong? please help.

Replies are listed 'Best First'.
(jeffa) 3Re: How to open sub FH when filename is passed to sub?
by jeffa (Bishop) on May 08, 2002 at 05:01 UTC
    You are welcome, but i am not getting through to you. You need to sit down and learn some basics first. Please read this book: Learning Perl. Repeat. Please read this book: Learning Perl.

    The reason for the warnings is because your hash probably has keys, but no values. Now, your problem is two fold:

    1. the way you are passing arguments to the sub
    2. the way you are receiving the arguments in the sub
    You refuse to show me what your input is and what your desired output should be, so i really can't help you any further with THIS problem. I can show you some examples of how to call functions in Perl, though. Run this code first to see the output, then study the code itself.
    use strict; my @array1 = (1,2,3,4); my @array2 = (5,6,7,8); my %hash = ( key => 'val', foo => 'bar', ); print '=' x 20, "\nwrong way:\n"; wrong1(@array1,@array2); sub wrong1 { my (@a1,@a2) = @_; print "array 1: @a1\n"; print "array 2: @a2\n"; # notice that @a2 is empty } print '=' x 20, "\nright way:\n"; right1(\@array1,\@array2); sub right1 { my @a1 = @{ shift @_ }; my @a2 = @{ shift @_ }; print "array 1: @a1\n"; print "array 2: @a2\n"; # multiple arrays must be passed in as references } print '=' x 20, "\nwrong way:\n"; wrong2(%hash); sub wrong2 { my %hash = %_; while (my($k,$v) = each %hash) { print "$k => $v\n"; } # %_ is not special, use @_ instead - always! } print '=' x 20, "\nright way:\n"; right2(%hash); sub right2 { my %hash = @_; while (my($k,$v) = each %hash) { print "$k => $v\n"; } # a hash is really just a special kind of array # and yes, multiple hashes must be passed as references too } # last - a hash slice my %slice; @slice{@array1} = @array2; print '=' x 20, "\nhash slice:\n"; while (my($k,$v) = each %slice) { print "$k => $v\n"; } # and don't creat hash slices inside an subroutine's # argument list - that is bad bad bad ;)
    Good luck, and please read this book: Learning Perl.

    jeffa

    Cargo Cult Programming - Just say 'NO!'