question

TOTH-EM avatar image
2 Likes"
TOTH-EM asked TOTH-EM commented

How to find a row in a table, and extract a value according to the item label?

Hi,

I have a table with some boxes that are labeled with an ID (box_id) and they all go through the same processor.

I also have a table with two collumns (Box_id and processing time) [cf. image].

How can I make my procecssor have a processing time that corresponds, regarding the table?

I naively tried this :

Table qer = Table.query("SELECT [Processing time] FROM [Magical Boxes] WHERE BOX_id == $1", item.labels["box_id"]);
return qer[1];

but it didn't work. I also tried to label the processor with the corresponding value, but the problem seems to come from the fact I cannot get the value from the table with the query.

Thanks,

--- EDIT ---

Using this in the processor worked.

query("FROM [Magical Boxes] WHERE BOX_id == $1", item.box_id);
return Table("Magical Boxes")[getquerymatchtablerow("Magical Boxes",1)][2];

--- EDIT 2 ---

After Mischa Spelt's comment, a better way would be to simply return qer[1][1] (faster and the first uses a deprecated command)

FlexSim 17.1.2
querysqltable commands
bx-query.png (4.7 KiB)
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Steven Hamoen avatar image
1 Like"
Steven Hamoen answered TOTH-EM commented

@TOTH-EM I think that item.labels() returns a pointer to the label and you need the value so I guess that item.box_id would be better here.

· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

TOTH-EM avatar image TOTH-EM commented ·

Thanks. That was indeed my error. Though the code I posted doesn't seem to work, I managed to solve it using getquerymatchtablerow().

0 Likes 0 ·
Steven Hamoen avatar image Steven Hamoen TOTH-EM commented ·

@TOTH-EM Have you seen the last last comment by @Mischa Spelt?

0 Likes 0 ·
TOTH-EM avatar image TOTH-EM Steven Hamoen commented ·

Yes and I think I have an idea why my code didn't work now

0 Likes 0 ·
Mischa Spelt avatar image
1 Like"
Mischa Spelt answered TOTH-EM commented

Since qer is a table, you need to get the value from the first row, first column:

return qer[1][1];
· 4
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

TOTH-EM avatar image TOTH-EM commented ·

I solved my problem, but I'm curious, why would selecting one collumn ([Magical Boxes])return a 2 dimensional table?

Oh wait, it's probabely the indexing, totally forgot about it. Thanks

0 Likes 0 ·
Mischa Spelt avatar image Mischa Spelt TOTH-EM commented ·

By the way, I saw your solution in the edit to the original post: note that this is relatively slow (first you do a query that already gives you the result, but you ignore it and have to go back to the table to read the result again) and also it uses the deprecated command getquerytablematchrow (the new way is SELECTing the ROW_NUMBER as one of the result columns). I would recommend just getting the result from the query like I described, or if you don't like/understand the query well enough, go with Clair's suggestion of findmatch.

0 Likes 0 ·
TOTH-EM avatar image TOTH-EM Mischa Spelt commented ·

That's what I ended up doing when I noticed the command is deprecated anyway and that your fix works. I didn't notice it was slower though, so it's good to know.

I edited my question once again to point it out.

Concerning to findmatch, I just tried it and it worked perfectly fine too.

Which one is faster? findmatch() or the query?

0 Likes 0 ·
Mischa Spelt avatar image Mischa Spelt commented ·

In your case, the WHERE also gives you just one matching object, so the result table will have one row, so you could also ask why you need indexing at all: why doesn't the query just give back a single value?

In fact, a SELECT query always returns a table. If you only select one column, this will be a table with one column. (One-column) tables are different from arrays and FlexSim doesn't automatically turn one into the other.

0 Likes 0 ·
Clair A avatar image
1 Like"
Clair A answered TOTH-EM commented

Hello,

I think that the findmatch command is what you're looking for.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

TOTH-EM avatar image TOTH-EM commented ·

Thanks, that worked perfectly too

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.