gets.chomp find a string in a list - ruby

Is it possible to find a string in a list for a gets.chomp for if, elsif?
def number_problem()
puts "Please enter a number 1-100 for a phrase or word back!"
number = gets.chomp
mult_of_three = ["3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", "36", "39", "42", "45", "48", "51", "54", "57", "60", "63", "66", "69", "72", "75", "78", "81", "84", "87", "90", "93", "96", "99"]
mult_of_five = ["5", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90", "95", "100"]
mult_of_both = ["15", "30", "45", "60", "75", "90"]
if number == mult_of_three
puts "Bit"

To find if an element is present in an Array, you can use include?. Here:
if mult_of_three.include?(number)
Example:
number = gets.chomp
36
# => "36"
mult_of_three.include?(number)
# => true
number = gets.chomp
98
# => "98"
mult_of_three.include?(number)
# => false

Related

Sorting arraylist of objects based on String property

I am new to groovy syntax.
I have data structure something like this.
[
{"name": "John", "gender": "Male", "id": "1"},
{"name": "Max", "gender": "Male", "id": "2"},
{"name": "John", "gender": "Male", "id": "3"},
{"name": "Sam", "gender": "Male", "id": "4"},
{"name": "Max", "gender": "Male", "id": "5"},
{"name": "Sam", "gender": "Male", "id": "6"},
{"name": "Sam", "gender": "Male", "id": "7"},
{"name": " ", "gender": "Male", "id": "8"},
{"name": "John", "gender": "Male", "id": "9"},
{"name": " ", "gender": "Male", "id": "10"},
]
I am trying to sort on it based on name property. I need all the records with name=max should come first followed by name=Sam and then name=John and the remaining records that have an empty name field or any other value.
Can someone suggest to me how I can achieve this?
Thanks.
Something like
def weights = [ "Max", "Sam", "John", '', ]
def sorted = [
["name": "John", "gender": "Male", "id": "1"],
["name": "Max", "gender": "Male", "id": "2"],
["name": "John", "gender": "Male", "id": "3"],
["name": "Sam", "gender": "Male", "id": "4"],
["name": "Max", "gender": "Male", "id": "5"],
["name": "Sam", "gender": "Male", "id": "6"],
["name": "Sam", "gender": "Male", "id": "7"],
["name": " ", "gender": "Male", "id": "8"],
["name": "John", "gender": "Male", "id": "9"],
["name": " ", "gender": "Male", "id": "10"],
].sort{ weights.indexOf it.name.trim() }
println sorted.join( '\n' )
prints
[name:Max, gender:Male, id:2]
[name:Max, gender:Male, id:5]
[name:Sam, gender:Male, id:4]
[name:Sam, gender:Male, id:6]
[name:Sam, gender:Male, id:7]
[name:John, gender:Male, id:1]
[name:John, gender:Male, id:3]
[name:John, gender:Male, id:9]
[name: , gender:Male, id:8]
[name: , gender:Male, id:10]

jqgrid treeGrid Cannot read property 'rowIndex' of undefined

I try to use local data with treeGrid, when i click the unfold
button . I get the tips "Cannot read property 'rowIndex' of
undefined"
<!DOCTYPE html>
<html lang="en">
<head>
<!-- The jQuery library is a prerequisite for all jqSuite products -->
<script type="text/ecmascript" src="./js/jquery.min.js"></script>
<!-- This is the Javascript file of jqGrid -->
<script type="text/ecmascript" src="./js/jquery.jqGrid.min.js"></script>
<!-- This is the localization file of the grid controlling messages, labels, etc.
<!-- We support more than 40 localizations -->
<script type="text/ecmascript" src="./js/i18n/grid.locale-en.js"></script>
<!-- A link to a jQuery UI ThemeRoller theme, more than 22 built-in and many more custom -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/jquery-ui-1.10.4.custom.min.css" />
<!-- The link to the CSS that the grid needs -->
<link rel="stylesheet" type="text/css" media="screen" href="./css/ui.jqgrid.css" />
<meta charset="utf-8" />
<title>jqTreeGrid - Load On Demand - Load all Rows at once collapsed</title>
</head>
<body>
<table id="tree"></table>
<div id="pager"></div>
<script type="text/javascript">
var rows = [
{
"category_id": "1",
"name": "ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "1",
"rgt": "44",
"level": "0",
"uiicon": ""
},
{
"category_id": "2",
"name": "TELEVISIONS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "2",
"rgt": "19",
"level": "1",
"uiicon": ""
},
{
"category_id": "3",
"name": "TUBE",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "3",
"rgt": "8",
"level": "2",
"uiicon": ""
},
{
"category_id": "11",
"name": "26 \" TV",
"price": "200.00",
"qty_onhand": "1",
"color": "black",
"lft": "4",
"rgt": "5",
"level": "3",
"uiicon": "ui-icon-image"
},
{
"category_id": "12",
"name": "30 \" TV",
"price": "350.00",
"qty_onhand": "2",
"color": "black",
"lft": "6",
"rgt": "7",
"level": "3",
"uiicon": "ui-icon-document"
},
{
"category_id": "4",
"name": "LCD",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "9",
"rgt": "12",
"level": "2",
"uiicon": ""
},
{
"category_id": "13",
"name": "Super-LCD 42\" ",
"price": "400.00",
"qty_onhand": "10",
"color": "all",
"lft": "10",
"rgt": "11",
"level": "3",
"uiicon": "ui-icon-video"
},
{
"category_id": "5",
"name": "PLASMA",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "13",
"rgt": "18",
"level": "2",
"uiicon": ""
},
{
"category_id": "14",
"name": "Ultra-Plasma 62\" ",
"price": "440.00",
"qty_onhand": "2",
"color": "silver",
"lft": "14",
"rgt": "15",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "15",
"name": "Value Plasma 38\" ",
"price": "312.00",
"qty_onhand": "0",
"color": "silver",
"lft": "16",
"rgt": "17",
"level": "3",
"uiicon": "ui-icon-clipboard"
},
{
"category_id": "6",
"name": "PORTABLE ELECTRONICS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "20",
"rgt": "43",
"level": "1",
"uiicon": ""
},
{
"category_id": "7",
"name": "MP3 PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "21",
"rgt": "32",
"level": "2",
"uiicon": ""
},
{
"category_id": "8",
"name": "FLASH",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "22",
"rgt": "29",
"level": "3",
"uiicon": ""
},
{
"category_id": "17",
"name": "Super-Shuffle 1gb",
"price": "20.00",
"qty_onhand": "11",
"color": "all",
"lft": "23",
"rgt": "24",
"level": "4",
"uiicon": "ui-icon-note"
},
{
"category_id": "21",
"name": "5Gb Flash",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "25",
"rgt": "26",
"level": "4",
"uiicon": "ui-icon-comment"
},
{
"category_id": "22",
"name": "10Gb flash ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "27",
"rgt": "28",
"level": "4",
"uiicon": "ui-icon-tag"
},
{
"category_id": "16",
"name": " Power-MP3 128mb",
"price": "123.00",
"qty_onhand": "2",
"color": "withe",
"lft": "30",
"rgt": "31",
"level": "3",
"uiicon": "ui-icon-signal-diag"
},
{
"category_id": "9",
"name": "CD PLAYERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "33",
"rgt": "38",
"level": "2",
"uiicon": ""
},
{
"category_id": "18",
"name": " Porta CD ",
"price": "10.00",
"qty_onhand": "0",
"color": "",
"lft": "34",
"rgt": "35",
"level": "3",
"uiicon": "ui-icon-eject"
},
{
"category_id": "19",
"name": "CD To go!",
"price": "110.00",
"qty_onhand": "11",
"color": "",
"lft": "36",
"rgt": "37",
"level": "3",
"uiicon": "ui-icon-power"
},
{
"category_id": "10",
"name": "2 WAY RADIOS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "39",
"rgt": "42",
"level": "2",
"uiicon": ""
},
{
"category_id": "20",
"name": "Family Talk 360 ",
"price": "200.00",
"qty_onhand": "15",
"color": "",
"lft": "40",
"rgt": "41",
"level": "3",
"uiicon": "ui-icon-volume-on"
},
{
"category_id": "23",
"name": "COMPUTERS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "45",
"rgt": "50",
"level": "0",
"uiicon": ""
},
{
"category_id": "25",
"name": "DESKTOP ",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "46",
"rgt": "47",
"level": "1",
"uiicon": ""
},
{
"category_id": "26",
"name": "LAPTOPS",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "48",
"rgt": "49",
"level": "1",
"uiicon": ""
},
{
"category_id": "24",
"name": "APPLIANCES",
"price": "0.00",
"qty_onhand": "0",
"color": "",
"lft": "51",
"rgt": "52",
"level": "0",
"uiicon": ""
}
]
jQuery(document).ready(function ($) {
jQuery('#tree').jqGrid({
// "url":"data.json",
"colModel": [
{
"name": "category_id",
"index": "accounts.account_id",
"sorttype": "int",
"key": true,
"hidden": true,
"width": 50
}, {
"name": "name",
"index": "name",
"sorttype": "string",
"label": "Name",
"width": 170
}, {
"name": "price",
"index": "price",
"sorttype": "numeric",
"label": "Price",
"width": 90,
"align": "right"
}, {
"name": "qty_onhand",
"index": "qty_onhand",
"sorttype": "int",
"label": "Qty",
"width": 90,
"align": "right"
}, {
"name": "color",
"index": "color",
"sorttype": "string",
"label": "Color",
"width": 100
}, {
"name": "lft",
"hidden": true
}, {
"name": "rgt",
"hidden": true
}, {
"name": "level",
"hidden": true
}, {
"name": "uiicon",
"hidden": true
}
],
"width": "780",
"hoverrows": false,
"viewrecords": false,
"gridview": true,
"height": "auto",
"sortname": "lft",
"loadonce": true,
"rowNum": 100,
"scrollrows": true,
// enable tree grid
"treeGrid": true,
// which column is expandable
"ExpandColumn": "name",
// datatype
"treedatatype": "json",
// the model used
"treeGridModel": "nested",
// configuration of the data comming from server
"treeReader": {
"left_field": "lft",
"right_field": "rgt",
"level_field": "level",
"leaf_field": "isLeaf",
"expanded_field": "expanded",
"loaded": "loaded",
"icon_field": "icon"
},
// "sortorder": "asc",
"datatype": "local",
"data": rows,
"pager": "#pager"
});
});
</script>
</body>
</html>
For localdata type, you must configure the localReader parameter:
localReader: {
repeatitems: true,
// cell: "",
id: "category_id"
}

In d3 4.0, how can I get extra data from the stratified object?

I'm a new to D3 also stackoverflow, and have some question.
[
{"id": "1", "name": "Eve", "parent": "" , "txt": "text1."},
{"id": "2", "name": "Cain", "parent": "1", "txt": "text2."},
{"id": "3", "name": "Seth", "parent": "1", "txt": "text3."},
{"id": "4", "name": "Enos", "parent": "3", "txt": "text4."},
{"id": "5", "name": "Noam", "parent": "3", "txt": "text5."},
{"id": "6", "name": "Abel", "parent": "1", "txt": "text6."},
{"id": "7", "name": "Awan", "parent": "1", "txt": "text7."},
{"id": "8", "name": "Enoch", "parent": "7", "txt": "text8."},
{"id": "9", "name": "Azura", "parent": "1", "txt": "text9."}
];
Above is array object which is not stratified yet.
below is the stratified result and code.
var stratify = d3.stratify()
.id(function(d){ return d.id; })
.parentId(function(d){ return d.parent; });
stratified object
I want to use the data object, like below code
node.append("text")
.text(function(d) { return d.name; });
but, above doesn't work.
refered to https://github.com/d3/d3-hierarchy/blob/master/README.md#stratify
and I edited the array object a little.
thanks & best regards. and It would be greatly appreciated if you could give me a feedback.
Finally, I found the answer.
return d.data.name
what an embarrassingly amateurish thing...

Get particular key's and value's from JSON array - Ruby [duplicate]

This question already has answers here:
Ruby: Easiest Way to Filter Hash Keys?
(14 answers)
Closed 6 years ago.
I am a beginner with Ruby, and I have the following Json array:
"elements": [
{
"type": "Contact",
"id": "1",
"createdAt": "131231235",
"name": "test",
"updatedAt": "1456328049",
"accountName": "Mr Test",
"country": "China",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"type": "Contact",
"id": "2",
"createdAt": "156453447",
"name": "test2",
"updatedAt": "124464554",
"accountName": "Mr Test2",
"country": "Germany",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I want to filter out only a few keys + values: for example I want to return only the id,name,accountName,firstname and lastname.
So the exspected output is the following:
"elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I tried the following: create a filter array which has the elements I want to return and then map over the items but then I get stuck..
filters = []
filters.push("accountName")
filters.push("lastName")
filters.push("firstName")
filters.push("Id")
output["elements"].each do |item|
result = []
item.map {|key,value|filters.include? key}
result.push(?)
Thank you for the help.
Check this out, you should be able to work out from this:
output = { "elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
"somethoong": "sdsad"
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2"
}
]}
attribs = %w(accountName lastName firstName id)
output[:elements].each do |item|
item.delete_if{|k,v| !attribs.include?(k.to_s)}
end

Tree structure with drag and drop rows in jqgrid?

I'm using jqgrid for tree structure in a table.I'm providing drag and drop row functionality.I want to call a callback method on drag event.Because I want to adjust the hierarchy of the rows which is a column having number in it ex:1.1,1.2.1,...Can I do this.
The following is the code I wrote inside my html:
jQuery(function(){
jQuery("#tree").tableDnD({scrollAmount:0});
jQuery("#tree").jqGrid({
url:'jsonSamplePots.json',
datatype: "json",
colNames: ["Task Id", "Task No.", "Task Name", "Priority", "Start Date", "End Date", "Task Type", "Link", "Link RelationShip", "Resources(Units)", "Reference"],
colModel: [
{name:'id', index:'id', width: 30, hidden: true, key: true},
{name:'no', width:80, sortable:false,editable:true},
{name:'name', width:150, sortable:false,editable:true},
{name:'priority', width:80, sortable:false,editable:true},
{name:'sDate', width:80, sortable:false},
{name:'eDate', width:80, sortable:false},
{name:'type', width:120, sortable:false},
{name:'link', width:80, sortable:false},
{name:'rship', width:80, sortable:false},
{name:'resources', width:300, sortable:false},
{name:'ref', width:80, sortable:false},
],
rowNum:10,
rowList:[10,20,30],
pager: '#pcelltbl',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"Tasks",
cellEdit:true,
treeGrid: true,
ExpandColumn:'name',
gridComplete: function() {
//alert("gridcomplete");
$("#_empty","#tree").addClass("nodrag nodrop");
jQuery("#tree").tableDnDUpdate();
},
I'm using the following json data:
{
"total": "1",
"page": "1",
"records": "2",
"rows": [
{"id": "1", "cell": ["1", "Super <b>Item</b>", "300", "0", "", "false", "true", "true"]},
{"id": "2", "cell": ["2", "<a href='http://www.google.com'>Google</a>", "100", "1", "1", "false", "false", "true"]},
{"id": "3", "cell": ["3", "Sub Item 1", "50", "2", "2", "true", "true", "true"]},
{"id": "4", "cell": ["4", "Sub Item 2", "25", "2", "2", "false", "false", "true"]},
{"id": "5", "cell": ["5", "Sub-sub Item 1", "25", "3", "4", "true", "true", "true"]},
{"id": "6", "cell": ["6", "Sub Item 3", "25", "2", "2", "true", "true", "true"]},
{"id": "7", "cell": ["7", "<span style='background-color:LightGreen; color:Tomato'>Item 2</span>", "200", "1", "1", "false", "true", "true"]},
{"id": "8", "cell": ["8", "Sub Item 1", "100", "2", "7", "false", "false", "true"]},
{"id": "9", "cell": ["9", "Sub-sub Item 1", "50", "3", "8", "true", "true", "true"]},
{"id": "10", "cell": ["10", "Sub-sub Item 2", "50", "3", "8", "true", "true", "true"]},
{"id": "11", "cell": ["11", "Sub Item 2", "100", "2", "7", "true", "true", "true"]}
]
}
Thanks in advance

Resources