Greetings perl monks,
Today my dilemma is reorganizing files. I actually have both a problem and a question. Say I'm starting with 2 files R1 and R2, as follows:and I want to rearrange them so instead I have filesR1-something.txt R2-something.txt DESCRP a DESCRP a 2 3 3 1 DESCRP b DESCRP c 3 5 4 9
I have the files R1, R2 stored in fileArray and I think this should output them the way I would like:a.txt b.txt c.txt R1a R1b R2c 2 3 5 3 4 9 R2a 3 1
foreach my $file (@{$fileArray}) { open (INPUT, "< $file") || die "Could not open $file\n"; while (<INPUT>) { if ($_ =~ /^DESCRP/) { #get name of element chomp (my $newFileName = substr($_, 7)); #open/create file of that name open OUTPUT, ">> $newFileName.txt" || die "Could not open +$_.txt\n"; #print the specific elements name print OUTPUT substr($file,0,2).$newFileName."txt\n"; } else { #or print the values following the element print OUTPUT $_; } } close OUTPUT; close INPUT; }
First, my problem is that when I try to print the values after each element, my OUTPUT handle is closed because it is out of scope from where I open it, and I am not sure how to keep it open.
My question is: Do you have a more efficient/aesthetic way of doing this?
Thanks for your time :)-Thomas
P.S. the data sets I will actually be working with are much larger (1000s of lines and a dozen 'R_' files), but they will be generally like this.
In reply to Reorganizing file contents by tomdbs98
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |