Re: Copying files from one file to another file
by davido (Cardinal) on Feb 19, 2018 at 20:14 UTC
|
I'm assuming that the "a[0] -", "a[1] -", and "a[2] -" prefixes on each line of your sample input are not part of the contents of your array, but rather, your demonstration of what element index of the array is being represented, and that the part that comes after that is the contents of your array. Also you were unclear in your explanation on what is supposed to happen with the line number portion of the string contained in each element of the array. Because it was unclear, I'm leaving it out of my demonstrated solution.
use strict;
use warnings;
my @array = <DATA>;
foreach my $element (@array) {
chomp $element;
my ($fname, $line_number, $content) = split /\s*:\s*/, $element;
open my $outfh, '>>', $fname
or die "Cannot open $fname: $!\n";
print $outfh $content; # You didn't tell us what is supposed to ha
+ppen with $line_number.
close $outfh or die "Couldn't close $fname after writing: $!\n";
}
__DATA__
tests/right/case1: 12 : //comment
tests/right/case1: 13 : //comment
test/right/case3: 5 : //comment
The standard behavior of the >> open mode is to create if the file does not exist, and append if it does, so you really don't need to worry about whether you've seen the file before.
If the code in this answer is difficult to follow I encourage you to spend some time with the following Perl documentation, which comes with any full installation of Perl.
| [reply] [d/l] [select] |
|
|
If the path is same, both the lines should be saved in one file and in different files.
| [reply] |
|
|
If the path is same, both the lines should be saved in one file and in different files. [emphasis added]
I don't understand this statement.
Update: I also don't understand why is not possible to provide an exact example of an input file you wish to parse (in <code> tags), less than a dozen lines would seem enough, and also exact examples of the output files produced from the example input file (again, in <code> tags). You've already done the first (maybe — see davido's comments here), so the output files would not seem a much greater step. Please see this. Having exact examples of both input and output might go a long way toward clarifying the situation.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
|
|
Re: Copying files from one file to another file
by AnomalousMonk (Archbishop) on Feb 19, 2018 at 19:16 UTC
|
Hi harishnv. In addition to posting the code you have tried so far (as thanos1983 suggested here), it's very important to post input and output data examples in a downloadable format, i.e., within <code> ... </code> tags. Please see Writeup Formatting Tips and How do I post a question effectively? For example, a dozen or fewer lines of downloadable (i.e., in <code> ... </code> tags) input data would have been far better than the confusing written description given in the OP. By the same token, two or three separate, downloadable (as above) example output files that you would expect to result from the given input example would have been very helpful to anyone who wanted to spend time trying to help you. The slogan for the day: Please help us to help you.
Update: OP updated to add some code tags, unfortunately without much improvement in comprehensibility IMHO.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
Re: Copying files from one file to another file
by thanos1983 (Parson) on Feb 19, 2018 at 16:42 UTC
|
Hello harishnv,
What have you got so far? Can you show us some code that you have tried and failed?
Looking forward to your update. BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
|
|
i'm trying to split the function : and then trying to read the preceeding term of it but i'm not able to do.
| [reply] |
|
|
I have tried doing so much.
I have saved the path address into a key and the rest of the line to the value using split (:) function.
Now, if the key is same how can I append the values to the same key?
| [reply] |
|
|
c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le
"my @lines = (
'foo: 12 : //comment twelve ',
'bar: 13 : //comment thirteen',
'foo: 5 : //comment five',
);
;;
my %hash;
for my $line (@lines) {
my ($k, $v) = split qr{ : \s+ }xms, $line, 2;
push @{ $hash{$k} }, $v;
}
dd \%hash;
"
{
bar => ["13 : //comment thirteen"],
foo => ["12 : //comment twelve ", "5 : //comment five"],
}
push-ing to an @{ ... } array referenced by a hash key $hash{$k} autovivifies an array reference if it did not already exist. See "autovivification" in perlglossary and here.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$hash{$key} = $hash{$key} . $line;
or simply
$hash{$key} .= $line;
poj
| [reply] [d/l] [select] |
|
|
is it good by creating a hash table?
| [reply] |
|
|
One can well imagine that split-ing each line read from the input file and creating some sort of hash table might be involved in the solution of the problem presented in the OP. Unfortunately, I, for one, do not now understand the basic problem well enough to want to make the effort to attempt a solution. (Update: davido, a more patient man than I (at least today), has made a determined effort to provide a helpful solution in the face of a vague specification. ++davido.) Please see this and thanos1983's reply.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] |
|
|
| [reply] |
Re: Copying files from one file to another file
by harishnv (Sexton) on Feb 20, 2018 at 10:26 UTC
|
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.
| [reply] [d/l] [select] |
|
|
#!/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 | [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
|
|