Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Loader script - Design question

by Cody Fendant (Hermit)
on Feb 23, 2016 at 01:56 UTC ( [id://1155875]=note: print w/replies, xml ) Need Help??


in reply to Loader script - Design question

You don't need selectall_hashref if you're just looking at one record, which is what you should be doing if you have an ID. You need selectrow_hashref.

It might be useful if you could say what you mean by "the result is messed"

The selectrow_hashref should give you something like:

$ref = { 'column_1' => 'foo', 'column_2' => 'bar', 'id' => 12345 };

At which point you can just check whether your column_1 from the CSV file matches $ref->{column_1} etc.

Replies are listed 'Best First'.
Re^2: Loader script - Design question
by artanisace (Initiate) on Feb 23, 2016 at 12:38 UTC

    This is what I meant by the result is messed, if I do this for example:

    # Update DB with file content my @keys = ('ID'); my $hash = $dbh->selectall_hashref("SELECT * FROM users", \@keys); print Dumper \$hash;

    This happens:

    http://postimg.org/image/3rhdfupnx/

    If I add more values to @keys, the 'chain' of hashref gets bigger... and I don't really know what's happening :S

      the 'chain' of hashref gets bigger.

      That's what selectall_hashref does

      The $key_field parameter defines which column, or columns, are used as keys in the returned hash. It can either be the name of a single field, or a reference to an array containing multiple field names. Using multiple names yields a tree of nested hashes.

      What structure do you want ?

      poj

        I want something like I have when I use text::CSV to read the file and turn it into hashref:

        # Read file from path my @rows; my $csv = Text::CSV->new ( { binary => 1, sep_char => "\t" } ) or die "Error creating TSV object: ".Text::CSV->err +or_diag (); open my $fh, "<:encoding(utf8)", "$filename" or die "Error reading CSV file: $!"; $csv->column_names ($csv->getline ($fh)); # Get header names while ( my $record = $csv->getline_hr( $fh ) ) { # Process record push @rows, $record; } $csv->eof or $csv->error_diag(); close $fh; print Dumper \@rows;

        This is the output after reading the file:

        $VAR1 = [ { 'ID' => '4564', 'Date' => '4-05-1986, 'Name' => 'Mengano' } ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found