in reply to Creating a Stringified Variable from a Data Structure

Hmm, I'm not sure I understand your question. Fixing the syntax errors in your code (s.below) makes it print out exactly what you seem to expect.

my %hash = ( 'mus09r' => [ '0,-464,AAACCATCTTGAAAC', '0,-350,GGTTCAGGATGGTTT', '0,-75,AAAACATCGTGACAC' ], ); while (my ($file,$val) = each %hash) { print ">dataset\n"; print "$file\n"; print ">instances\n"; print join("\n",@{$val}),"\n"; }

If what you're looking to do is put the text into a variable instead, simply append to that variable instead of printing directly.

use Data::Dumper; my %hash = ( 'mus09r' => [ '0,-464,AAACCATCTTGAAAC', '0,-350,GGTTCAGGATGGTTT', '0,-75,AAAACATCGTGACAC' ], ); my $string; while (my ($file,$val) = each %hash) { $string.=">dataset\n". "$file\n". ">instances\n". join("\n",@{$val}). "\n"; } print Dumper $string;

Update: Appending to string instead of replacing it every hash iteration, as per Mandrake's suggestion (note to self: Duh!).


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^2: Creating a Stringified Variable
by Mandrake (Chaplain) on Dec 14, 2005 at 09:45 UTC
    One suggestion. Use the dot (.) operator to append since the hash can have more that one key-value pair.
    my $VAR1 ; while (my ($file,$val) = each %hash) { $VAR1 .= ">dataset\n". $file."\n". ">instances\n". join("\n",@{$val})."\n"; } print $VAR1;
    Thanks