2016-10-12 1 views
-4

Ich habe ein Perl-Skript, das Professor gab, der leicht zu laufen sein soll, aber meine Maschine gibt Fehler, den ich nicht verstehe. da das Verzeichnis, in dem sich diese Datei befindet, eine andere Datei mit dem Namen file_in hat, die ich erstellt habe, weil ich denke, dass das einzige ist, was ich tun muss, um dieses Skript auszuführen. Aber es gibt mir einen Fehler in Zeile 33. Bitte helfen Sie mir. Danke,Kann mir jemand mit einem einfachen Perl-Skript helfen

#!/usr/bin/perl 

# the strict package forces you to declare each variable you use beforehand 
use strict; 

# a variable in strict mode is declared using my 
# the $ symbol means it is a single-valued variable 
# the @ symbol means it is an array 
# each declaration/instruction is closed with a ; sign 
my @par_list = (1,2,3,4,5,6,7,8,9,10); 

# creating a variable for the current value of the parameter 
my $value; 

# get and store the size of the array 
my $nbr_of_values = $#par_list; 

# now, we read in a variable that will be the filename of the template input file 
# $ARGV are the input arguments, 0 means it is the first one (perl starts counting at 0, not 1) 
my $file_in = $ARGV[0]; 

# start of the loop 
for(my $i=0; $i<= $nbr_of_values; $i++){ 
    $value = $par_list[$i]; 
    print "This is the current parameter value: $value \n"; 

    # now we create a new string variable that will later be the filename of the new input deck 
    # the . symbol is the concatenation operator between strings 
    my $new_input_filename = $file_in."_".$value; 
    print " The new filename is $new_input_filename \n"; 

    # open the template file and store its filehandle (fh_in) 
    open my $fh_in, '<', $file_in or die "Can't open output $file_in !"; 
    # open the new file (it currently does not exist and is thus empty) and store its filehandle (fh_out) 
    open my $fh_out, '>', $new_input_filename or die "Can't open output $new_input_filename !"; 

    while (<$fh_in>) { 
    # this is for you to see on the console, we read line-by-line, while there is something 
    # the line read is stored in a special PERL variable $_ 
    print "I have read $_"; 
    # now we actually print that line intot he new file 
    print $fh_out $_; 
    } 
    close $fh_in; 
    close fh_out; 
} 

print " I am done with this !!! \n"; 
exit 111; 
+1

laufen Was ist die Fehlermeldung, die Sie bekommen? Diese Fehlermeldung hat wahrscheinlich wichtige Hinweise darauf, was das Problem ist. – dasgar

+0

Nur ein paar andere Artikel. Es könnte hilfreich sein, "Warnungen zu verwenden"; in der Zeile nach "use strict;", was hilfreiche Warnungen liefern könnte. Die Variable $ nbr_of_values ​​erhält die Größe des Arrays nicht wirklich, wie die Kommentare andeuten. Stattdessen erhält es die letzte Element-ID des Arrays. Es sieht auch so aus, als ob Zeile 33 der ersten offenen Anweisung entspricht. Wenn du die Variable $! Innerhalb der doppelten Anführungszeichen nach dem Wort "sterben" erhalten Sie eine Fehlermeldung mit einigen Informationen darüber, warum Perl eine Datei nicht öffnen konnte. – dasgar

+0

Haben Sie die 10 Dateien, die Ihr Skript lesen soll? Ist die Fehlermeldung, die Sie mit einem Beendigungscode von 111 beenden? – AntonH

Antwort

1

Das Skript wird nicht geöffnet file_in. $file_in ist eine Variable, die an das Skript übergeben werden soll. Hinweis my $file_in = $ARGV[0];.

$ARGV[0] ist das erste Befehlszeilenargument, das Sie an das Skript übergeben müssen.

How do you use command line parameters

Wenn Sie eine Datei in diesem Verzeichnis erstellt haben und es file_in "genannt, dann perl_part3.pl file_in

+0

Shabash boi Shabadh. –

+1

Das war genial. Nimm meine Erwähnung. –

+0

Andere haben es nicht verstanden, aber du warst dran. –

Verwandte Themen