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

Hi guys, I have wrote the small script to get data from 2 columns of sql server, I like to save data into a new array for processing, but I am not sure how to do it as each column has the long trailing of white space. When I assign to new array it breaks column to new row

$VAR1 = [ 'Wellbeing Office + ', 'Pending + ', 'Library + ', 'Pending + ', 'Y219 + ', 'InProgress + ', 'B201 + ', 'InProgress + ', 'B108 + ', 'InProgress + ', 'LAB1 + ', 'InProgress + ', 'C303 + ', 'InProgress + ' ];
#!/usr/bin/perl use strict; use warnings; use DBI; #establish the connection my $dbh = DBI->connect('dbi:ODBC:myDSN','username','userpassword') || die "Could not connect to database: $DBI::errstr"; #$dbh->{'LongTruncOk'} = 0; #$dbh->{'LongReadLen'} = 100; # sql query statement my $sql = qq/select location,CompletionStatus from ewNetworkFaults where Type like '%Audio Visual%' and CompletionStatus not like '%Completed%'/; # prepare the query my $sth = $dbh->prepare($sql); #execute the query $sth->execute() || die "SQL Error: $DBI::errstr\n"; + my @row; my @table = (); # retrieve the values returned from executing your SQL statement while (my @row = $sth->fetchrow_array()) { push @table,@row; #### This is the place I dont know how to save } use Data::Dumper; print Dumper(\@table);
Nirvana is Now or Never

Replies are listed 'Best First'.
Re: how to save data to new array after retrieving from sql server
by kcott (Archbishop) on Jul 27, 2017 at 05:35 UTC

    G'day mhoang,

    "push @table,@row; #### This is the place I dont know how to save"

    If you wrote that as

    push @table, [@row];

    your output would look more like

    $VAR1 = [ [ 'Wellbeing Office ', 'Pending ' ], ... [ 'C303 ', 'InProgress ' ], ];

    Now each row is a discrete arrayref, ready for whatever subsequent processing you need to do.

    — Ken

Re: how to save data to new array after retrieving from sql server
by poj (Abbot) on Jul 27, 2017 at 06:53 UTC

    selectall_arrayref() combines "prepare", "execute" and "fetchall_arrayref" into a single call. It returns a reference to an array containing a reference to an array for each row of data fetched.

    Also, you could remove the trailing spaces with RTRIM

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use DBI; # establish the connection my $dbh = DBI->connect('dbi:ODBC:myDSN','username','userpassword', { RaiseError => 1, PrintError => 1 } ) or die "Could not connect to database: $DBI::errstr"; # sql query statement my $sql =<< "SQL"; SELECT RTRIM(location),RTRIM(CompletionStatus) FROM ewNetworkFaults WHERE Type like '%Audio Visual%' AND CompletionStatus not like '%Completed%' SQL my $table = $dbh->selectall_arrayref($sql); print Dumper($table);
    poj

      Hi Pol Let say I like to build a hash from that array reference. I wrote like this, but I got error. where am i wrong?

      my %hdata = (); for my $i (0 .. $#{$table}) { print $#{$table}; ### why cant find the length of array here my $location = $table[i]->[0]; my $room = $table[i]->[1]; push $hdata{$location}=$room; }
      Nirvana is Now or Never
        where am i wrong

        $#{$table} should be either $#${table} or just $#$table

        $table is an arrayref so these

        my $location = $table[i]->[0]; # missing $ on i my $room = $table[i]->[1];
        should be
        my $location = $table->[$i][0]; my $room = $table->[$i][1];

        and push is for building an array not a hash, so this

        push $hdata{$location}=$room;

        should be

        $hdata{$location} = $room;

        or alternatively

        #!/usr/bin/perl use strict; use Data::Dump 'pp'; my $table = [ ['Wellbeing Office', 'Pending'], ['Library','Pending'], ['Y219','InProgress'], ['B201','InProgress'], ['B108','InProgress'], ['LAB1','InProgress'], ['C303','InProgress'], ]; my $last_index = $#${table}; my $count = scalar @$table; print " records = $count last index = $last_index\n"; #my %hdata = (); #for my $i (0..$last_index){ # my $location = $table->[$i][0]; # my $room = $table->[$i][1]; # $hdata{$location} = $room; #} #pp \%hdata; my %hdata = (); for my $row (@$table){ my $location = $row->[0]; my $status = $row->[1]; $hdata{$location} = $status; } pp \%hdata;
        poj
        where am i wrong?
        ...
        print $#{$table}; ### why cant find the length of array here

        Your syntax for "highest array index" by reference is correct (as are the more common variations given by poj here), but be aware of the fact (pointed out by poj) that the highest array index is not the same as the array length (i.e., number of elements) for the default value of  $[ (see perlvar). (BTW:   Don't ever touch $[   :)

        c:\@Work\Perl\monks>perl -wMstrict -le "my $ar = [ qw(a b c d e) ]; ;; print $#{ $ar }; print $#${ ar }; print $#$ar; " 4 4 4

        For readability and maintainability, I tend to prefer the for-loop approach used by poj for populating a hash from an array or array reference, but here's a variation that's a bit more terse:

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $table = [ ['Wellbeing Office', 'Pending'], ['Library','Pending'], ['Y219','InProgress'], ['B201','InProgress'], ['B108','InProgress'], ['LAB1','InProgress'], ['C303','InProgress'], ]; ;; my %hdata = map @{ $_ }[0, 1], @$table ; ;; dd \%hdata; " { B108 => "InProgress", B201 => "InProgress", C303 => "InProgress", LAB1 => "InProgress", Library => "Pending", "Wellbeing Office" => "Pending", Y219 => "InProgress", }


        Give a man a fish:  <%-{-{-{-<

Re: how to save data to new array after retrieving from sql server
by thanos1983 (Parson) on Jul 27, 2017 at 15:37 UTC

    Hello mhoang,

    Well in this case I will agree with fellow monk poj the best/easiest solution for me is selectall_arrayref() but just for reference there is also another "more correct/efficient way" of fetching data than the way that you are using fetchrow_array(), see bind_columns().

    Sample of code that I put together just for demonstration purposes, convert it based on your example:

    my $sth = $dbh->prepare("SELECT `Test-Column-1`, `Test-Column-2` FROM +`" .$config{'MySQL.table'}."` WHERE 1"); if (!$sth->execute()) { die "Error: ". $sth->errstr ."\n"; } my ($column_1, $column_2); # Bind Perl variables to columns: $sth->bind_columns(\$column_1, \$column_2); my %row; # Column binding is the most efficient way to fetch data while ($sth->fetch) { $row{$column_1} = $column_2; } print Dumper \%row; __END__ $VAR1 = { 'value-1 Column1' => 'value-1 Column2', 'value-2 Column1' => 'value-2 Column2', 'value-3 Column1' => 'value-3 Column2' };

    Complete code for replication purposes:

    Update: Minor update of creating hash of hashes including name of table for easier sorting, when using multiple tables.

    my %row; # Column binding is the most efficient way to fetch data while ($sth->fetch) { $row{$config{'MySQL.table'}}{$column_1} = $column_2; } $ perl test.pl Database: PerlMonks exists not creating: PerlMonks Table: Data exists not creating: Data $VAR1 = { 'Data' => { 'value-1 Column1' => 'value-1 Column2', 'value-3 Column1' => 'value-3 Column2', 'value-2 Column1' => 'value-2 Column2' } };

    I am also using the module Config::Simple maybe you will find it useful as I did. Configuration sample bellow:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      I love the way Pol builds data structure. Very clean and clear, and also like the bind column, I will try soon.Thanks heaps  code

      Nirvana is Now or Never

        Hello again mhoang,

        No problems no worries, to be honest I also prefer using selectall_arrayref instead of bind_columns. But in cases that you are retrieving huge amount of data the only way to proceed is through bind_columns. Alternatively you will be utilizing huge amount of memory until all data have been processed and stored to the selectall_arrayref, when the other way you are just retrieving line by line the data. If in your case the retrieved data are not huge you should be fine.

        From the documentation bind_columns:

        # Column binding is the most efficient way to fetch data

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!