invalid sort function on second call? - sorting

So I have a nested table
t = { a={},b={},c={},d={}}
..etc
Each item of t has a value in it named F(integer) (a.F, b.F etc)
Using lua table.sort() on t once with my sort function :
local function sort(a,b)
return a.F < b.F
end
Calling the sort once is fine, but if it is called again it throws invalid order function for sorting.
I'm not sure why this is so and what I must do to fix.
Info :
The values and items sorted are not nil (i assert() beforehand to make sure)

solved, it was due to some reference to other items in table t and modifications to values in table t in the sort function. It did not like changing the tables contents inside the function.

Related

Why does my forget() method not remove object from collection?

I have a collection of "Tickets", using the random collection utility method I select one from the list. The "Tickets" collection should now remove (or forget) that randomly selected ticket so I can further process that collection. Using the forget method doesn't appear to do what is described in the documentation or (more likely I'm missing something).
Can someone spot whats wrong in my code?
$tickets = Tickets::all();
$total_winners = 5;
$selected_tickets = $tickets->random($total_winners);
$jackpot_winner = $selected_tickets->random();
$selected_tickets->forget($jackpot_winner->id); // this line should remove the $jackpot_winner
When I print the contents of $selected_tickets on lines 3 and lines 5, they have the exact same items, including the $jackpot_winner.
Forget function uses the collection key not the id from the model. To achieve what you want you may use this method:
$selected_tickets = $selected_tickets->except($jackpot_winner->id);
https://laravel.com/docs/8.x/collections#method-except

Syntax for using SAMPLE() function with two or more orderBy expressions

I would like to use the SAMPLE() function in DAX to select random rows from a table. In the documentation the function appears to support multiple OrderBy_Selection parameters but I'm unable to get the syntax right.
I have a table named dCRQRisk with two columns that I would like to order by before selecting the sample rows: RSO, then RISK_LEVEL, both in Ascending order.
//This is the syntax
SAMPLE(<n_value>, <table>, <orderBy_expression>, [<order>[, <orderBy_expression>, [<order>]]…])`
//This works
SAMPLE(31,dCRQRisk, dCRQRisk[RSO],1)
//When I try to add the second OrderBy_Expression it does not work
SAMPLE(31, dCRQRisk, dCRQRisk[RSO],[1[,dCRQRisk[RISK_LEVEL],[1]]])
//This is the error message
Query(2, 60) Unexpected value for ORDER argument in SAMPLE function.
Use 0/FALSE/DESC for descending order or 1/TRUE/ASC for ascending
order.
Those brackets are to indicate optional arguments, not part of the syntax.
Try this:
SAMPLE(31, dCRQRisk, dCRQRisk[RSO], 1, dCRQRisk[RISK_LEVEL], 1)

Deleting multiple same id from mysql database

Below is my code for deleting all rows with ids one.It seems that only first index was deleted
and an error will trigger right away.How can i loop through the array and delete all rows with id one.
The array I've created here is only a representation from my database with multiple columns.
Any idea in resolving this problem is much appreciated.
public function deleteRow(){
$ids = ['1','1','1','3','3','1','1'];
foreach($ids as $id ){
$id = Scholarshipcount::find($id);
$id->delete();
}
}
my error
Call to a member function delete() on a non-object
Find() can fail to find the record, especially of you have multiple times the same PK as in your example (and you have already deleted the object the first time). You might want to consider using findOrFail instead.
You should use a different kind of approach instead. Assuming that your table name is scholarshipcounts then:
DB::table('scholarhipcounts')->whereIn('id', $ids)->delete();

Eloquent Collections Where Condition

I want to get alternative products pictures.
dd($alternativeProduct->pictures);
When die and dump i get this result.
I need to get only the picture which is main. If main equals to 1. It is main picture.
When I write
dd($alternativeProduct->pictures->where('main', 1))
I got an empty array.
Here is my relation with Product and Picture relation
public function pictures(){
return $this->hasMany('App\Models\ProductPicture');
}
What can i do ?
The where method in a collection has three parameters: $key, $value and $strict, the last one defaults to true if not passed when calling the method. When $strict is true the comparison is done via === which does not do type coercion, meaning "1" === 1 is false.
From your dump data, I can see that "main" => "1" which means it's a string and you're passing an integer 1 to the where method, resulting in what I described above as a false condition and returning an empty result set. You can fix that by disabling strict comparison:
$alternativeProduct->pictures->where('main', 1, false);
// or the equivalent helper whereLoose
$alternativeProduct->pictures->whereLoose('main', 1);
Or passing a string as the value:
$alternativeProduct->pictures->where('main', "1");
That being said, if that's the only place you're using that collection in that request's context, I suggest that you filter the results at the database level, not after they are fetched, like so:
$alternativeProduct->pictures()->where('main', 1)->get();
Accessing the relations as a method ->pictures(), instead of as a property ->pictures, will return a Query Builder instance that allows you to add conditions to the database query and only fetch the actual items you need from the database, instead of fetching them all and filtering them out afterwards.
You may want to use whereLoose instead:
dd($alternativeProduct->pictures->whereLoose('main', 1))
From the Laravel docs:
The where method uses strict comparisons when checking item values. Use the whereLoose method to filter using "loose" comparisons.

Deadloop after using orderby

I want to get a list of files in a folder in which the files are named as 0.html, 1.html, 2.html,... 10.html, 11.html.....
I want to sort them by number, not by preceding number.
so I write the query
var SeedPages = from pages in Directory.GetFiles(DownloadFolderString) orderby pages.Length select pages;
when I access SeedPages.First(), it keeps looping at pages.Length
I don't understand why the program goes back to the query statement.
The execution of this LINQ statement is deferred, you can put all resulting values in a list or array by calling .ToList() or ToArray() on your LINQ statement.

Resources