frasco has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks, some times ago I asked for Monks wisdom in order to find a convenient way to fetch data from a DB table. I had the following table structure holding words of a splitted text file:
|id |line_n |word |word_position -------------------------------------------- |1 |a1 |Lorem | 1 |2 |a1 |ipsum | 2 |3 |a1 |dolor | 3 |4 |a1 |sit | 4 |5 |a1 |amet, | 5 |6 |b2 |consectetuer | 1 |7 |b2 |adipiscing | 2 |8 |b2 |elit. | 3 |9 |c3 |Phasellus | 1 |10 |c3 |non | 2 |11 |c3 |erat | 3 |12 |c3 |... | 4
By retrieving data from db I needed that my output would be the same as follow (without GROUP_CONCT and similar database functions):
line 1 Lorem ipsum dolor sit amet, line 2 consectetuer adipiscing elit. line 3 Phasellus non erat ...
That is the original layout of my text file.
A wise monk suggested this code to print the output:
my $sql = qq(select * from table order by id); my $data_col = $dbh->selectcol_arrayref($sql, { Columns => [ 2, 3 ] }) +; my %seen; my @list = @$data_col; while (@list){ my $line_n = shift @list; my $word = shift @list; if ($seen{$line_n}++) { print " "; } else { print "\n$line_n - "; } print $word; }
Of course, It was ok until I had to immediately print the output, but I cannot figure out how to store retrieved data in a complex data structure for further use (i.e. to use such output as json, etc). For instance:
thanks, Francescomy $text = [ "myText_01", [ a1, ["Lorem", "ipsum", "dolor", "sit", "amet,"] ], [ b2, ["consectetuer", "adipiscing", "elit."] ], [ c3, ["Phasellus", "non", "erat"] ], [ 4, ["...", "..."] ] ];
|
|---|