Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

printing to filehandle contained in a hash

by deadpickle (Pilgrim)
on Feb 20, 2010 at 01:00 UTC ( [id://824298]=perlquestion: print w/replies, xml ) Need Help??

deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

Trying to print to a filehandle that is stored in a hash, but I get this error when I try to print to it: Can't use string ("GLOB(0x9547728)") as a symbol ref while "strict refs"
while (my ($fileh, $repoch) = each(%radar)){ if ($ltgepoch == $repoch) { if ($temp[3] eq "") { print $fileh "$temp[2],$temp[4],@date,@time\n"; print "$temp[0] $temp[1] $temp[2] $temp[4]\n"; } elsif ($temp[4] eq "") { print $fileh "$temp[2],$temp[3],@date,@time\n"; print "$temp[0] $temp[1] $temp[2] $temp[3]\n"; } } }
Ideas?

Replies are listed 'Best First'.
Re: printing to filehandle contained in a hash
by ikegami (Patriarch) on Feb 20, 2010 at 01:44 UTC
    The pattern you are using,
    while (my ($fileh, $repoch) = each(%radar)){ if ($ltgepoch == $repoch) { ... } }
    defies the entire purpose of using a hash. The above should be
    if (exists($radar{$ltgepoch})) { ... }
    which means your keys and your values are backwards. Switch them around and it also solves the problem you asked about.
Re: printing to filehandle contained in a hash
by jethro (Monsignor) on Feb 20, 2010 at 01:17 UTC

    You can store a filehandle as hash value, but you can't store it as hash key. As key the filehandle gets stringified and loses any special properties

    #!/usr/bin/perl use warnings; use strict; my %f; open($f{r},'>','test.txt') or die; my $fhvalue=$f{r}; print '<',ref $fhvalue,">\n"; my %i; $i{$fhvalue}= 1; my ($fhkey)= keys %i; print '<',ref $fhkey,">\n"; #prints <GLOB> <>

    UPDATED with the code example

Re: printing to filehandle contained in a hash
by rubasov (Friar) on Feb 20, 2010 at 01:18 UTC
    All of your hash keys (just the keys, not the values) will be stringified in perl5, so you cannot store a filehandle (or a reference) in a hash key (a hash key is a string, not a scalar variable for general use). Try to store these somewhere else, depending on your data structure for instance:
    %radar = (some_id => [$fileh, $repoch], some_other_id => [$fileh2, $re +poch2], ...)
Re: printing to filehandle contained in a hash
by crashtest (Curate) on Feb 20, 2010 at 01:39 UTC

    Hash keys are stringified. When you used the file handle reference as hash key, perl turned it into a string representation of the reference. The reference itself is gone!

    Perhaps a hash wasn't the right choice for your data structure. Since all you're doing is iterating over your data anyway, perhaps an array of arrays (AoA) would be more useful. Untested:

    # Somewhere earlier... my @radar = (); push @radar, [$fileh, $repoch] for (1..5); # ... then: for (@radar){ my ($fileh, $repoch) = @$_; # ... continue as before. }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://824298]
Approved by crashtest
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found