How to split array list into multiple rows and columns in scala - scala

I am reading this XML file using scala and I had to define custom schema's for each table attribute as they are different.
<CATALOG>
<TABLE at_name="Furniture">
<ROWDATA>
<ROW typeid="0" caseid="0" key="1" code="0"/>
<ROW typeid="1" caseid="0" key="1" code="0"/>
<ROW typeid="1" caseid="1" key="1" code="0"/>
</ROWDATA>
</TABLE>
<TABLE at_name="Cutlery">
<ROWDATA>
<ROW cutleryTypeid="0" color="Blue" code="0"/>
<ROW cutleryTypeid="0" color="Blue" code="0"/>
</ROWDATA>
</TABLE>
<TABLE at_name="Apparel">
<ROWDATA>
<ROW ApparelTypeid="1" color="Blue" barcode="0111"/>
<ROW ApparelTypeid="0" color="Blue" barcode"1122"/>
</ROWDATA>
</TABLE>
<CATALOG>
I now have the following array list created like this(for attribute furniture):
val gl = DF.select("ROWDATA").rdd.map(r => r(0)).collect()
for(element<-gl)
{
println(element)
}
When I print the elements I get this:
[WrappedArray([0,0,1,0], [1,0,1,0], [1,1,1,0])]
I want this transformed into two outputs.
Output 1:
id elements
1 [0,0,1,0]
2 [1,0,1,0]
3 [1,0,1,0]
Then I want convert it like this:
Output 2:
id col1 col2 col3 col4
1 0 0 1 0
2 1 0 1 0
3 1 0 1 0
Any help on this please?

Related

MaterialUI Table rows with different amount of cells aligned

I am trying to create a table where I essentially have two headings of different cell size and the items in the table must follow the second heading, like this:
**Main Heading 1** | **Main Heading 2**
sub 11 | sub 12 | sub 13 | sub 14 | sub 21 | sub 22 | sub 23 | sub 24
item | item | item | item | item | item | item | item
...
I have tried with the following structure
<Table size="small" aria-label="stock-flow">
<TableHead>
<TableRow>
<TableCell>Main Heading 1</TableCell>
<TableCell>Main Heading 2</TableCell>
</TableRow>
<TableRow>
<TableCell>Sub 11</TableCell>
<TableCell>Sub 12</TableCell>
<TableCell>Sub 13</TableCell>
<TableCell>Sub 14</TableCell>
<TableCell>Sub 21</TableCell>
<TableCell>Sub 22</TableCell>
<TableCell>Sub 23</TableCell>
<TableCell>Sub 24</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>Item 11</TableCell>
<TableCell>Item 12</TableCell>
<TableCell>Item 13</TableCell>
<TableCell>Item 14</TableCell>
<TableCell>Item 21</TableCell>
<TableCell>Item 22</TableCell>
<TableCell>Item 23</TableCell>
<TableCell>Item 24</TableCell>
</TableRow>
</TableBody>
</Table>
But that aligns Main heading 1 and Main Heading 2 above sub11 and sub12 and leaves 6 empty spaces after that.
I essentially want to have 2 "main" columns and a way to create whatever sizes of columns/rows I want in each of them.
EDIT: I found the answer, I have to use the colSpan property and give it a value of the amount of cells that I want to have underneath the main heading, so both headings get colSpan={4}
I found the answer, I have to use the colSpan property and give it a value of the amount of cells that I want to have underneath the main heading, so both headings get colSpan={4}

DokuWiki: Side-By-Side diff: Table with two cols in code block in table cell

I want to create a table in DokuWiki which has two rows.
On the left side is a code block (old code), and on the right side there is an other code block (new code).
How to format this in dokuwiki?
If it would be html, it would be easy:
<table>
<tr>
<td>
<pre>
...
</pre>
</td>
<td>
<pre>
...
</pre>
</td>
</tr>
</table>
Optional: Syntax highlighting for Python would be nice.
This should do the work:
| <code>Row 1 Col 1</code> | <code>Row 1 Col 2</code> |
Or for a simple monospaced text use '':
| ''%%Row 1 Col 1%%'' | ''%%Row 1 Col 2%%'' |
EDIT Add %% %% makes string to be not interpreted as DokuWiki syntax.
Here the documentation: https://www.dokuwiki.org/wiki:syntax
||border=1
||!Table heading 1||!Table heading 2||
||Cell 1.1||Cell 1.2||
||Cell 2.1||Cell 2.2||

ColdFusion processing a form more efficiently and a minor error

I have a form processing that I am sure can be done more efficiently and there is an error in the result set although not "life threatening" just not correct.
The purpose of the page is to associate an item with a program and at the same time associate a designation within the program -- B and F fields are checkboxes allowing the item to be associated with multiple programs and designations within that specific program. (edited for clarity)
Example:
Item: Lightsaber
Program: Jedi Training
Designation: Tool(b) (yes)
Designation: Weapon(f) (no)
Program: Jedi Master
Designation: Tool(b) (yes)
Designation: Weapon(f) (yes)
Program: Smuggler
Designation: Tool(b) (no)
Designation: Weapon(f) (no)
Form:
<form action="#CGI.SCRIPT_NAME#" method="post" name="program">
<label>Item</label>
<select id="item" name="item">
<!---//loop through and display items --->
<cfloop query="getitems">
<option value="#itemid#">#itemname#</option>
</cfloop>
</select>
<table>
<!---//loop through and display programs --->
<cfloop query="getprogram">
<tr>
<td>#programname#</td>
<td><input type="checkbox" id="B#programid#" name="B#programid#"></td>
<td><input type="checkbox" id="F#programid#" name="F#programid#"></td>
</tr>
</cfloop>
</table>
<input type="submit">
</form>
Action Page:
<!---// is there is a form being processed --->
<cfif #CGI.REQUEST_METHOD# is 'post'>
<!---// create program list from query --->
<cfset pl = ValueList(query.var, ','>
<!---// set addtl form var's --->
<cfset listassid = 'form.item'>
<!---// loop over program list --->
<cfloop list="#pl#" index="i">
<!---// loop over form fields --->
<cfloop list="form.fieldnames" index="field">
<cfif #field# EQ 'B'&#i#>
<!---// if field is B and var, set designation true --->
<cfset b = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset b = 'null'>
</cfif>
<cfif #field# EQ 'F'&#i#>
<!---// if field is F and var, set designation true --->
<cfset f = 1>
<cfelse>
<!---// it's not, set to null --->
<cfset f = 'null'>
</cfif>
<cfif b EQ 'null' AND f EQ 'null'>
<!---// if both are null then skip --->
//do nothing
<cfelse>
<!---//insert record into table --->
insert into table (table fields)
(#i#, #listassid#, #b#, #f# )
</cfif>
</cfloop>
</cfloop>
</cfif>
The result should be:
id item_id program_id B F
1 24 1 x
2 32 2 x x
The actual result is:
id item_id program_id B F
1 24 1 x
2 32 2 x
3 32 2 x
Thank you in advance for any clarification and efficiencies you can suggest.
I think a slightly different naming convention would help resolve the current issue, and improve the readability of the code.
One option is to store all of the project id's in a hidden form field. Be sure to use the same "name", so the id's are submitted as a CSV list. Also, I would recommend more meaningful names for the checkboxes, to improve readability. For example, "isSomething" rather than just "B" or "F":
<select id="item" name="itemID">
<cfoutput query="getitems">
<option value="#itemid#">#itemname#</option>
</cfoutput>
</select>
...
<cfoutput query="getPrograms">
<input type="hidden" name="programIDList" value="#programID#">
...
<!--- Set checkbox values to 1 / Yes --->
<td><input type="checkbox" name="isTool_#programID#" value="1"></td>
<td><input type="checkbox" name="isWeapon_#programID#" value="1"></td>
...
</cfoutput>
When the form is submitted, loop through the list of project id's and use the current id to extract the values of each set of checkboxes. If either box was checked, insert a record into the database.
* Note: Checkboxes are only submitted when "checked". Before accessing the field, either use structKeyExists to verify the field exists OR use cfparam to set a default value for the field.
<!--- Loop through list of available program id's --->
<cfloop list="#form.programIDList#" index="variables.programID">
<!--- Ensure fields exist. Set default value = 0 / No --->
<cfparam name="form.isTool_#variables.programID#" default="0">
<cfparam name="form.isWeapon_#variables.programID#" default="0">
<!--- Extract the current values --->
<cfset variables.isTool = FORM["isTool_"& variables.programID ] >
<cfset variables.isWeapon = FORM["isWeapon_"& variables.programID ] >
<!--- DEBUG ONLY: Display current values --->
<cfoutput>
<hr>variables.programID = #variables.programID#
<br>variables.isTool = #variables.isTool#
<br>variables.isWeapon = #variables.isWeapon#
<br>form.itemID = #form.itemID#
</cfoutput>
<!--- If either box was checked, save to database --->
<cfif variables.isTool || variables.isWeapon>
... run cfquery here
</cfif>
</cfloop>
Two important notes about database queries:
Never use raw client values in SQL. Instead use cfqueryparam. It helps protects your database against sql injection. It also improves query performance when a statement is executed multiple times (as in this case)
When executing multiple (related) queries, be sure to wrap them in a cftransaction to ensure consistency, ie All statements succeed or fail as a unit.

XML::twig to filter XML parent nodes in PERL

I have a xml snippet
<head>
<a>
<b attr_1=1>
<b attr_1=2>
<c attr_2 =3 attr_3 =5/>
<c attr_2 =4 attr_3 =6 />
</b>
</a>
<a>
<b attr_1=1/>
<b attr_1=3>
<c attr_2 =3 attr_3 =5/>
<c attr_2 =10 attr_3 =10/ >
</b>
</a>
</head>
Now only those node are legitimate which have <b attr_1 =3>(at least one) and at least one respective child <c> having attr_2=10 and attr_3 =10 is there.
Thus the ouput file should have following trade
<a>
<b attr_1=1/>
<b attr_1=3>(this is the legitimate value)
<c attr_2 =3 attr_3 =5/>
<c attr_2 =10 attr_3 =10/ >(this is the legitimate combination)
</b>
</a>
My Code is
use strict;
use warnings;
use XML::Twig;
my $twig = new XML::Twig( twig_handlers => { a=> \&a} );
$twig->parsefile('1511.xml');
$twig->set_pretty_print('indented');
$twig->print_to_file('out.xml');
sub a {
my ( $twig, $a ) = #_ ;
$a->cut
unless grep { $_->att( 'attr_1' ) eq '3' } $a->children( 'b' )
}
By this I am able to go till level . Please help if anybody can in explaining how to traverse and grep till node C which is inside node B.
You had some errors in your XML-file. Also you seem to have deleted some parts of your description. You can also set some attribute restrictions to handlers and the *child methods.
sub a {
my ( $twig, $a ) = #_ ;
my $cut = 1;
foreach my $b ($a->children('b[#attr_1="3"]')){
$cut &&= not grep {$_->att('attr_2') eq '10'
and $_->att('attr_3') eq '10'} $b->children('c');
}
$a->cut if $cut;
}
This is the file I used for testing:
<head>
<a>
<b attr_1="1" />
<b attr_1="2">
<c attr_2 ="3" attr_3 ="5"/>
<c attr_2 ="4" attr_3 ="6" />
</b>
</a>
<a>
<b attr_1="1"/>
<b attr_1="3">
<c attr_2 ="3" attr_3 ="5"/>
<c attr_2 ="10" attr_3 ="10" />
</b>
</a>
<a>
<b attr_1="1"/>
<b attr_1="3">
<c attr_2 ="3" attr_3 ="5"/>
<c attr_2 ="10" attr_3 ="12" />
</b>
</a>
</head>
The output:
<head>
<a>
<b attr_1="1"/>
<b attr_1="3">
<c attr_2="3" attr_3="5"/>
<c attr_2="10" attr_3="10"/>
</b>
</a>
</head>
Edit: If you really want to have only grep statements you could use some nested greps like this, though I'd advice you to use the above, more readable solution.
$a->cut unless
grep {grep {$_->att('attr_2') eq '10' and $_->att('attr_3') eq '10'}
$_->children('c')} $a->children('b[#attr_1="3"]');

Show table rownumber

I have a script which type is="text/x-tmpl"
This script generates the rows and columns into the tbody.
What I try to do is to create a column which have a input to show the number of row of each item.
example:
row 1: thumbnail - Filename - 1
row 2: thumbnail - Filenaem - 2
...
...
I want that the user can change his row number.
The problem is that always show "0".
Here is the code:
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td><input type="text" value="{%=[i]%}"></td> /**My input to show the number of row**/
</tr>
{% } %}