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

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
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 whic +h 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 wi +ll be filled by PEPE 190 the fourth row: DL. Therefore, it will take the name from DL. txt whic +h 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, i +t 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 whi +ch is RAMOS 200, BUT since RAMOS, PEPE and VARANE are already exist i +n the line up, it will be blank the eighth row: DL. Therefore, it will take the name from DL. txt whic +h is MARCELO 200, BUT since MARCELO is already exist in the line up, +it will be filled by COENTRAO 190
Any help?

Replies are listed 'Best First'.
Re: Football formation
by GrandFather (Saint) on Oct 28, 2014 at 03:18 UTC

    I can't see the code you've tried nor any description of how it failed. Without that it's hard to see where you are having difficulty.

    Perl is the programming world's equivalent of English
Re: Football formation
by crashtest (Curate) on Oct 28, 2014 at 03:50 UTC

    Is this homework? If so that's fine but you should indicate it, and get started on the problem yourself. The monks will be happy to help you if you get stuck. Loops just gave you a nice terse solution but how would you explain it to a teacher or professor if she asked?

    As a starting point, this is a pretty standard way in Perl to open a file for reading and iterate over its contents line by line:

    open (my $file, '<', 'formation.txt') || die "Error opening file: $!"; while (<$file>) { chomp; # removes trailing newline character print "Line contents: $_\n"; }
    If you want to store the lines of a file in an array, you can use push (actually, there are much shorter ways to "slurp" the contents of a file into an array, but this works fine):
    my @lines; open (my $file, '<', 'formation.txt') || die "Error opening file: $!"; while (<$file>) { chomp; # removes trailing newline character push @lines, $_; }

      Hi, for sure it is not home work
Re: Football formation
by Loops (Curate) on Oct 28, 2014 at 03:38 UTC

    Hi there. Seems you just want to alternate reading between files, in the order given by the formation file, printing as you go?:

    use autodie qw( :all ); my %player; open $player{$_}, '<', $_.'.txt' for qw( DR DL DC ); open my $formation, '<', 'formation.txt'; for (<$formation>) { chomp; print "$_ ", readline($player{$_}) // "No Player\n"; }
      It cannot run. :)

        Darn. Why not?

        Update:

        You need a better Perl setup. Don't know what platform you're on but hopefully you can configure your machine to run Perl and install CPAN modules. And most importantly, see error messages without them flashing by. It doesn't make sense to try to do a project like this until you can comfortably run a "hello world" type application.

        If you're impatient though, you can just remove the "use autodie ..." line in the script. You wont get decent errors if one of the files can't be opened. But the error would probably just flash by anyway. ;o)