Decoding JSON in Perl [closed] - json

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a JSON file like this one (it is a piece of the entire JSON file):
{
id => "mgp1310",
samples => [
{
envPackage => {
data => {
diss_carb_dioxide => {
aliases => ["sediment_diss_carb_dioxide"],
definition => "concentration of dissolved carbon dioxide",
mixs => 1,
required => 0,
type => "text",
unit => "",
value => "17 mM",
},
},
id => "mge64559",
},
},
],
}
This has been decoded by module JSON, using:
use Data::Dumper;
use JSON;
open($fh, '<', 'hola.txt' );
$json_text = <$fh>;
$perl = decode_json($json_text);
print Dumper($perl);
Now I know that $perl has a hash. So I'd like to print the id of the JSON file using print $perl{"id"};. However, it prints nothing I don't know why.

I found the answer by adding use strict at my code. It threw the following error:
Global symbol "%perl" requires explicit package name at json.pl line 12.
The variable $perl is a scalar, not a hash!. Of course... I didn't thought about that. So I cannot access to the hash writing $perl{"id"}. The correct way is $perl->{id}.

Related

#1146 - Table 'crud_app.names' doesn't exist [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am following this tutorial here, crud app with node js and mysql.
My database, crud_app has a table named crud_table, and crud_table has got 3 columns, id, names, and date_added.
I am trying to make a SQL query using node js
async getAllData() {
try {
const response = await new Promise((resolve, reject) => {
const query = "SELECT * FROM names"
connection.query(query, (err, results) => {
if (err) { reject(new Error(err.message))}
resolve(results)
})
});
console.log(response);
return response;
} catch (error) {
console.log(error)
}
}
But I get this error
Error: ER_NO_SUCH_TABLE: Table 'crud_app.names' doesn't exist
And again I went to PHPMyAdmin to run this query in the console.
SELECT * FROM names
Output
It is claiming that the names column does not exist but you can clearly see the column here
Is there any way I can resolve this problem?
Your table name is crud_table and within your table you have the column names. You cant select all the date from a column by using SELECT * on the column,with SELECT you select data from your table in your case crud_table,so if you want all the names then try this "SELECT names FROM crud_table"

Is there an NPM package to log a SQL database in node? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm making an app that retrieves information from SQL databases and displays the info in node, but I'm a bit stuck in trying to find a way to print the entire table as a table instead of the loose individual data. Is there an npm package that might do this that you might recommend? If not, is there an obvious way to do this I'm not seeing?
Thanks for the help.
I've used https://www.npmjs.com/package/table for this. See example for logging mysql output ( I'm using it in my simple mysql repl - https://github.com/sidorares/myki ):
const connection = mysql.createConnection(Object.assign({}, config, { rowsAsArray: true }));
const chalk = require('chalk');
function myEval(cmd, context, filename, callback) {
connection.query(cmd, (err, rows, fields) => {
if (err) {
if (!err.fatal) {
console.log(chalk.red(err.message));
return;
} else {
return callback(err);
}
}
console.log(
table([fields.map(f => chalk.bold(f.name))].concat(rows.map(r => r.map(c => util.inspect(c, { colors: true })))))
);
callback(null);
});
}

Laravel 5 Eloquent - whereIn on join table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create an online directory, where, for example, people can search through the website and find all takeaways that have a specific type. For example:
"Indian",
"Chinese"
etc..
I have 3 tables:
Business
Tags
Business Tags
And my model is as follows:
class Business extends Model
{
protected $table = 'businesses';
protected $fillable = [
'business_name', 'postcode'
];
public function tags()
{
return $this->belongsToMany('App\Tags');
}
}
The issue is, whenever I come to do the search, and try to do a whereIn the issue is that it takes forever to load, in fact, it doesn't even load. For example:
$business = Business::whereHas('tags', function($tag) use ($request) {
if($request->get('terms'))
{
$tag->whereIn('tags.name', ['chinese']);
}
})->get();
So my question is this:
I have just over 10k rows of data stored inside the table. This table is split into three "Business", "Tags", "Business Tags". The process above is taking so long to complete, probably because I use the whereHas('tags') and whereIn therefore, how do I go about using the following syntax:
$business = Business::where( function ($business) use ($request) {
// Search for businesses with a specific tag, passed from request
});
Is this possible?
I'm just wild guessing here, but try to pull the condition outside of the function and don't specify the name of the table:
if($request->get('terms'))
{
$business = Business::whereHas('tags', function($tag) use ($request) {
$tag->whereIn('name', ['chinese']);
})->get();
}

Can't use Perl syntax to test html web page with Selenium? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to create a test to check a form using perl. And i have to catch an id from html/css. In Selenium. How i can do it?
In my #login => 'fahd', #login is an empty array, so only 'fahd' is returned. Therefore, you're calling
$s->submit_form_ok({ input => { 'fahd' => undef }, # plus the "odd number" warning
click => '#submit',
});
(fahd was removed from the source, but is still readable in the output.)
input => { my #login => 'fahd' }
is the same thing as
input => { 'fahd' }
since the newly-created array by #login is empty.
The hash initializing expression is expected to return key-value pairs, but only a single value is returned. That's why you're getting the error message "odd number of elements in anonymous hash".
And since the keys of the hash are expected to be locators, that's why you get the error message "Element fahd not found".
I suspect you meant to use
$s->submit_form_ok({
input => { '#login' => 'fahd' },
click => '#submit',
})
But that brings up a second problem. I don't think #login and #submit are valid Selenium locators, which is why you are getting "Element #submit not found". Selenium accepts multiple styles of selectors, one of which being CSS selectors. In CSS selectors, #id finds nodes by id.
$s->submit_form_ok({
input => { '#login' => 'fahd' },
click => '#submit',
})

execute query with cakephp [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to execute a query to get my data
how can I write this query with cakephp
select materiel_id , SUM(quantite) from paiements
GROUP BY
materiel_id
ORDER BY sum(quantite)
Thinks,
PS : I use cakephp 2.3.6
Well assuming you have the model relationship Paiement belongsTo Materiel and you are in the PaiementsController
$this->Paiement->find('all',
array(
'group' => array('Paiement.materiel_id'),
'fields' => array('Paiement.materiel_id),
'contain' => array(
'Materiel' => array(
'fields' => array(
'SUM(Paiement.quantite) AS sum'
),
'order' => array(
'sum' => 'desc'
),
), // Materiel
) // contain
) // end array
); // end find
you ca us this code
$mat=$this->Materiel->Paiement->query("
select *,materiel_id , SUM(paiements.quantite) as t from paiements ,materiels
where paiements.materiel_id=materiels.id
GROUP BY
materiel_id
ORDER BY sum(paiements.quantite) desc
");
Ajust this code for your model