vis1982 has asked for the wisdom of the Perl Monks concerning the following question:

Suppose u have a file

file_A

Messi Ronaldo Beckham Figo Terry Gerrard Owen Rooney Drogba Zidane

file_B

Ronaldo 01490 NP_0033 SIMO

Messi 01740 NP_0006 SUMO

Messi 05961 NP_055184.2 SIMO

Zidane 06090 AAK69431.1 Fren

Ronaldo 06901 NP_073576.1 Por

Drogba 06980 NP_000259.1 Hin

CD82 09004 NP_002222.1 Ronaldo

ERR 09218 NP_061821.1 Figo

MMP7 01525 NP_002414.1 Rooney

Beckham 09273 NP_005740.1 ERB

MUC4 11829 NP_060876.3 ERBB2

Rooney 16176 NP_036539.1 ERBB

Gerrard 06901 NP_073576.1 TXN

DDX 05859 NP_009135.3 Gerrard

TLE 02557 NP_005068.2 Roonye

TLE3 02558 NP_005069.1 Messi

Rooney 03143 NP_004955.2 FOXG1B

The output should be

Ronaldo SIMO

Ronaldo Por

Ronaldo CD82 in one file created

Messi SUMO

Messi SIMO

Messi TLE3 in another file created

Zidane Fren another file created

...so on...

Beckham ERB

Gerrard TXN

Gerrard DDX

Rooney MMP7

Rooney ERBB

Rooney TLE

Rooney FOXG1B

However while running this code but i have not written code for files to be created separately

#!/usr/bin/perl use warnings; use strict; ( my ( $file_1, $file_2 ) = @ARGV ) == 2 or die "usage: $0 file_1 file +_2\n"; my $data = do { local $/; open my $FH, '<', $file_2 or die "Cannot open '$file_2' $!"; <$FH>; }; open my $FH, '<', $file_1 or die "Cannot open '$file_1' $!"; open my $WRITE, '>', 'output.txt' or die "Cannot open 'output.txt' $!" +; $\ = $/; while ( <$FH> ) { chomp; next unless /\S/; print $WRITE $data =~ /($_)\s+\d+\s+\S+(\s+\S+)/; } __END__

Output is

Messi SUMO

Ronaldo SIMO

Beckham ERB<\p>

Gerrard TXN

Rooney ERBB

Drogba Hin

Zidane Fren

So what can we do?

Replies are listed 'Best First'.
Re: matching options and create new file
by Corion (Patriarch) on May 11, 2010 at 07:42 UTC

    open takes as second parameter the name of the file to create. So, instead of giving it the hardcoded output.txt, give it a variable that holds the name of the file you want to create.

      what about the output which have been single line instead whatever mentioned like

      Ronaldo SIMO

      Ronaldo POR

      Ronaldo CD82

      in one file ....