in reply to Writing to a new file with a dynamically created name

Hope this helps:

$chunklookup = "Datafile.txt"; open(DGRAB, $chunklookup) || die ("Could not open $chunklookup (DATA T +O GRAB) \n"); while (<DGRAB>) {push(@datain, $_)};close DGRAB; foreach @datain {$currentline =$_; @linesplitfetch = split(/,/, $currentline); open (MYFILE6, '>@linesplitfetch[0].html')|| die ("Could not open @lin +esplitfetch[0].html \n"); print MYFILE6 "$currentline here ";close(MYFILE6); } # foreach @datain

Replies are listed 'Best First'.
Re^2: Writing to a new file with a dynamically created name
by chromatic (Archbishop) on Feb 21, 2011 at 03:19 UTC

    That code would be much better as:

    use Modern::Perl; use autodie; open my $in_fh, '<', 'Datafile.txt'; while (<$in_fh>) { my ($outfile, @extra) = split /,/, $_; open my $out_fh, '>', "$outfile.html"; print $out_fh @extra; }

    ... and you could even golf it further, but that's a decent balance of simplicity and clarity and safety.