in reply to Copying files from one file to another file

my @arr= nv.txt; my (%hash, $key, $val); foreach (@arr) { $pre1="\$hash{$key}"; #previous key of the loop $pre2=$val; #previous value of the loop ($key, $val) = split (/:/,$_,2); if($pre1 eq "\$hash{$key}") #if previous and current key same { $hash{$key}= $pre . '\n' . $val; #concatenate } $hash{key}=$val; print "\$hash{$key}={$vaal}\n"; }
input file nv.txt has files in the form of a array like this, test/right/case1:12: //comment test/right/case1:344: //comment test/wrong/case3:123: //comment
code> output test/right/case1:12 : //comment :344: //comment test/wrong/case3:123: //comment

If the path address is same, then values should be saved in one key(path address) else different key. I think I'm not able to concatenate now or if there should be any other solution or modification, help me.

Replies are listed 'Best First'.
Re^2: Copying files from one file to another file
by poj (Abbot) on Feb 20, 2018 at 11:00 UTC

    What is the name of the files you want the values saved to ?

    #!/usr/bin/perl use strict; use warnings; my %hash=(); # input my $inputfile = 'nv.txt'; open IN, '<',$inputfile or die "Could not open $inputfile : $!"; while (<IN>) { my ($key, $val) = split (/:/,$_,2); $hash{$key} .= $val; } # output for my $key (keys %hash){ my $outputfile = "output_$key"; $outputfile =~ s{/}{-}g; print "Writing to $outputfile\n"; open OUT,'>',$outputfile or die "Could not open $outputfile : $!"; print OUT $hash{$key}; close OUT; } __DATA__ test/right/case1:12: //comment test/right/case1:344: //comment test/wrong/case3:123: //comment
    poj

      I want to save each key in one text file having the key-value's in each text file.

        key-value's in each text file.

        Yes but what is the name of that text file, is it the same as the key ?

        how to create only "right" or "wrong" as filenames? and save them in one directory? thanks for the help