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

hi, i have a hash variable, which i want to convert to a string containing all the data from the hash. how do i do that ? could u please tell me what i'm doing wrong in this code : use IO::File;
$filehandle = new IO::File;
$filehandle->open("<$file"); - this is a text file
readfile(\*$filehandle); - calls many subroutines which populate a hash variable called %seg000

while (($key,$value) = each %seg000) {
$newstr .= $key . ":" . $value; -newstr is scalar
}

print $newstr; - this shows that the data is present in newstr, but when i try to write it to another text file,nothing happens :
$filehandle2 = new IO::File;
$filehandle2->open(">testhash.txt");
$filehandle2->close();
thanks

Replies are listed 'Best First'.
Re: converting a hash to a string
by merlyn (Sage) on Sep 23, 2000 at 05:21 UTC
(jcwren) RE: converting a hash to a string
by jcwren (Prior) on Sep 23, 2000 at 07:28 UTC
    If your code is accurate, the problem is you're not printing anything to the file. You'd need something more like this:
    $filehandle2 = new IO::File; $filehandle2->open (">texthash.text") or die $!; print $filehandle2 $newstr; $filehandle2->close ();
    --Chris

    e-mail jcwren
Re: converting a hash to a string
by fundflow (Chaplain) on Sep 23, 2000 at 22:29 UTC
    Why are you using File::IO?

    Since you are writing strings, why not
    open(F,">outfile") or die; while (($key,$value) = each %seg000) { print F $key , ":" , $value; }
    or something like that?