So I have a loop where I am printing out values to a file. The only problem is, I only ever get the last print of the loop. For example, if this were what is being printed in my loop:
1 1
2 2
3 3
All my file would have would be 3 3 as the first and only line. Here is my code:
open my $f, '>', 'file.txt';
#$node_id set above somewhere...
for my $key (keys %{$todo->{$node_id}->{'urls_hash'}}) {
print $f "$node_id $todo->{$node_id}->{'urls_hash'}->{$key}->{'domain_id'}\n";
}
close $f;
I’ve tried using $| = 1 to flush, but that didin’t seem to do anything. I was hoping maybe someone had run into this before. And sorry for the hash of hash of…etc. I know it’s messy 🙂
Your question says
#$node_id set above somewhere.... From your expected output:It’s hard to say without the code which is setting
$node_idbut it looks like you’re reopening the file each time you increment$node_id. Is there another loop you need to move theopenoutside?