小编use*_*783的帖子

解析CSV文件和散列

我正在尝试解析CSV文件以读取所有其他邮政编码.我正在尝试创建一个散列,其中每个键都是一个邮政编码,值是它在文件中出现的数字.然后我想打印出内容为邮政编码 - 数字.这是我到目前为止的Perl脚本.

use strict;
use warnings;

my %hash = qw (
     zipcode count
);

my $file = $ARGV[0] or die "Need CSV file on command line \n";

open(my $data, '<', $file) or die "Could not open '$file $!\n";
while (my $line = <$data>) {
   chomp $line;
   my @fields = split "," , $line;
   if (exists($hash{$fields[2]})) {
        $hash{$fields[1]}++;
   }else {
        $hash{$fields[1]} = 1;
   }
}

my $key;
my $value;
while (($key, $value) = each(%hash)) {
  print "$key - $value\n";
} …
Run Code Online (Sandbox Code Playgroud)

csv perl hash parsing

2
推荐指数
1
解决办法
597
查看次数

标签 统计

csv ×1

hash ×1

parsing ×1

perl ×1