我有一个问题,我似乎无法找到答案。
我有一个CSV文件,其中包含不同个人的效果记录。每个人只有一个记录,但是,有些人有几个记录不同的信息。我想将这个第一个文件与另一个也有个人列表的文件进行比较,但我只想比较文件1中的个人是否也有文件2中的记录(文件2没有重复项)。个人的身份证是独一无二的。
文件1的示例:
ID number A B C D
4011NM16001 apple 24 sunday 2016-01-01
4011NM16001 apple 16 wednesday 2016-01-01
4012NM15687 pear 16 sunday 2015-04-19
4012NM15002 banana 8 monday 2015-09-09
4012NM14301 peach 10 wednesday 2014-03-18
4012NM14301 peach 18 wednesday 2014-03-18
我打开了第一个文件并尝试将数据放入哈希(或者更确切地说是哈希和数组的组合,如果我正确理解了这些概念),以便使用ID作为唯一键来删除重复项。但是,不是覆盖具有相同ID的条目,它似乎仍然添加它,所以我仍然最终得到重复的记录。
我想看到这个:
ID number
4011NM16001
4011NM15687
4012NM15002
4012NM14301
但我仍然看到了这一点:
ID number
4011NM16001
4011NM16001
4012NM15687
4012NM15002
4012NM14301
4012NM14301
我在代码中输入了错误或者我没有正确使用散列吗?我还是Perl的新手,所以我使用以前程序的一部分,并在我去的时候尝试学习..
#!/usr/bin/env perl
use DBI;
use strict;
use warnings;
my $file1 = 'location1.csv'; #file1 containing records with duplicates
my $exists = 'location3.csv'; #output file with unique IDs that will be compared to file2
open (EXISTS, ">$exists") or die "Cannot open $exists";
print EXISTS "ID number\n";
open (FILE1, "$file1") or die "Cannot open $file1";
while (<FILE1>){
my %file1;
my $line = $_;
$line =~ s/\s*$//g;
my ($ID, $a, $b, $c, $d) = split('\,', $line);
next if !$ID or substr($ID,0,2) eq 'ID';
$file1{$ID}[0]=$ID; #unique ID number
$file1{$ID}[1]=$a; #record a
$file1{$ID}[2]-$b; #record b
$file1{$ID}[3]=$c; #record c
$file1{$ID}[4]=$d; #record d
print EXISTS "$file1{$ID}[0]\n";
}
exit;
答案 0 :(得分:4)
您正在为每条输入行打印行,而不是仅为不存在的行打印。
移动分配段落前的 **activity_layout.xml**
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.allyouask.hungryforhistory.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcomeMessage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/welcomeMessage"
android:fontFamily="sans-serif-light"
android:text="Welcome to Hungry For History!\n Let's get started!"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/inputQuestion"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/QuestionsStyle"
android:text=" Who was the king of the Gods in Ancient Greece:"
android:layout_below="@+id/welcomeMessage"/>
<EditText
android:id="@+id/inputAnswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/inputQuestion"/>
<TextView
android:id="@+id/firstQuestion"
style="@style/QuestionsStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/inputAnswer"
android:paddingLeft="5dp"
android:text="Who was born in Ancient City Stagira, Greece?"/>
<RadioGroup
android:id="@+id/firstGroupRadioButtons"
android:layout_below="@+id/firstQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/firstLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/firstGroupRadioButtons"
android:text="Aristotle"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/firstRightRadioButton"
android:layout_below="@+id/firstGroupRadioButtons"
style="@style/RadioButtonStyle"
android:layout_toRightOf="@+id/firstLeftRadioButton"
android:text="Pythagoras"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/secondQuestion"
style="@style/QuestionsStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firstLeftRadioButton"
android:paddingLeft="5dp"
android:text="Who said in his last speech:With malice toward none;...let us strive on to finish the work we are in;to bind up the nation's wounds;into care for him who shall have borne the battle and for his widow and his orphans?"/>
<RadioGroup
android:id="@+id/secondGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/secondQuestion"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/secondLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_alignParentLeft="true"
android:layout_below="@+id/secondGroupRadioButtons"
android:text="William McKinley"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/secondRightRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/secondGroupRadioButtons"
android:layout_toRightOf="@+id/secondLeftRadioButton"
android:text="Abraham Lincoln"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/thirdQuestion"
style="@style/QuestionsStyle"
android:layout_below="@+id/secondLeftRadioButton"
android:paddingLeft="5dp"
android:text="Where the An Lushan Rebellion took place?"/>
<RadioGroup
android:id="@+id/thirdGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/thirdQuestion"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/thirdLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/thirdGroupRadioButtons"
android:text="China"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/thirdRightRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/thirdGroupRadioButtons"
android:layout_toRightOf="@+id/thirdLeftRadioButton"
android:text="Thailand"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/fourthQuestion"
style="@style/QuestionsStyle"
android:layout_below="@+id/thirdLeftRadioButton"
android:text="Who was the most famous exemplar of absolute monarchy in France?"/>
<RadioGroup
android:id="@+id/fourthGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fourthQuestion"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/fourthLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/fourthGroupRadioButtons"
android:text="Louis XIV"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/fourthRightCheckBox"
style="@style/RadioButtonStyle"
android:layout_below="@+id/fourthGroupRadioButtons"
android:layout_toRightOf="@+id/fourthLeftRadioButton"
android:text="Michael I"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/fifthQuestion"
style="@style/QuestionsStyle"
android:layout_below="@+id/fourthLeftRadioButton"
android:text="When Alexander The Great lived?"/>
<RadioGroup
android:id="@+id/fifthGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fifthQuestion"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/fifthLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/fifthGroupRadioButtons"
android:text="330-323 BC"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/fifthRightRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/fifthGroupRadioButtons"
android:layout_toRightOf="@+id/fifthLeftRadioButton"
android:text="336-323 BC"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/sixthQuestion"
style="@style/QuestionsStyle"
android:layout_below="@+id/fifthLeftRadioButton"
android:text="Where Albert Einstein studied?"/>
<RadioGroup
android:id="@+id/sixthGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sixthQuestion"
android:orientation="horizontal"/>
<RadioButton
android:id="@+id/sixthLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/sixthGroupRadioButtons"
android:text="University of Zurich"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/sixthRightRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/sixthGroupRadioButtons"
android:layout_toRightOf="@+id/sixthLeftRadioButton"
android:text="University of Germany"
android:onClick="onRadioButtonClicked"/>
<TextView
android:id="@+id/seventhQuestion"
style="@style/QuestionsStyle"
android:layout_below="@+id/sixthLeftRadioButton"
android:text="What was the main interest of Democritus?"/>
<RadioGroup
android:id="@+id/seventhGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seventhQuestion"
android:orientation="horizontal">
<RadioButton
android:id="@+id/seventhLeftRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/seventhGroupRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mathematics-Astronomy"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/seventhRightRadioButton"
style="@style/RadioButtonStyle"
android:layout_below="@+id/seventhGroupRadioButtons"
android:layout_toRightOf="@+id/seventhLeftRadioButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Philosophy-Psychology"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/submitButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/backgroundColor"
android:layout_marginBottom="4dp"
android:text="Submit"
android:textColor="@color/textColor"
android:onClick="OnClick"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/resetButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
android:onClick="OnClick"
android:background="@color/backgroundResetColor"
android:textColor="@color/textColor"
android:text="Reset"
android:textAllCaps="true"
android:layout_below="@+id/submitButton"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#FFC107</item>
<item name="colorPrimaryDark">#E65100</item>
<item name="colorAccent">#EF6C00</item>
</style>
<style name="welcomeMessage">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_centerHorizontal">true</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">#EF6C00</item>
</style>
<style name="QuestionsStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingLeft">6dp</item>
<item name="android:paddingRight">6dp</item>
<item name="android:textSize">15sp</item>
<item name="android:textStyle">italic</item>
<item name="android:textColor">#000000</item>
</style>
<style name="RadioButtonStyle">
<item name="android:textSize">14sp</item>
<item name="android:textColor">#FFAB00</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
<item name="android:paddingLeft">6dp</item>
</style>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FFC107</color>
<color name="colorPrimaryDark">#E65100</color>
<color name="colorAccent">#EF6C00</color>
<!-- Button Colors -->
<color name="backgroundColor">#EF6C00</color>
<color name="backgroundResetColor">#FB8C00</color>
<color name="textColor">#FFFFFF</color>
</resources>
</resources>
并将其更改为
print
答案 1 :(得分:0)
除了choroba's diagnosis之外,你还需要声明哈希在之外的while
循环,否则循环的每次迭代都会处理一个新的空哈希
以下是使用最佳实践Perl的代码版本,并生成您想要的结果。请注意,由于您显示的值不包含任何逗号,因此我必须更改输入文件location1.csv
的格式
#!/usr/bin/env perl
use strict;
use warnings;
my $file1 = 'location1.csv'; # file1 containing records with duplicates
my $exists = 'location3.csv'; # output file with unique IDs that will be compared to file2
open my $exists_fh, '>', $exists or die qq{Unable to open "$exists" for output: $!};
print $exists_fh "ID number\n";
open my $file1_fh, '<', $file1 or die qq{Unable to open "$file1" for input: $!};
<$file1_fh>; # skip header line
my %file1;
while ( <$file1_fh> ) {
next unless /\S/; # Skip blank lines
s/\s+\z//;
my @fields = split /,/;
my $id = $fields[0];
next if $file1{$id}; # Skip this record if the ID is already known
$file1{$id} = \@fields;
print $exists_fh "$id\n"
}
ID number
4011NM16001
4012NM15687
4012NM15002
4012NM14301