String Perl条件
正在尝试遍历两个文件。一切都正常,尽管一旦我否定了if语句,就会把一切搞砸。唯一可以打印的是else语句 定义时,请忽略任何未使用的变量。以后我会把它清理干净String Perl条件,string,perl,loops,conditional-statements,String,Perl,Loops,Conditional Statements,正在尝试遍历两个文件。一切都正常,尽管一旦我否定了if语句,就会把一切搞砸。唯一可以打印的是else语句 定义时,请忽略任何未使用的变量。以后我会把它清理干净 #!/usr/bin/perl # # Packages and modules # use strict; use warnings; use version; our $VERSION = qv('5.16.0'); # This is the version of Perl to be used use Text::CS
#!/usr/bin/perl
#
# Packages and modules
#
use strict;
use warnings;
use version; our $VERSION = qv('5.16.0'); # This is the version of Perl to be used
use Text::CSV 1.32; # We will be using the CSV module (version 1.32 or higher)
# to parse each line
#
# readFile.pl
# Authors: schow04@mail.uoguelph + anilam@mail.uoguelph.ca
# Project: Lab Assignment 1 Script (Iteration 0)
# Date of Last Update: Monday, November 16, 2015.
#
# Functional Summary
# readFile.pl takes in a CSV (comma separated version) file
# and prints out the fields.
# There are three fields:
# 1. name
# 2. gender (F or M)
# 3. number of people with this name
#
# This code will also count the number of female and male
# names in this file and print this out at the end.
#
# The file represents the names of people in the population
# for a particular year of birth in the United States of America.
# Officially it is the "National Data on the relative frequency
# of given names in the population of U.S. births where the individual
# has a Social Security Number".
#
# Commandline Parameters: 1
# $ARGV[0] = name of the input file containing the names
#
# References
# Name files from http://www.ssa.gov/OACT/babynames/limits.html
#
#
# Variables to be used
#
my $EMPTY = q{};
my $SPACE = q{ };
my $COMMA = q{,};
my $femalecount = 0;
my $malecount = 0;
my $lines = 0;
my $filename = $EMPTY;
my $filename2 = $EMPTY;
my @records;
my @records2;
my $record_count = -1;
my $top_number = 0;
my $male_total = 0;
my $male_count = 0;
my @first_name;
my @gender;
my @first_name2;
my @number;
my $count = 0;
my $count2 = 0;
my $csv = Text::CSV->new({ sep_char => $COMMA });
#
# Check that you have the right number of parameters
#
if ($#ARGV != 1) {
print "Usage: readTopNames.pl <names file> <course names file>\n" or
die "Print failure\n";
exit;
}
$filename = $ARGV[0];
$filename2 = $ARGV[1];
#
# Open the input file and load the contents into records array
#
open my $names_fh, '<', $filename
or die "Unable to open names file: $filename\n";
@records = <$names_fh>;
close $names_fh or
die "Unable to close: $ARGV[0]\n"; # Close the input file
open my $names_fh2, '<', $filename2
or die "Unable to open names file: $filename2\n";
@records2 = <$names_fh2>;
close $names_fh2 or
die "Unable to close: $ARGV[1]\n"; # Close the input file
#
# Parse each line and store the information in arrays
# representing each field
#
# Extract each field from each name record as delimited by a comma
#
foreach my $class_record (@records)
{
chomp $class_record;
$record_count = 0;
$count = 0;
foreach my $name_record ( @records2 )
{
if ($csv->parse($name_record))
{
my @master_fields = $csv->fields();
$record_count++;
$first_name[$record_count] = $master_fields[0];
$gender[$record_count] = $master_fields[1];
$number[$record_count] = $master_fields[2];
if($class_record eq $first_name[$record_count])
{
if($gender[$record_count] eq 'F')
{
print("$first_name[$record_count] ($record_count)\n");
}
if($gender[$record_count] eq 'M')
{
my $offset = $count - 2224;
print("$first_name[$record_count] ($offset)\n");
}
}
} else {
warn "Line/record could not be parsed: $records[$record_count]\n";
}
$count++;
}
}
#
# End of Script
#
Adam (187)
Alan (431)
Alejandro (1166)
Alex (120)
Alicia (887)
Ambrose (305)
Caleb (794)
这就是其他人应该找到的。if语句是否没有返回任何内容
else {
print("$first_name[$record_count] (0)\n");
}
当我把它加在一起的时候,我得到的结果,用来解释否定,实际上是:
Elzie (0)
Emer (0)
Enna (0)
Enriqueta (0)
Eola (0)
Eppie (0)
Ercell (0)
Estellar (0)
拟议的4项修改:
foreach my $class_record (@records)
{
chomp $class_record;
$record_count = 0;
$count = 0;
# add found - modification A
my $found = 0;
foreach my $name_record ( @records2 )
{
# should not be here
#$record_count++;
if ($csv->parse($name_record))
{
my @master_fields = $csv->fields();
$record_count++;
$first_name[$record_count] = $master_fields[0];
$gender[$record_count] = $master_fields[1];
$number[$record_count] = $master_fields[2];
if($class_record eq $first_name[$record_count])
{
if($gender[$record_count] eq 'F')
{
print("$first_name[$record_count] ($record_count)\n");
}
if($gender[$record_count] eq 'M')
{
my $offset = $count - 2224;
print("$first_name[$record_count] ($offset)\n");
}
# modification B - set found =1
$found = 1;
#last; # no need to keep looping
next; # find next one if try to find more than 1
}
} else {
warn "Line/record could not be parsed: $records[$record_count]\n";
}
$count++;
}
# modification C -
if($found){
}else{
print "${class_record}(0)\n";
}
}
如果没有更好的信息,很难正确地帮助您,所以我写了这个,它从主数据文件中的名称文件中查找每个名称,并显示相关的值 从来没有理由在程序顶部写一长串这样的声明,而且在开始调试之前,您已经编写了太多的代码。在测试代码是否有效并继续添加之前,应该编写不超过三到四行的代码。你已经有140行了,其中大部分是评论,这些都不是你想要的,你现在不知道应该先修复什么 我无法理解你所有不同的计数器是用来做什么的,或者为什么你要为男性记录减去magic 2224,所以我只是直接从主文件中打印了数据 我希望您会同意,在需要时声明变量比在程序顶部列出一个大的列表要清晰得多。我删除了数组@first_name、@gender和@number,因为您只使用了最新的值,所以它们没有任何用途
#!/usr/bin/perl
use strict;
use warnings;
use v5.16.0;
use autodie;
use Text::CSV;
STDOUT->autoflush;
if ( @ARGV != 2 ) {
die "Usage: readTopNames.pl <names file> <master names file>\n";
}
my ( $names_file, $master_file ) = @ARGV;
my @names = do {
open my $fh, '<', $names_file;
<$fh>;
};
chomp @names;
my @master_data = do {
open my $fh, '<', $master_file;
<$fh>;
};
chomp @master_data;
my $csv = Text::CSV->new;
for my $i ( 0 .. $#names ) {
my $target_name = $names[$i];
my $found;
for my $j ( 0 .. $#master_data ) {
my $master_rec = $master_data[$j];
my $status = $csv->parse($master_rec);
unless ( $status ) {
warn qq{Line/record "$master_rec" could not be parsed\n};
next;
}
my ( $name, $gender, $count ) = $csv->fields;
if ( $name eq $target_name ) {
$found = 1;
printf "%s %s (%d)\n", $name, $gender, $count;
}
}
unless ( $found ) {
printf "%s (%d)\n", $target_name, 0;
}
}
如果呢?还有什么?那不行。请提供缺少的位。如果$class\u record eq$first\u name[$record\u count]这部分,我去掉了else,它工作,没有else它不工作。你需要从正确命名变量开始。例如,您有$record\u count、$count和$counter。即使您可能也不知道它们都在计算什么,因为$counter对于@records2的每次迭代都会增加两次,但在其他方面没有使用@记录似乎是一个名字列表,因为迭代器$class_记录在$class_记录eq$first_name[$record_count]中进行比较,但是还有一个@records2,它也没有说明它可能包含什么。我对你的困惑并不感到惊讶!正如我在回答中提到的,它应该是我的$name_record@records2{$record_count++…}。它是您放置$record\u count++的地方。
#!/usr/bin/perl
use strict;
use warnings;
use v5.16.0;
use autodie;
use Text::CSV;
STDOUT->autoflush;
if ( @ARGV != 2 ) {
die "Usage: readTopNames.pl <names file> <master names file>\n";
}
my ( $names_file, $master_file ) = @ARGV;
my @names = do {
open my $fh, '<', $names_file;
<$fh>;
};
chomp @names;
my @master_data = do {
open my $fh, '<', $master_file;
<$fh>;
};
chomp @master_data;
my $csv = Text::CSV->new;
for my $i ( 0 .. $#names ) {
my $target_name = $names[$i];
my $found;
for my $j ( 0 .. $#master_data ) {
my $master_rec = $master_data[$j];
my $status = $csv->parse($master_rec);
unless ( $status ) {
warn qq{Line/record "$master_rec" could not be parsed\n};
next;
}
my ( $name, $gender, $count ) = $csv->fields;
if ( $name eq $target_name ) {
$found = 1;
printf "%s %s (%d)\n", $name, $gender, $count;
}
}
unless ( $found ) {
printf "%s (%d)\n", $target_name, 0;
}
}
Adam F (7)
Adam M (5293)
Alan F (9)
Alan M (2490)
Name (0)
Alejandro F (6)
Alejandro M (2593)
Alex F (157)
Alex M (3159)
Alicia F (967)
Ambrose M (87)
Caleb F (14)
Caleb M (9143)