Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How to add a new column into my csv file?

by chaney123 (Acolyte)
on Sep 07, 2017 at 06:05 UTC ( [id://1198807]=perlquestion: print w/replies, xml ) Need Help??

chaney123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

After I succeeded in combining the two files into one using hash, I have a problem now to add one more column into the last column. So I have these two files:

File1: Item,Period,Waveform,Attributes,Sources an_en11,200.00,{0 133},{ } br_gh13,140.09,{0 59},{ } ce_oy74,300.05,{0 230},{int_43} dt_pp50,200.11,{0 122},{ } er_tk02,305.47,{0 220},{ } ef_yb41,200.05,{0 233},{ } File2: Item,Sink,Buffer,Cell,Slew,Path,Violation,Area dt_pp50,0,0,2,0.000,0.000,0,0.000 er_tk02,0,2,3,0.002,0.004,0,0.001 ef_yb41,0,1,5,0.000,0.000,0,0.000

I wish to generate the last column (%Skew) by using (Slew/Period)*100%.

Item,Sink,Buffer,Cell,Slew,Path,Violation,Area,Period,%Skew dt_pp50,0,0,2,0.000,0.000,0,0.000,200.11,(Slew/Period)x100% er_tk02,0,2,3,0.002,0.004,0,0.001,305.47,(Slew/Period)x100% ef_yb41,0,1,5,0.000,0.000,0,0.000,200.05,(Slew/Period)x100%

Is there any suggestion on what command should i use in my script to get the output? My code :

#! /usr/intel/bin/perl-w use strict; use warnings; my %file1Hash; my $value1; open my $file1, "<","design.rpt.csv"; open my $file2, "<","summary.rpt.csv"; open my $outfile_1, ">", "combined.rpt.csv"; print $output_1 "Item, Sink, Bufer, Cells, Slew, Path, Violation, Area +, Period, %Skew\n"; while(my $line = <$file1>){ chomp $line; my($key, $value1) = (split /,/, $line); if (defined $key) { $file1Hash{$key}= $value1; } else { # print $line."\n"; } } while(my $line1 = <$file2>){ chomp $line1; my($key1) = (split /,/, $line1) ; if (defined $file1Hash{$key1}) { print $outfile_1 $line1.",".$file1Hash{$key1}."\n"; + } else { # print $line1."\n"; } } close $file1; close $file2; close $outfile_1;

Where should i start or how do i start?

Replies are listed 'Best First'.
Re: How to add a new column into my csv file?
by Tux (Canon) on Sep 07, 2017 at 06:20 UTC
      Hi,

      Thanks for the suggestion but may I know what's the reason behind it? Is split not efficient? Sorry, I just started to learn Perl few weeks ago.

        what's the reason behind it?

        The modules will handle any complications: embedded separation characters, quoting rules, missing columns, character encoding, EOL issues, etc. split will simply split a string into a list which is fine for trivial, well-formed in-memory CSV datasets. To go beyond such datasets would be tortuous without using Text::CSV_XS et al.

        It is the same reason why you would not be recommended to use regular expressions to parse any but the most trivial XML sets, for example.

        Update: (edited for typo)

Re: How to add a new column into my csv file?
by kcott (Archbishop) on Sep 07, 2017 at 06:27 UTC

    G'day chaney123,

    See the response I gave you two days ago.

    Change

    $csv->print(\*STDOUT, [$_, @{$data{$_}}]) ...

    to

    $csv->print(\*STDOUT, [$_, @{$data{$_}}, SKEW]) ...

    where SKEW evaluates to your new column datum.

    — Ken

Re: How to add a new column into my csv file?
by Laurent_R (Canon) on Sep 07, 2017 at 08:55 UTC
    Hi chaney123,

    in which respect does your script not fulfill your needs?

    I can see one coding error in this line:

    print $output_1 "Item, Sink, Bufer, Cells, Slew, Path, Violation, Area +, Period, %Skew\n";
    The $output_1 variable has not been defined (you presumably mean $outfile_1), but I assume that this is just a mistake while copying the code into your post, since this would generate a compile time error fairly easy to understand and to fix.

    Besides that, the general algorithm seems correct to me for what you want to do. So, please tell us how your script fails to do what you want.

      Yea you are right! I copy my codes wrongly. It should be $outfile_1. My problem is that I have no idea on how to perform calculation from the csv files generated. I wish to generate a %skew by using the data in column 'skew' and 'period' *Formula: ('skew' / 'period')100.* Can you give me some idea on how to continue? Thanks

        while (my $line1 = <$file2>){ chomp $line1; # Item,Sink,Buffer,Cell,Slew,Path,Violation,Area my @f = split /,/, $line1 ; my $item = $f[0]; my $slew = $f[4]; next if $item eq 'Item'; # skip header if (defined $file1Hash{$item}) { my $period = $file1Hash{$item}; my $pc = sprintf "%.10f",100*$slew/$period unless ($period==0); print $outfile_1 join ',',$line1,$period,$pc."\n"; } }
        poj
        Just a quick piece of code that seems to be doing what you want:
        use strict; use warnings; use Data::Dumper; my $file1 = "an_en11,200.00,{0 133},{ } br_gh13,140.09,{0 59},{ } ce_oy74,300.05,{0 230},{int_43} dt_pp50,200.11,{0 122},{ } er_tk02,305.47,{0 220},{ } ef_yb41,200.05,{0 233},{ }"; open my $FH1, "<", \$file1 or die "cannot open $file1"; my %file1Hash; while (my $line = <$FH1>) { my ($item, $period) = split /,/, $line; $file1Hash{$item} = $period; } close $FH1; print Dumper \%file1Hash; my $file2 = "dt_pp50,0,0,2,0.000,0.000,0,0.000 er_tk02,0,2,3,0.002,0.004,0,0.001 ef_yb41,0,1,5,0.000,0.000,0,0.000"; open my $FH2, "<", \$file2 or die "cannot open $file2"; while (my $line = <$FH2>) { chomp $line; my ($item, $slew) = (split /,/, $line)[0,4]; next unless $file1Hash{$item}; # skip the line if the period is no +t defined or if it is 0 my $skew = 100 * $slew / $file1Hash{$item}; printf "%s,%d,%.8f\n", $line, $file1Hash{$item}, $skew; }
        Note that I embedded the input data into the code for simplicity of the test.

        And an sample execution (at the command line):

        Also note that I used split to keep closer to your code, but it would make sense to use Text::CSV_XS or Text::CSV as recommended earlier by other monks, especially if your input data gets more complicated.

        HTH.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1198807]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found