Finding the contents under div with specific id patterns using MOJO::DOM - perl

I need to parse some HTML codes. The patterns of the tag ID are:
<tr id="date">.....</tr>
<tr id="band01"><td>field1</td><td>field2</td></tr>
<tr id="band02">...contents...</tr>
.....
<tr id="(others">.....
I'm using PERL Mojo::DOM parser, and want to extract all the actual ids with names starting with "band" following by a number, as well as its contents.
How could I achieve this?

The E[foo^="bar"] selector matches any element with a "foo" attribute starting with "bar". Thus you can use:
my $dom = Mojo::DOM->new($html);
my $rows = $dom->find('tr[id^="band"]');
$rows would be a Mojo::Collection of Mojo::DOM objects representing each matching element and its respective contents. For example, to get the list of matched IDs:
my #ids = $rows->map(attr => 'id')->each;
Or with more standard Perl:
my #ids = map { $_->{id} } #$rows;

Related

How can I extract an attribute from multiple elements in Playwright?

I'm trying to extract some attributes from multiple elements using playwright.
( this is F# but you should be able to figure out what is wrong )
let! pagelinkRows = page.EvalOnSelectorAllAsync("tr[title]","(tr) => tr.getAttribute("title")") |> Async.AwaitTask
I get the error: tr.getAttribute is not a function. I have also tried tr.attr .
What is the simplest way to get the attributes from a collection of elements
<tr title="xxx1"></tr>
<tr title="xxx2"></tr>
<tr title="xxx3"></tr>
<tr title="xxx4"></tr>

setting variable name with variable at the end - perl

$id = 1;
foreach (#products) {
$productid$id = param(product$id);
++$id;
}
I am trying to loop through the products array and create a new variable for each item in products for a form which uses checkboxes to select products which are for sale. I tried to use $productid$id and $productid"$id" but it returned syntax errors. Is there a way to achieve this?
I am trying to loop through the products array and create a new variable for each item in products.
While it is possible to create a new variable for each item, it's a bad idea. They have to be global, the syntax is ugly, and it's difficult to pass those variables around as a collection. Instead, use a hash or an array. In this case, since your items are indexed by number and there's no gaps an array makes the most sense.
my #product_params;
foreach my $product (#products) {
push #product_params, param($product);
}
For each $product in #products, it will add param($product) to #product_params. For example, the parameter for $products[5] is stored in $product_params[5].
Since this is a one-to-one mapping of #products to its parameters, its easier and faster to do the above with a map. A mapping runs each element of a list through a function to create a new list. $_ contains each element of #products in turn.
my #product_params = map { param($_) } #products;
Variables aren't interpolated in single quotes.
You're resetting the $id in each iteration of the loop.
Update: Your changes invalidated both the bullets above.
my $id = 1;
for my $product (#products) {
$product_id = param("product$id");
++$id;
}
Say you have four checkboxes named
product1
product2
product3
product4
Say product1 and product3 are checked.
The following will place 1 and 3 in #selected_product_ids:
my #selected_product_ids =
map { my ($id) = /^product(\d+)\z/; defined($id) && param($_) ? $id : () }
params();
If you have the list of all existing product ids in #product_ids, the following will do the same:
my #selected_product_ids =
grep { param("product$_") }
#product_ids;

How to retrieve only part of a document back in a call

I need to retrieve only part of a document and call it via a helper so that I can render a subtemplate multiple times as the part I require to pull from the db is an array of object itself. I have the following as the fields. What I need to do with my helper is only retrieve the ordersDispatch array of one particular document which would be uniquely called by the tripNumber field.
I have tried several things but nothing has come close to only having an array of the objects in the orderDisptach field be returned in a fashion that it can be used by the helper to render my subtemplate for each object in the array.
{
tripNumber: companyRecord.lastTripNum + 1,
custID: $('input:hidden[name=orderCustomerId]').val(),
custContact: $('input:text[name=customerContact]').val(),
custEmail: $('input:text[name=customerEmail]').val(),
trailerSealNum: $('input:text[name=trailerSealNum]').val(),
orderBroker: $('input:text[name=orderBroker]').val(),
orderEquipment: $('input:text[name=orderEquipment]').val(),
orderLoadNum: $('input:text[name=orderLoadNum]').val(),
orderPlacedDate: $('input:text[name=orderPlacedDate]').val(),
orderPrivateNotes: $('textarea[name=orderPrivateNotes]').val(),
orderPublicNotes: $('textarea[name=orderPublicNotes]').val(),
orderCurrency: $("input[name=orderCurrency]:checked").val(),
orderCharges: $('input:text[name=orderCharges]').val(),
orderFUELCheck: $('input:checkbox[name=orderFUELCheck]').is(':checked'),
orderFUELPerc: $('input:text[name=orderFUELPerc]').val(),
orderFUELTotal: $('input:text[name=orderFUELTotal]').val(),
orderGSTCheck: $('input:checkbox[name=orderGSTCheck]').is(':checked'),
orderGSTPerc: $('input:text[name=orderGSTPerc]').val(),
orderGSTTotal: $('input:text[name=orderGSTTotal]').val(),
orderPSTCheck: $('input:checkbox[name=orderPSTCheck]').is(':checked'),
orderPSTPerc: $('input:text[name=orderPSTPerc]').val(),
orderPSTTotal: $('input:text[name=orderPSTTotal]').val(),
orderTAXCheck: $('input:checkbox[name=orderTAXCheck]').is(':checked'),
orderTAXPerc: $('input:text[name=orderTAXPerc]').val(),
orderTAXTotal: $('input:text[name=orderTAXTotal]').val(),
orderTotalCharges: $('input:text[name=orderTotalCharges]').val(),
ordeBlockInvoicing: $('input:checkbox[name=ordeBlockInvoicing]').is(':checked'),
orderExtraCharges: orderExtraCharges,
orderPickups: puLocations,
orderDeliveries: delLocations,
orderDispatch: dispatchLocations,
createdDate: new Date(),
createdUser: currentUser.username
Any help in building a helper that will accomplish this would be greatly appreciated as I am new to meteor and mongo.
The following helper should give you what you need:
Template.oneTrip.helpers({
orderDispatch: function(tn){
return Trips.findOne({ tripNumber: tn }).orderDispatch;
}
});
Trips.findOne({ tripNumber: tn }) gets you an individual document and .orderDispatch returns the value of the orderDispatch key which in your case will be an array.
html:
<template name="oneTrip">
{{#each orderDispatch this._id}} <!-- assuming you've already set the data context to an individual order -->
{{this}} <!-- now the data context is an element of the orderDispatch array -->
{{/each}}
</template>

Loop over the Mongodb objects to get the values in Meteorjs

I am first Time working With Meteorjs and MongoDB. I am working on a Q&A application. where I have a list of objects which i got using Meteor database call. I want to looper over the list and want to split one of its attribute to the length 50 and then want to send a list of these splitted objects to the database.what I have is this
Template.questions.questions = function () {
questions= Meteor.questions.find({topic_id: Session.get("currentTopicId")});
return questions
}
Every question object in the questions list has an attribute question_text. Using loop i want to split this attribute to length of fifty and then want to push that to an empty list and return that list . Like
questions_list=[]
for(i=0;i<questions.length;i++){
question=questions[i].question_text[0:50] // python syntex to get first 50 char of a string
questions_list.push(question
}
return questions_list
and My HTML is like
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{question_text}}
</td>
</tr>
{{/each}}
</tbody>
suggest me guys how can i acieve this in meteorjs. my problem is when i try to loop over this questions list there are many attributes like Collection, Results, Queries. here i am unable to iterate this list of objects.
In the same way how can i get the error message thrown back by meteorjs
This will give you list of shortened texts of all questions matching a query:
var questions = Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
You could use it directly in a helper:
Template.questions.questions = function () {
return Questions.find({...}).map(function(question) {
return question.text.substr(0,50);
});
};
By the way, Questions.find({...}) does not return a list of questions, but a cursor object that you can use to manipulate query data in an effective way (like that map method above). To get raw array, you need to use fetch on that cursor: Questions.find({...}).fetch().
If you're just displaying the data you could create a handlebars helper:
Template.questions.helpers({
first50: function(question) {
return question.text.substr(0,50);
}
});
Then add the helper:
Template.questions.questions = function() {
return Questions.find({...});
};
Then in your template you could do:
<tbody>
{{#each questions}}
<tr>
<td class="span2" style="overflow:hidden;width:50px">
{{first50 question_text}}
</td>
</tr>
{{/each}}
</tbody>

Using dom to get content from table with no id

I don't know how to get values from tables that don't have an ID.
I would like to use DOM to get the value "03" from
<tr><th scope='row'>Total WUs</th><td>03</td></tr>
but the table doesn't have an ID. It is the first table on the page. I'll give a code printout of the whole table:
<table cellspacing='0' rules='all' border='1' style='border-collapse:collapse;'><tr><th scope='row'>User Name</th><td>laughing_squid</td></tr><tr><th scope='row'>Project Rank</th><td>1,156,036</td></tr><tr><th scope='row'>Team Rank</th><td>2</td></tr><tr><th scope='row'>Total Points</th><td>207</td></tr><tr><th scope='row'>Total WUs</th><td>03</td></tr><tr><th scope='row'>Points Yesterday</th><td>NA</td></tr><tr><th scope='row'>Points Today</th><td>69</td></tr><tr><th scope='row'>PPD Average</th><td>138</td></tr></table>
Check this out, this script uses JQuery which can be downloaded from here:
$('table td').each(function(){
console.log($(this).text());
});
if you need only the value 03 then use following:
console.log($('table td:eq(4)').text());