代码之家  ›  专栏  ›  技术社区  ›  capser

用Perl逐行打印出csv

  •  0
  • capser  · 技术社区  · 6 年前

    我有一个价值观清单

    2018.04.09, 22:39, SPX , 2200 , 10 
    2018.04.09, 22:39, SPX , 2225 , 5 
    2018.04.09, 22:39, SPX , 2175 , 5
    2018.04.26, 34:03, SPXW , 2585 , 66
    2018.04.26, 34:03, SPXW , 2595 , 33
    2018.04.26, 34:03, SPXW , 2575 , 33
    2018.04.26, 34:03, SPXW , 2580 , 33
    2018.04.26, 34:03, SPXW , 2600 , 33
    2018.04.26, 34:03, SPXW , 2590 , 66
    

    我需要在这些值中投入时间-只需替换vlaues。

    2018.04.09 where ticker = `SPX, strikeprice = 2200, orderqty = 10, (string transacttime) like "*22:39.*"
    2018.04.09 where ticker = `SPX, strikeprice = 2225, orderqty = 5, (string transacttime) like "*22:39.*" 
    2018.04.09 where ticker = `SPX, strikeprice = 2200, orderqty = 5, (string transacttime) like "*22:39.*"  
    

    我不知道如何用Perl来实现它,我用Bash来实现。如何在Perl中执行此操作?

    #!/bin/bash
    OLDIFS=$IFS
    IFS=,
    while read -r date time sym str qty
    do
    echo "
    $date where ticker = \`${sym}, strikeprice = ${str}, orderqty = ${qty}, (string transacttime) like \"*${time}*\"
    "
    done < /tmp/list
    IFS=$OLDIFS
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   beasy    6 年前

    Text::CSV

    use strict;
    use warnings;
    
    use Text::CSV;
    my $parser = Text::CSV->new;
    
    open my $data, '<', 'data.csv';
    while (my $row = $parser->getline($data)) {
        # $row is an array reference. dereference by using "@$row"
        print 'the row is: '.join('||', @$row)."\n";
        # or access elements like $row->[0]
        print 'the first element of the row is '.$row->[0];
    }
    
        2
  •  2
  •   choroba    6 年前

    split

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    open my $in, '<', '/tmp/list' or die $!;
    while (<$in>) {
        chomp;
        my ($date, $time, $sym, $str, $qty) = split /,/;
        print qq(\n$date where ticker = `$sym, strikeprice = $str, orderqty = $qty, (string transacttime) like "*$time*"\n\n);
    }
    

    \n

        3
  •  1
  •   Tigran Khachikyan    6 年前

    # Simple headerless comma-seperated column parser
    my $simple = Parse::CSV->new(
        file => 'file.csv',
    );
    
    while ( my $array_ref = $simple->fetch ) {
       # Do something...
    }