如何从CSV中获取数据并使用它来填充Perl Hash?

时间:2013-05-06 21:43:59

标签: perl csv hash

我是Perl的新手,我正在寻找指导。我使用的是没有Text :: CSV或Text :: CSV_XS模块的Perl版本。我有Text :: ParseWords模块。

CSV文件以逗号分隔,其中包含一个或多个带逗号的逗号字段。我想删除双引号并保留嵌入的逗号。使用Text :: ParseWords:parse_line做了我正在寻找的东西,但是我希望将输出放到散列中,散列的第一个元素将成为散列的关键。

希望有人能在这里帮助我。感谢。

#!/usr/bin/perl -w
{
    use Text::ParseWords;
    local $record_count = 0;
    local $output_file  = "Test\_Hospital\.txt";
    local $source_folder =
      "\\\\test.org\\test\\vol1\\sharing\\finance files\\Priority Pay";
    local $dest_folder =
      "\\\\test.org\\test\\vol1\\sharing\\finance files\\Priority Pay";
    local $log_folder =
      "\\\\test.org\\test\\vol1\\sharing\\finance files\\Priority Pay\\save";
    local $logfile           = "CSV\_Conversion\.txt";
    local $input_file_exists = $FALSE;

    open logfile, ">$log_folder\\$logfile" || die "Unable to open logfile\n";
    open output, ">$dest_folder\\$output_file";

    opendir( workdir, $source_folder );    # open input folder
    @inputfiles = readdir(workdir);        # create array of
                                           # all file names in thisfolder
                                           #print LOG_FILE "@inputfiles\n";
    print logfile "@inputfiles\n";

    foreach $inputname (@inputfile) {      # looking for input files
        print logfile "$inputname\n";
        if ( ( $_ ne "\." ) && ( $_ ne "\. " ) && ( $_ ne "\.\." ) ) {
            print logfile "[$inputname]\n";
            if ( $inputname =~ "Winthrop_Hospital" ) {
                $input_file = $inputname;
                print logfile "$input_file exists\n";
                $input_file_exists = $TRUE;
            }
        }
    }

    closedir(workdir);                     # close work folder

    if ( $input_file_exists = $TRUE ) {
        print logfile "$input_file exists, processing will continue...\n\n";
    }

    open input,
      "<$source_folder\\$input_file" || die "unable to open input file\n";

    local $lines_read = 0;
    while ( $line = <input> ) {
        $lines_read++;
        chomp($line);
        $line =~ s/\$//g;
        $line =~ s/\.//g;

        #$line =~ s/\"//g;
        if ( $lines_read == 1 ) {
            next;    # Skip header record in csv file.
        }
        @output = Text::ParseWords::parse_line( ',', 0, $line );
        print logfile "Here is the parsed line:[@output]\n";

    }

}

0 个答案:

没有答案