Reputation: 325
I am a Perl newbie and despite that I manage to knock some code together, I am generally slowed down when I need to perform array manipulations -- clearly not yet familiar enough. Currently, I am writing a Perl code that queries a mySQL database and subsequently generates a LaTeX table using the package LaTeX::Table. In the process I need to manipulate arrays, and I am seeking advise to improve my understanding of dealing with arrays in Perl.
In the first part of the code I query a mySQL database using a subroutine:
my @stashYear = queryRecent($Ticker,'YEAR');
print "Last Year:\t";
### Dump the stash contents!
foreach my $array_ref ( @stashYear ) {
print "@$array_ref\n";
}
I have analogue code for a monthly and weekly query. The output looks of the above looks as follows:
bash-3.2$ ./test.pl DG.PA
Last Year: 2015 45.935 62.6 43.4 59.14 256
Last Month: 2016-03 63.9 66.69 62.36 65.47 21
Last Week: 2016-15 64.96 66.24 64 65.95 5
bash-3.2$
At this stage I wish to add and element to the arrays to obtain the following:
Year 2015 45.935 62.6 43.4 59.14 256
Month 2016-03 63.9 66.69 62.36 65.47 21
Week 2016-15 64.96 66.24 64 65.95 5
I have tried a variety of methods, but until now I am unsuccessful. I have played around with unshift, but none of my attempts have resulted in a satisfactory outcome.
In the remainder of the code, I combine the arrays:
#Combine Year, Month and Week Array into one
my @stashRecent = @stashYear;
push (@stashRecent, @stashMonth);
push (@stashRecent, @stashWeek);
#Convert Array in Latex::Table compatible format
my $recentData = \@stashRecent;
And subsequently I pass it on to LaTeX::Table to generate the required table:
$table = LaTeX::Table->new(
{
data => $recentData,
}
);
$table->generate();
print $table->generate_string();
It get the expected output:
bash-3.2$ ./test.pl DG.PA
\begin{table}
\centering
\begin{tabular}{lrrrrr}
\toprule
2015 & 45.935 & 62.6 & 43.4 & 59.14 & 256 \\
2016-03 & 63.9 & 66.69 & 62.36 & 65.47 & 21 \\
2016-15 & 64.96 & 66.24 & 64 & 65.95 & 5 \\
\bottomrule
\end{tabular}
\end{table}
bash-3.2$
Despite I manage to combine the arrays and convert them into the format required by LaTeX::Table, I have not succeeded to add the elements (Year, Month and Week), as described above.
Any feedback to get me going and improve my knowledge will be highly appreciated. In the mean time, I thank everybody who to give this some thought, and help a newbie with becoming a little less newbie.
Upvotes: 0
Views: 248
Reputation: 241858
I'm not sure I understand the question fully, but it seems the following should do the work:
unshift @stashYear, 'Year';
unshift @stashMonth, 'Month';
unshift @stashWeek, 'Week';
Upvotes: 1