Aus der Beschreibung Sie @ikegami gab gegeben «mein Programm akzeptieren ...» I eine Datei erstellt, die die Daten haben würde:
data_1.txt: bis
john 10 45 20
alex 30 15 12
pete 23 45 10 21
will 06 56
bob 8 12 3
lazy
beachten Sie, dass nur die ersten beiden Linien entsprechen tatsächlich die Beschreibung, komme ich zurück, dass später.
sum.pl:
use strict;
use warnings;
use List::Util 'sum';
# get the two filenames it should work with
#
my $filename_1 = shift;
my $filename_2 = shift;
# be sure we read a file for most modern systems, UTF-8
#
open(my $file1, '<:encoding(UTF-8)', $filename_1)
or die "Can't open file: $filename_1";
# create the (empty) data structure
#
my %sums_and_names;
#
# the % in perl means you are talking about a hash,
# use a sensible name instead of 'hash'
# read line by line
while (my $line = <$file1>) {
chomp $line; # get rid of the line endings
my ($name, @grades) = split ' ', $line;
#
# this is not strictly doing what you asked for, just more flexible
#
# split on ' ', a space character, splits on any asmount of (white) space
# your task said that there is one space.
# strictly, you could should split on//, the regular expression
#
# the first part will go into the variable $name, the rest in array @grades
# strictly you have only three grades so the following would do
# my ($name, $grade_1, $grade_2, $grade_3) = split//, $line;
my $sum = sum(@grades) // 'no grades';
#
# since we now can handle any number of grades, not just three, we could
# have no grades at all and thus result in `undef`
#
# using the function sum0 would return the value 0 instead
#
# you'll get away with the `undef` using in a hash assignment,
# it will turn it into an empty string `''`
=pod
$sums_and_names{foo}{bar} = [ 'baz', 'qux' ];
=cut
#
# here is where your task doesn't make sense
# i am guessing:
#
$sums_and_names{$sum}{$name} = \@grades;
#
# at least we have all the data from filename_1, and the sum of the grades
}
# please decide on what you want to print
use Data::Dumper;
print Dumper \%sums_and_names;
und perl sum.pl data_1.txt data_2.txt
laufen wird Ihnen etwas wie
Ausgang:
$VAR1 = {
'no grades' => {
'lazy' => []
},
'23' => {
'bob' => [
'8',
'12',
'3'
]
},
'57' => {
'alex' => [
'30',
'15',
'12'
]
},
'62' => {
'will' => [
'06',
'56'
]
},
'75' => {
'john' => [
'10',
'45',
'20'
]
},
'99' => {
'pete' => [
'23',
'45',
'10',
'21'
]
}
};
Bitte beachten Sie, dass ausschließlich der Block innerhalb der while-Schleife könnte geschrieben worden sein als:
chomp $line;
my ($name, $grade_1, $grade_2, $grade_3) = split//, $line;
$sum = $grade_1 + $grade_2 + $grade_3;
$sums_and_names{$sum}{$name} = [ $grade_1, $grade_2, $grade_3 ];
aber ich zitiere aus @Borodin:
Und während ich bin sicher, dass Sie als etwas allgemeinere wollen, haben Sie wirklich nicht genug Informationen
Bitte immer 'hinzuzufügen gegeben Verwenden Sie streng; Verwenden Sie Warnungen; 'zu Ihren Skripten, um häufige Fehler zu vermeiden. /// Was genau ist dein Problem?Sie haben einen Hash erstellt, der '{'10' => {'20' => 75}}' enthält. Was willst du stattdessen? – PerlDuck
am Ende der Hashes Ich möchte 10: 75 auf den Bildschirm drucken, aber ich weiß nicht, wie man den Reference für diesen Hash geben .. –
Warum verwenden Sie überhaupt einen 2-dimensionalen Hash? Warum nicht einfach '$ hash {$ array [0]} = $ sum [0]'? Das gibt '{' 10 '=> 75} '. – PerlDuck