Count number of columns in file and compare column names

How can i retrieve the number of columns in a file.
I need to compare if in two files are the same number of columns.If not then the rest of the workflow should not run.
If the number of columns are the same, then i need to compare if the column names of the two files are the same.

Anyone an idea how to achieve this

1 Like

Hi RonG,

To retrieve the number of columns you can use the “Extract Table Dimension” node.
To compare the column headers I would first extract them with the “Extract Column Header” node and then compare them with “Rule Engine” node.
To control whether or not to execute the rest of the workflow you can use a “IF Switch” node.

Is this already helpful or do you need a small example workflow?

Cheers,
Kathrin

4 Likes

Hi Kathrin,

thanks for your response, but a small example would be very helpful

Cheers,

Ron

Hi Kathrin,

if the two header rows are the same then I don’t know how to connect the rule engine node and how to set this up to compare all the column headers.
Finally when the columnheaders are the same, then the file should be opened and further processed.

Would be great if you could create a small example

Cheers,

Ron

testing 1234.knwf (63.8 KB)

Hi RonG,

I wonder if this would help you. Please do check.

Thanks and Regards,
Van

Hi Van,

thanks for the workflow. I tested it and you get the difference in number of columns between the two tables. But I wonder how to continue. If the number of columns is the same then the whole table should be further processed.

Thanks,

Ron

Hi Ron,

attached is a workflow, which checks whether all column headers are equal. If both tables have the same column headers, then they have also the same number of columns.

Please let me know in case you have any questions!

Cheers,
Kathrin

CompareColumnNames.knwf (26.7 KB)

3 Likes

Hi Kathrin,

thanks a lot for your help. Your workflow does the job. I only changed the connection from “Transpose to the IF-Switch” to “Table 2 to the IF-Switch”. Now I get the original table data and can continue handling these data.

Again thanks,

Ron

1 Like

Hi Kathrin,

I am new to KNIME and I truly appreciate your detailed responses and extremely helpful workflow!!

While I was working through your workflow, I noticed the console warnings on the “Row Filter” node creating an empty data table regardless of whether the tables created by the “Table Creator” nodes had equal columns or not…

I suspect this is because the table created by the “Joiner” node creates a row with a “Row ID” column similar to Column #_? and corresponding “Column Header” column, instead of missing values.

To resolve this issue, I set the “Row Filter” criteria to “Include rows by row ID” and used a regular expression to identify rows with missing values ([a-zA-Z]+\s+\d+[_]+[?]).

Just sharing this minor update since I find your workflow to be extremely helpful and wanted to clarify further for those who will reference in the future.

Please let me know if there are items I have misunderstood, and many thanks again!!

Best,
Zara

1 Like