in reply to Re: Compare 2 CSV files and create a new CSV file out of comparision
in thread Compare 2 CSV files and create a new CSV file out of comparision

This is an example that does not fully work: this is my improved Code ( Hope it will help somebody in the future) : use Data::Dump qw/dump/; use DBI; my $dbh = DBI->connect( "dbi:CSV:", "", "", { #f_schema => 'test', f_dir => '.', f_ext => '.csv', } ) or die "Could not open database: $DBI::errstr"; $dbh->{RaiseError} = 1; $dbh->{csv_tables}->{file1} = { file => 'vm.csv', }; $dbh->{csv_tables}->{file2} = { file => 'vfiler_ds.csv', }; my $query = "SELECT file1.vm_name, vm_cluster, vFiler_IP, vFiler_cluster FROM file1 join file2 WHERE file1.vm_name = file2.vm_name"; my $result = $dbh->selectall_arrayref($query); print dump($result);
  • Comment on Re^2: Compare 2 CSV files and create a new CSV file out of comparision

Replies are listed 'Best First'.
Re^3: Compare 2 CSV files and create a new CSV file out of comparision
by Anonymous Monk on Jan 19, 2014 at 21:50 UTC
    Now hopefully more readable This is an example that does not fully work: this is my improved Code ( Hope it will help somebody in the future) :
    use Data::Dump qw/dump/;
    use DBI;
    
    my $dbh = DBI->connect(
        "dbi:CSV:",
        "", "",
        {
    	    #f_schema => 'test',
            f_dir    => '.',
            f_ext    => '.csv',
        }
    ) or die "Could not open database: $DBI::errstr";
    $dbh->{RaiseError} = 1;
    
    
    $dbh->{csv_tables}->{file1} = {
    	file  => 'vm.csv',
    };
    
    $dbh->{csv_tables}->{file2} = {
    	file  => 'vfiler_ds.csv',
    };
    
    my $query = "SELECT file1.vm_name, vm_cluster, vFiler_IP, vFiler_cluster FROM file1 join file2 WHERE file1.vm_name = file2.vm_name";
    my $result = $dbh->selectall_arrayref($query);
    
    print dump($result);
    

      Overly complicated. Why use file and file1? That doesn't say anything in the queries, making them hard to read.

      my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { f_dir => ".", f_ext => ".csv/r", RaiseError => 1, PrintError => 1, }) or die "Could not open database: $DBI::errstr"; my $result = $dbh->selectall_arrayref (join " " => "SELECT vm.vm_name, vm_cluster, vFiler_IP, vFiler_cluster", "FROM vm JOIN vfiler_ds", "WHERE vm.vm_name = vfiler_ds.vm_name");

      Using f_ext - which you already do - just add /r to make the extension required, and just use the file names as table names in the query.

      As the f_*** attributes are just DBI attributes (passed to DBD::File eventually), you can put RaiseError and other DBI attributes in the same hash.


      Enjoy, Have FUN! H.Merijn