Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading file names into perl and adding to their names

Status
Not open for further replies.

djnsh

Programmer
Aug 1, 2003
4
GB
My problem, although appearing simple on the surface, does not seem as straight forward as I first thought.

I have a directory containing files, with each file having the same start such as apples_45.4554-34, apples_56.4554-34, and so on. There are hundreds like this which I generated using another perl program.

The same directory also contains similar file names, however different starting positions such as bananas_45.4554-34, bananas_56.4554-34.

What I am trying to do is merge the two files together which have the same end to produces an output file such as one called oranges_45.4554-34 which contains the contents of both apples_45.4554-34 and bananas_45.4554-34.

I can do the bringing together bit as I have tried it on one example when I specified the complete file name of each of the two components like in my example above, however I cannot work out how to do it automatically for every file with the apples_* and bananas_* start for every end without specifing each file separately which is impossible.

Help!
 
I should leave this for the more clever programmers here, but since no one has replied yet, this might get you started..

Code:
@files = glob("*.*");

foreach $fileName (@files){ 
    
    ($leftSide,$rightSide) = split("_",$fileName);
    
    push (@arrRightSide,$rightSide);
}

# from Effective Perl Programming:
@arrRightSide = sort keys %{ { map { $_, 1 } @arrRightSide } };

the last line will eliminate any duplicates from @arrRightSide, then you could loop through it and use pattern matching to grab the files that you want to combine.
There's probably a simple way to do this, but you're right.. it's trickier than it sounds. If I get a chance, I'll see if I can finish this..

-jt
 
Code:
my $some_dir = '/home/dir/';
opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
my @files = grep { /^apple/ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

for my $file1 (@files)
{
  (my $file2 = $file1) =~ s/^apple/banana/;

  #now you have both the file names ("$some_dir/$file1" & "$some_dir/$file2"), so concat them here...
}


---
cheers!
san
pipe.gif


"The universe has been expanding, and Perl's kind of been expanding along with the universe" - Larry Wall
 
A word of caution for above.. I just made the code AS-IS without checking, so make a backup of your file before testing it.

---
cheers!
san
pipe.gif


"The universe has been expanding, and Perl's kind of been expanding along with the universe" - Larry Wall
 
Thanks 133tcamel for your reply - I have my program reading all the files in the directory and can produce an output file with the all the different extensions which exist with a common start as shown below:

e.g: apples234.564 -> use 234.564 part -> which produces bananas234.564

Apologies if my explanation of my problem has not been very specific however my real problem which I have been trying to solve is that I need two files combined into one like shown below without one writing over the other when each is opened. I have tried to summarise this paradox below:

apple456.334 => extract contents of file
} add contents of both files to produce a file called bananas456.334
orange456.334 => extract contents of file

Any help is greatly appreciated - I keep telling myself surely it can't be that difficult!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top