Replace variables extracted from database with theirs values - mysql

I'd like to translate a perl web site in several languages. I search for and tried many ideas, but I think the best one is to save all translations inside the mySQL database. But I get a problem...
When a sentence extracted from the database contains a variable (scalar), it prints on the web page as a scalar:
You have $number new messages.
Is there a proper way to reassign $number its actual value ?
Thank you for your help !

You can use printf format strings in your database and pass in values to that.
printf allows you to specify the position of the argument so only have know what position with the list of parameters "$number" is.
For example
#!/usr/bin/perl
use strict;
my $Details = {
'Name' => 'Mr Satch',
'Age' => '31',
'LocationEn' => 'England',
'LocationFr' => 'Angleterre',
'NewMessages' => 20,
'OldMessages' => 120,
};
my $English = q(
Hello %1$s, I see you are %2$s years old and from %3$s
How are you today?
You have %5$i new messages and %6$i old messages
Have a nice day
);
my $French = q{
Bonjour %1$s, je vous vois d'%4$s et âgés de %2$s ans.
Comment allez-vous aujourd'hui?
Vous avez %5$i nouveaux messages et %6$i anciens messages.
Bonne journée.
};
printf($English, #$Details{qw/Name Age LocationEn LocationFr NewMessages OldMessages/});
printf($French, #$Details{qw/Name Age LocationEn LocationFr NewMessages OldMessages/});
This would be a nightmare to maintain so an alternative might be to include an argument list in the database:
#!/usr/bin/perl
use strict;
sub fetch_row {
return {
'Format' => 'You have %i new messages and %i old messages' . "\n",
'Arguments' => 'NewMessages OldMessages',
}
}
sub PrintMessage {
my ($info,$row) = #_;
printf($row->{'Format'},#$info{split(/ +/,$row->{'Arguments'})});
}
my $Details = {
'Name' => 'Mr Satch',
'Age' => '31',
'LocationEn' => 'England',
'LocationFr' => 'Angleterre',
'NewMessages' => 20,
'OldMessages' => 120,
};
my $row = fetch_row();
PrintMessage($Details,$row)

Related

decode_json and return first key in hash

JSON string input: https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=demo
I am trying to return just the first key (current day) in the hash but have been unable to do so. My code looks like the following
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use Data::Dumper;
use JSON;
my $html = get("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=AMD&apikey=CMDPTEHVYH7W5VSZ");
my $decoded = decode_json($html);
my ($open) = $decoded->{'Time Series (Daily)'}->[0]->{'1. open'};
I keep getting "Not an ARRAY reference" which I researched and got more confused.
I can access what I want directly with the below code but I want to access just the first result or the current day:
my ($open) = $decoded->{'Time Series (Daily)'}{'2017-12-20'}{'1. open'};
Also if I do something like this:
my ($open) = $decoded->{'Time Series (Daily)'};
print Dumper($open);
The output is as follows:
$VAR1 = {
'2017-09-07' => {
'1. open' => '12.8400',
'5. volume' => '35467788',
'2. high' => '12.9400',
'4. close' => '12.6300',
'3. low' => '12.6000'
},
'2017-11-15' => {
'3. low' => '10.7700',
'4. close' => '11.0700',
'2. high' => '11.1300',
'5. volume' => '33326871',
'1. open' => '11.0100'
},
'2017-11-30' => {
'1. open' => '10.8700',
'2. high' => '11.0300',
'5. volume' => '43101899',
'3. low' => '10.7600',
'4. close' => '10.8900'
},
Thank you in advance for any help you can provide a noob.
Problem 1: { denotes the start of a JSON object, which gets decoded into a hash. Trying to derefence an array is going to fail.
Problem 2: Like Perl hashes, JSON objects are unordered, so talking about the
"first key" makes no sense. Perhaps you want the most recent date?
use List::Util qw( maxstr );
my $time_series_daily = $decoded->{'Time Series (Daily)'};
my $latest_date = maxstr #$time_series_daily;
my $open = $time_series_daily->{$latest_date}{'1. open'};
You are picking among hashref keys, not array (sequential container) elements. Since hashes are inherently unordered you can't index into that list but need to sort keys as needed.
With the exact format you show this works
my $top = (sort { $b cmp $a } keys %{ $decoded->{'Time Series (Daily)'} } )[0];
say $decoded->{'Time Series (Daily)'}{$top}{'1. open'};
It gets the list of keys, inverse-sorts them (alphabetically), and takes the first element of that list.
If your date-time format may vary then you'll need to parse it for sorting.
If you will really ever only want the most-recent one this is inefficient since it sorts the whole list. Then use a more specific tool to extract only the "largest" element, like
use List::Util qw(reduce);
my $top = reduce { $a gt $b ? $a : $b }
keys %{ $decoded->{'Time Series (Daily)'} };
But then in your case this can be done simply by maxstr from the same List::Util module, as shown in ikegami's answer. On the other hand, if the datetime format doesn't lend itself to a direct lexicographical comparison used by strmax then the reduce allows use of custom comparisons.

How to create dynamically sized HTML table?

I have a Perl CGI script. I would like to make a dynamic, appropriately-sized table based on query information from a simple HTML form: http://jsfiddle.net/wBgBZ/4/. I wanted to use HTML::Table but the server doesn't have the module installed. The administrator won't install it either. Therefore, I have to do it the old fashion way.
Here's what I have so far.
#!/usr/bin/perl
use strict; use warnings;
use CGI qw( :standard);
print header;
print start_html(
-title => 'Creating Tables'
);
# Process an HTTP request
my $query = param("names");
my #students_in_class = split(/;/, $query);
my %attributes = (
'Tommy' => 'A star baseball player who has lots of potential to play in the Major League of Baseball. ',
'Tyrone' => 'An honor roll athlete. His father is really proud of him. When he graduates, he wents to work at the National Institute for Public Health. His father wants him to become a doctor but he wants to pursue Physics.',
'Marshall' => 'A professional WWE wrestler.',
);
print table({-border=> undef},
caption('Students in the class'),
Tr({-align=>'CENTER',-valign=>'TOP'},
[
th(['Student', 'List of Attributes']),
foreach (#students_in_class){ # !!!!! problem line !!!!!!
td(['$_' , '$attributes{$}']),
}
]
)
);
Such that if the user enters the following into the search bar: Tyrone;Tommy;Marshall
the CGI should produces something similar to the following
Desired Output
http://jsfiddle.net/PrLvU/
If the user enters just Marshall;Tommy, the table should be 3x2.
It doesn't work. I need a way to dynamically add rows to the table.
This is untested, but I think this is what you are wanting. You may need to change some of the table attributes to your desired needs.
use strict;
use warnings;
use CGI qw( :standard );
print header,
start_html(-title => 'Creating Tables');
my $query = param('names');
my #headers;
my #students = split(/;/, $query);
my %attributes = (
Tommy => 'A star baseball player.',
Tyrone => 'An honor roll athlete.',
Marshall => 'A professional WWE wrestler.',
);
$headers[0] = Tr(th('Student'), th('List of Attributes'));
for my $i (#students) {
push #headers, Tr( td($i), td($attributes{$i}));
}
print table( {-border => undef}, #headers );

Appending one array to another array in the same loop

This is for a custom Wordpress page but I think the basic array principles should apply. I've not worked with complex arrays before so am a little lost, trial and error hasn't worked yet.
I have a database of Posts, each post has meta_key's of 'shop' and 'expired'.
'expired' is a date (YYYY-MM-DD) which is used to tell the visitor when a Post's content expires and this key is what I'm trying to work with.
If a Post's 'expired' date is before today, they are shown 'Offer expired'
If the 'expired' date is in the future, they are shown 'Offer expires in X days' (this script isn't shown below, not necessary)
Posts are listed in order of their 'expired' date, ASC. The problem is that when a post expires I'd like that post to show at the end rather than stay on top.
Example of what I currently see:
Post 1 | Expired 3 days ago
Post 2 | Expired 1 day ago
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
And what I'd like to see (note Post X order):
Post 3 | Expires in 2 days
Post 4 | Expires in 6 days
Post 2 | Expired 1 day ago
Post 1 | Expired 3 days ago
This is my array code where I've attempted to merge the two
$postid = get_the_ID();
$meta1 = get_post_meta($postid, 'shop', true);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$today = date('Y-m-d', time());
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'shop',
'value' => $meta1
)
),
'paged' => $paged,
'posts_per_page' => '5',
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$args2 = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'shop',
'value' => $meta1
)
),
'meta_key' => 'expired',
'meta_value' => $today,
'meta_compare' => '<',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$final = $args + $args2;
$query = new WP_Query( $final );
while ( $query->have_posts() ) : $query->the_post(); ?>
HTML FOR DISPLAYING POST
endwhile;
At the moment it doesn't seem to take any notice of "$args2" and only displays $args
I'm sure my idea is on the right lines, needing to create two arrays and join them with the "+" rather than array_merge() but that's where I can't get any further.
Can someone kindly shed some light please? Thanks!
Now the solution you are trying to achieve is actually impossible if i understood your requirement properly. Let me explain why this is not achievable.
In your two arrays $args and $args2 most of the values are same leaving two odds , i am picking only one to just illustrate :
//it's in args
'meta_compare' => '>'
//it's in args2
'meta_compare' => '<'
Now what happens when you are trying to merge this two using array_merge($args , $args2):
'meta_compare' => '<'
That means it is taking 'meta_compare' from the later array which is $args2 here. This is the behavior of array_merge function defined in the doc:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one
Now if you are using + array union operator $args+$args2 :
'meta_compare' => '>'
That means it is taking 'meta_compare' from the first array which is $args here. This is the behavior of + array union operator defined in the doc :
The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.
Why it is happening because in the same level keys have to be unique . So in your situation they are in the same level :
$args = array ('key' => 'value1')
$args2= array ('key' => 'value2')
So only and only one value can exist here as in the merged array 'key' can point only one.
If this was the scenario :
$args [0] = array ('key' => 'value1' )
$args2 [1]= array ('key' => 'value2' )
Then both of the value stored by key will be resrved. This will be the resulting array :
array(2) {
[0]=>
array(1) {
["key"]=>
string(6) "value1"
}
[1]=>
array(1) {
["key"]=>
string(6) "value2"
}
}
Though i am not a geek of WP but as far as i understood from WP_query you can't pass args like this (I am not sure). So that leaves only one way to achieve this. You can pass these two args separately and merge the result as the result most probably (I am not sure) will be a 2D array you can merge them easily.
Hope that helps.
Happy coding :)
You can't just add 2 arrays together using the args+args2 syntax. PHP has no way of knowing what you mean by that. If you want the result to be an array containing both of these arrays, you could do this:
$final = array($args, $args2)
Otherwise I'm not sure how you want these two arrays combined. If you clarify how they need to be combined we might be able to help more.

Parsing PubMed XML to submit to mySQL database (XML::Twig)

I'm new to XML::Twig, and I'm trying to parse a PubMed XML 2.0 esummary final to place into a mySQL database. I've gotten this far:
#!/bin/perl -w
use strict;
use DBI;
use XML::Twig;
my $uid = "";
my $title = "";
my $sortpubdate = "";
my $sortfirstauthor = "";
my $dbh = DBI->connect ("DBI:mysql:medline:localhost:80",
"root", "mysql");
my $t= new XML::Twig( twig_roots => { 'DocumentSummary' => $uid => \&submit },
twig_handlers => { 'DocumentSummary/Title' => $title, 'DocumentSummary/SortPubDate' => $sortpubdate, 'DocumentSummary/SortFirstAuthor' => $sortfirstauthor});
$t->parsefile('20112.xml');
$dbh->disconnect();
exit;
sub submit
{ my $insert= $dbh->prepare( "INSERT INTO medline_citation (uid, title, sortpubdate, sortfirstauthor) VALUES (?, ?, ?, ?);");
$insert->bind_param( 1, $uid);
$insert->bind_param( 2, $title);
$insert->bind_param( 3, $sortpubdate);
$insert->bind_param( 4, $sortfirstauthor);
$insert->execute();
$t->purge;
}
But Perl seems to stall for some reason. Am I doing this right? I'm trying to use twig_roots to decrease the amount of parsing since I'm only interested in a few fields (these are large files).
Here is an example of the XML:
<DocumentSummary uid="22641317">
<PubDate>2012 Jun 1</PubDate>
<EPubDate></EPubDate>
<Source>Clin J Oncol Nurs</Source>
<Authors>
<Author>
<Name>Park SH</Name>
<AuthType>
Author
</AuthType>
<ClusterID>0</ClusterID>
</Author>
<Author>
<Name>Knobf MT</Name>
<AuthType>
Author
</AuthType>
<ClusterID>0</ClusterID>
</Author>
<Author>
<Name>Sutton KM</Name>
<AuthType>
Author
</AuthType>
<ClusterID>0</ClusterID>
</Author>
</Authors>
<LastAuthor>Sutton KM</LastAuthor>
<Title>Etiology, assessment, and management of aromatase inhibitor-related musculoskeletal symptoms.</Title>
<SortTitle>etiology assessment and management of aromatase inhibitor related musculoskeletal symptoms </SortTitle>
<Volume>16</Volume>
<Issue>3</Issue>
<Pages>260-6</Pages>
<Lang>
<string>eng</string>
</Lang>
<NlmUniqueID>9705336</NlmUniqueID>
<ISSN>1092-1095</ISSN>
<ESSN>1538-067X</ESSN>
<PubType>
<flag>Journal Article</flag>
</PubType>
<RecordStatus>
PubMed - in process
</RecordStatus>
<PubStatus>4</PubStatus>
<ArticleIds>
<ArticleId>
<IdType>pii</IdType>
<IdTypeN>4</IdTypeN>
<Value>N1750TW804546361</Value>
</ArticleId>
<ArticleId>
<IdType>doi</IdType>
<IdTypeN>3</IdTypeN>
<Value>10.1188/12.CJON.260-266</Value>
</ArticleId>
<ArticleId>
<IdType>pubmed</IdType>
<IdTypeN>1</IdTypeN>
<Value>22641317</Value>
</ArticleId>
<ArticleId>
<IdType>rid</IdType>
<IdTypeN>8</IdTypeN>
<Value>22641317</Value>
</ArticleId>
<ArticleId>
<IdType>eid</IdType>
<IdTypeN>8</IdTypeN>
<Value>22641317</Value>
</ArticleId>
</ArticleIds>
<History>
<PubMedPubDate>
<PubStatus>entrez</PubStatus>
<Date>2012/05/30 06:00</Date>
</PubMedPubDate>
<PubMedPubDate>
<PubStatus>pubmed</PubStatus>
<Date>2012/05/30 06:00</Date>
</PubMedPubDate>
<PubMedPubDate>
<PubStatus>medline</PubStatus>
<Date>2012/05/30 06:00</Date>
</PubMedPubDate>
</History>
<References>
</References>
<Attributes>
<flag>Has Abstract</flag>
</Attributes>
<PmcRefCount>0</PmcRefCount>
<FullJournalName>Clinical journal of oncology nursing</FullJournalName>
<ELocationID></ELocationID>
<ViewCount>0</ViewCount>
<DocType>citation</DocType>
<SrcContribList>
</SrcContribList>
<BookTitle></BookTitle>
<Medium></Medium>
<Edition></Edition>
<PublisherLocation></PublisherLocation>
<PublisherName></PublisherName>
<SrcDate></SrcDate>
<ReportNumber></ReportNumber>
<AvailableFromURL></AvailableFromURL>
<LocationLabel></LocationLabel>
<DocContribList>
</DocContribList>
<DocDate></DocDate>
<BookName></BookName>
<Chapter></Chapter>
<SortPubDate>2012/06/01 00:00</SortPubDate>
<SortFirstAuthor>Park SH</SortFirstAuthor>
</DocumentSummary>
Thanks!
The way I'd do this is to have a single handler, for the DocumentSummary, that feeds the DB, then purges the record. There is no need to get fancier than this.
Also, I find DBIx::Simple, well, simpler to use than raw DBI, it takes care of preparing and caching statements for me:
#!/bin/perl
use strict;
use warnings;
use DBIx::Simple;
use XML::Twig;
my $db = DBIx::Simple->connect ("dbi:SQLite:dbname=t.db"); # replace by your DSN
my $t= XML::Twig->new( twig_roots => { DocumentSummary => \&submit },)
->parsefile('20112.xml');
$db->disconnect();
exit;
sub submit
{ my( $t, $summary)= #_;
my $insert= $db->query( "INSERT INTO medline_citation (uid, title, sortpubdate, sortfirstauthor) VALUES (?, ?, ?, ?);",
$summary->att( 'uid'),
map { $summary->field( $_) } ( qw( Title SortPubDate SortFirstAuthor))
);
$t->purge;
}
If you're wondering about map { $summary->field( $_) } ( qw( Title SortPubDate SortFirstAuthor)), it's just a fancier (and IMHO more maintainable) way to write $summary->field( 'Title'), $summary->field( 'SortPubDate'), $summary->field( 'SortFirstAuthor' )
Your syntax of handlers is wrong. See the documentation for examples:
my $twig=XML::Twig->new(
twig_handlers =>
{ title => sub { $_->set_tag( 'h2') }, # change title tags to h2
para => sub { $_->set_tag( 'p') }, # change para to p
hidden => sub { $_->delete; }, # remove hidden elements
list => \&my_list_process, # process list elements
div => sub { $_[0]->flush; }, # output and free memory
},
pretty_print => 'indented', # output will be nicely formatted
empty_tags => 'html', # outputs <empty_tag />
);

Codeigniter database issue

Having a spot of bother trying to grab some data out of my database.
I have the following model:
function GetRestaurants($options = array())
{
// Qualification
if(isset($options['restaurantID']))
$this->db->where('restaurantID', $options['restaurantID']);
if(isset($options['restaurantRegionID']))
$this->db->where('restaurantRegionID', $options['restaurantRegionID']);
if(isset($options['restaurantName']))
$this->db->where('restaurantName', $options['restaurantName']);
if(isset($options['restaurantAddress1']))
$this->db->where('restaurantAddress1', $options['restaurantAddress1']);
if(isset($options['restaurantAddress2']))
$this->db->where('restaurantAddress2', $options['restaurantAddress2']);
if(isset($options['restaurantSuburb']))
$this->db->where('restaurantSuburb', $options['restaurantSuburb']);
if(isset($options['restaurantCity']))
$this->db->where('restaurantCity', $options['restaurantCity']);
if(isset($options['restaurantInformation']))
$this->db->where('restaurantInformation', $options['restaurantInformation']);
// limit / offset
if(isset($options['limit']) && isset($options['offset']))
$this->db->limit($options['limit'], $options['offset']);
else if(isset($options['limit']))
$this->db->limit($options['limit']);
// sort
if(isset($options['sortBy']) && isset($options['sortDirection']))
$this->db->order_by($options['sortBy'], $options['sortDirection']);
$query = $this->db->get("tblRestaurants");
if(isset($options['count'])) return $query->num_rows();
if(isset($options['restaurantID']))
return $query->row(0);
if(isset($options['limit']) && $options['limit'] == '1')
return $query->row(0);
return $query->result();
}
Now the following code works fine:
$this->load->model('Restaurants_model');
$data['restaurant'] = $this->Restaurants_model->GetRestaurants(array(
'restaurantName' => 'shed 5',
'limit' => '1'
));
However the following does not work:
$this->load->model('Restaurants_model');
$data['restaurant'] = $this->Restaurants_model->GetRestaurants(array(
'restaurantName' => str_replace('-', ' ', $this->uri->segment(2)),
'limit' => '1'
));
Even though the result of
str_replace('-', ' ', $this->uri->segment(2))
is in this instance: ‘shed 5’.
I have compared var_dumps of the output of the str_replace and the string itself and determined them to be identical. So why does the straight string return a result yet the string generated from the uri segment doesn’t? Some kind of encoding issue? My database holds data in ‘utf8_general_ci’.
Thanks for any suggestions!
$restaurant=str_replace('-', ' ', $this->uri->segment(2));
get value outside array and try array_push
$data['restaurant'] = $this->Restaurants_model->GetRestaurants(array(
'restaurantName' => $restaurant,
'limit' => '1'
));