Football formation

I'm trying to create a perl script that will automatically fit the player's name to the formation.
For example, in my:
DR.txt

Arbeloa 200
Carvajal 190
Ramos 180

DC.txt

Ramos 200
Pepe 190
Varane 180

DL.txt

Marcelo 200
Coentrao 190
Arbeloa 180

formation.txt:

DR
DC
DC
DL
DR
DC
DC
DL

My expected output from my formation.txt will be:

DR Arbeloa 200
DC Ramos 200
DC Pepe 190
DL Marcelo 200
DR Carvajal 190
DC Varane 180
DC
DL Coentrao 190

Any help?

For your first request, which included only the first four lines of your output equivalent to formation's content, this might work:

awk     'FILENAME != "formation"        {PL[FILENAME,++CNT[FILENAME]]=$0; next}
                                        {print $0, PL[$0,++CNT2[$0]]}
        ' DR DC DL formation
DR Arbeloa 200
DC Ramos 200
DC Pepe 190
DL Marcelo 200
1 Like

Actually the next line is "Carvajal" because "Arbeloa" is already existed in the line up. :slight_smile:

Maybe I'm lost because I'm used to American football, but I don't understand the rules that are being used to create the output you are expecting from your four given input files.

Why does this need to be done using perl ? Is this a homework assignment?

For sure it is not homework assignment. From the formation.txt,

the first row: DR. Therefore, it will take the name from DR. txt which is ARBELOA 200
the second row: DC. Therefore, it will take the name from DC. txt which is RAMOS 200
the third row: DC. Therefore, it will take the name from DC. txt which is RAMOS 200, BUT since RAMOS is already exist in the line up, it will be filled by PEPE 190
the fourth row: DL. Therefore, it will take the name from DL. txt which is MARCELO 200
the fifth row: DR. Therefore, it will take the name from DR. txt which is ARBELOA 200, BUT since ARBELOA is already exist in the line up, it will be filled by CARVAJAL 190
the sixth row: DC. Therefore, it will take the name from DC. txt which is RAMOS 200, BUT since RAMOS and PEPE are already exist in the line up, it will be filled by VARANE 180
the seventh row: DC. Therefore, it will take the name from DC. txt which is RAMOS 200, BUT since RAMOS, PEPE and VARANE are already exist in the line up, it will be blank
the eighth row: DL. Therefore, it will take the name from DL. txt which is MARCELO 200, BUT since MARCELO is already exist in the line up, it will be filled by COENTRAO 190

Because I believe PERL is efficient for text processing and I have around 3312 of formation.

With your new formation file, this is the result:

DR Arbeloa 200
DC Ramos 200
DC Pepe 190
DL Marcelo 200
DR Carvajal 190
DC Varane 180
DC 
DL Coentrao 190

What about perl?
After I run the awk, it gives me error

C:\Program Files\GnuWin32\bin>awk 'FILENAME != "formation.txt" {PL[FILENAME,++CNT[FILENAME]]=$0; next} {print PL[$0,++CNT2[$0]]} ' DR.txt DC.txt DL.txt formation.txt
awk: 'FILENAME
awk: ^ invalid char ''' in expression

Start with this one:

#!/usr/bin/perl
open (DR,"DR.txt");
open (DC,"DC.txt");
open (DL,"DL.txt");
open (F,"formation.txt");
while (<F>) {
  chomp;
  if ($_ eq "DR") {
    chomp ($line = <DR>);
  }
  elsif ($_ eq "DC") {
    chomp ($line = <DC>);
  }
  elsif ($_ eq "DL") {
    chomp ($line = <DL>);
  }
  else {print "error\n"; next}
  printf "%s %s\n",$_,$line;
}
close F;
close DR;
close DC;
close DL;