--- "John W. Krahn" <[EMAIL PROTECTED]> wrote: > Peter Lemus wrote: > > > > I have a file "1st file) that reads... > > one > > two > > three > > four > > five > > > > Anotherone "2nd file"that reads: > > day > > weeks > > months > > quarter > > year > > century > > > > I need to read the 2nd file and add the text from it > > to every word of the first 1st file. So it will look > > something like: > > > > oneday > > oneweek > > onemonth > > onequarter > > oneyear > > onecentury > > twoday > > twoweek etc..etc. > > > > Pleas give me an example on how I can accomplish this. > > > Here is one way to do it: > > #!/usr/bin/perl -w > use strict; > > open FILE, 'file1.txt' or die "Cannot open 'file1.txt': $!"; > chomp( my @data1 = <FILE> ); > close FILE; > > open FILE, 'file2.txt' or die "Cannot open 'file2.txt': $!"; > my $data2 = do { local $/; <FILE> }; > close FILE; > > my $regex = join( '|', sort { length $b <=> length $a } split( ' ', $data2 ) ); > > open FILE, '> file2.txt' or die "Cannot open 'file2.txt': $!"; > for ( @data1 ) { > $data2 =~ s/^(.*)($regex)$/$_$2/mg; > print FILE $data2; > }
John, That's an interesting solution, but I think the following is a bit cleaner (unless I am just completely missing something here). #!/usr/bin/perl -w use strict; open FIRST, "< first.txt" or die "Cannot open first.txt: $!"; open SECOND, "< second.txt" or die "Cannot open second.txt: $!"; chomp( my @first = <FIRST> ); chomp( my @second = <SECOND> ); close SECOND; close FIRST; open THIRD, "> third.txt" or die "Cannot open third.txt: $!"; foreach ( @second ) { foreach my $word ( @first ) { print THIRD "$word$_\n"; } } close THIRD; Cheers, Curtis "Ovid" Poe ===== "Ovid" on http://www.perlmonks.org/ Someone asked me how to count to 10 in Perl: push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//; shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A __________________________________________________ Do You Yahoo!? Send your FREE holiday greetings online! http://greetings.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]