Filter NSMutableDiction with Key - ios

I need to filter this NSMutableDictionary on key value based where item_type= bad
i tried but not working
NSPredicate *pred=[NSPredicate predicateWithFormat:#"(item_type == %#)", #"Good"];
NSArray *resultArray = [[dummyDictionary allValues] filteredArrayUsingPredicate:pred];
NSLog(#"resultArray:%#",resultArray);
{
“Cat1” = (
{
"image_item" = “a”;
"item_type" = “Good”;
}
);
Cat2 = (
{
"image_item" = “b”;
"item_type" = Good;
},
{
"image_item" = “c”;
"item_type" = Bad;
},
);
Cat3 = (
{
"image_item" = “d”;
"item_type" = Good;
},
{
"image_item" = “e”;
"item_type" = Bad;
},
);
}
And need such output NSMutableDictionary
{
“Cat1” = (
);
Cat2 = (
{
"image_item" = “c”;
"item_type" = Bad;
},
);
Cat3 = (
{
"image_item" = “e”;
"item_type" = Bad;
},
);
}
NOTE: dummyDictionary is value i get from .plist

Related

How to Sort a Multi-dimensional Array by Value in Lua

Unfortunately, sorting tables in lua still remains difficult for me. Who can help me with the following question:
I would like to sort the table below by pluNumber:
Original tabel:
[3] = {
[1] = {
['article'] = {
['pluNumber'] = '204',
['name'] = 'Cola Zero'
}
},
[2] = {
['article'] = {
['pluNumber'] = '202',
['name'] = 'Sinas'
}
},
[3] = {
['article'] = {
['pluNumber'] = '203',
['name'] = '7-up'
}
},
[4] = {
['article'] = {
['pluNumber'] = '201',
['name'] = 'Cola'
}
}
}
New table:
[3] = {
[1] = {
['article'] = {
['pluNumber'] = '201',
['name'] = 'Cola'
}
},
[2] = {
['article'] = {
['pluNumber'] = '202',
['name'] = 'Sinas'
}
},
[3] = {
['article'] = {
['pluNumber'] = '203',
['name'] = '7-up'
}
},
[4] = {
['article'] = {
['pluNumber'] = '204',
['name'] = 'Cola Zero'
}
}
}
Where do I begin? I didn't succeed with a normal table.sort
Please help
You need to provide a function that tells Lua which of two table elements comes first.
table.sort(yourTable, function (a, b)
return a.article.pluNumber < b.article.pluNumber end
Or
table.sort(yourTable, function (a, b)
return a.article.pluNumber > b.article.pluNumber end
for decending order. This is explained in the Lua reference manual.
https://www.lua.org/manual/5.4/manual.html#pdf-table.sort

Robot allowed the website but being identified and rejected

I need to do web scraping for a website which has allowed robot access. Below is the robot.txt file's content.
User-agent: *
Disallow:
Sitemap:https://www.sample.com/sitemap-index.xml
But when I try to fetch the website's content using nokogiri, it's being detected.
Nokogiri::HTML(open('https://www.sample.com/search?q=test', :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))
Here the output:
> (Document:0x3fda40e7cf70 {
name = "document",
children = [
#(DTD:0x3fda40e9591c { name = "html" }),
#(Element:0x3fda40e8c95c {
name = "html",
attributes = [ #(Attr:0x3fda4071a598 { name = "style", value = "height:100%" })],
children = [
#(Element:0x3fda3fefa28c {
name = "head",
children = [
#(Element:0x3fda401a3088 {
name = "meta",
attributes = [ #(Attr:0x3fda40ebd7a0 { name = "name", value = "ROBOTS" }), #(Attr:0x3fda40ebd778 { name = "content", value = "NOINDEX, NOFOLLOW" })]
}),
#(Element:0x3fda4074faf4 {
name = "meta",
attributes = [ #(Attr:0x3fda3ff0beec { name = "name", value = "format-detection" }), #(Attr:0x3fda3ff0bed8 { name = "content", value = "telephone=no" })]
}),
#(Element:0x3fda401ca700 {
name = "meta",
attributes = [ #(Attr:0x3fda401c2050 { name = "name", value = "viewport" }), #(Attr:0x3fda401c217c { name = "content", value = "initial-scale=1.0" })]
}),
#(Element:0x3fda4079a284 {
name = "meta",
attributes = [ #(Attr:0x3fda4078bfb8 { name = "http-equiv", value = "X-UA-Compatible" }), #(Attr:0x3fda4078bf04 { name = "content", value = "IE=edge,chrome=1" })]
})]
}),
#(Element:0x3fda407e2e6c {
name = "body",
attributes = [ #(Attr:0x3fda430205f0 { name = "style", value = "margin:0px;height:100%" })],
children = [
#(Element:0x3fda4072e2a0 {
name = "iframe",
attributes = [
#(Attr:0x3fda3ff45214 {
name = "src",
value = "/_Incapsula_Resource?SWUDNSAI=28&xinfo=5-66719320-0%200NNN%20RT%281543054979096%20247%29%20q%280%20-1%20-1%20-1%29%20r%280%20-1%29%20B12%284%2c315%2c0%29%20U2&incident_id=245000650118470008-256430953704260629&edet=12&cinfo=04000000"
}),
#(Attr:0x3fda3ff451d8 { name = "frameborder", value = "0" }),
#(Attr:0x3fda3ff451b0 { name = "width", value = "100%" }),
#(Attr:0x3fda3ff45188 { name = "height", value = "100%" }),
#(Attr:0x3fda3ff45174 { name = "marginheight", value = "0px" }),
#(Attr:0x3fda3ff4514c { name = "marginwidth", value = "0px" })],
children = [ #(Text "Request unsuccessful. Incapsula incident ID: 245000650118470008-256430953704260629")]
})]
})]
})]
})
How can I achieve this web scraping?

How to add table to table?

I want to add table 'mm' to table 'fmenu.pages' but it doesnt work at all. Error: attempt to index a nil value (field 'main2'). Its about last line. Code:
local fmenu = {
selected_button = 0,
menu = {
font = 1,
},
pages = {
["main"] = {
name = "name",
id = 1,
btns = {
{name = "name I", id = 1}
}
}
}
}
local mm = {
["main2"] = {
name = "name2",
id = 2,
btns = {
{name = "name I", id = 1},
{name = "name II", id = 2}
}
}
}
table.insert(fmenu.pages, mm)
print(fmenu.pages["main2"].name)
How about using table.merge from lua-stdlib?
local table = require"std.table"
local fmenu = {
selected_button = 0,
menu = {
font = 1,
},
pages = {
["main"] = {
name = "name",
id = 1,
btns = {
{name = "name I", id = 1}
}
}
}
}
local mm = {
["main2"] = {
name = "name2",
id = 2,
btns = {
{name = "name I", id = 1},
{name = "name II", id = 2}
}
}
}
table.merge(fmenu.pages, mm)
print(fmenu.pages["main2"].name)

I want the value for key open_now in array

Opening :- (
"Not Available",
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
{
"open_now" = 1;
"weekday_text" = (
);
},
"Not Available",
{
"open_now" = 1;
"weekday_text" = (
);
},
"Not Available"
)
This is an output of my NSArray. It contains some strings as Not Available and it contains some NSDictionary starting with { "open_now" = 1; "weekday_text" = ( ); } .I wanted the value of only open_now. I don't know how to get value of open_now in my new NSArray. Please anyone help me.
Yes, use the NSArray -valueForKey: method.
NSArray *extracted = [sourceArray valueForKey:#"a key"];
You can use valueForKeyPath method of NSArray to get value of particular key.
NSArray *aryValue = [YOUR_ARRAY valueForKeyPath:#"open_now"];

JSON that contains an array to NSString

How do I extract each address from the NSDictionary I instantiated (below), so I can assign to various NSString objects?
I have the following JSON:
{
"Address":[
{
"$":{
"ID":"0"
},
"Address2":[
"10 Smith RD"
],
"City":[
"Mapleville"
],
"State":[
"NJ"
],
"Zip5":[
"90210"
],
"Zip4":[
"764"
]
},
{
"$":{
"ID":"1"
},
"Address2":[
"32 Hog CT"
],
"City":[
"New York City"
],
"State":[
"NY"
],
"Zip5":[
"90210"
],
"Zip4":[
"1390"
]
}
]
}
cData (below) came from the JSON above.
I did the following to convert to a NSDictionary:
NSDictionary* dictionary2 = [NSJSONSerialization JSONObjectWithData:cData options:NSJSONReadingAllowFragments error:nil];
once I did a:
NSLog(#"%#", dictionary2);
output from NSLog:
{
Address = (
{
"$" = {
ID = 0;
};
Address2 = (
"10 Smith RD"
);
City = (
"Mapleville"
);
State = (
NJ
);
Zip4 = (
7642
);
Zip5 = (
90210
);
},
{
"$" = {
ID = 1;
};
Address2 = (
"32 Hog CT"
);
City = (
"New York City"
);
State = (
NY
);
Zip4 = (
1390
);
Zip5 = (
90210
);
}
);
}
Just follow the structure.
NSDictionary* dictionary2 = [NSJSONSerialization ...
NSArray *addresses = dictionary2[#"Address"];
for (NSDictionary *addressData in addresses) {
NSString *address2 = addressData[#"Address2"];
NSString *city = addressData[#"City"];
// and the rest as needed
}

Resources