Fetch odd position value from array - ruby-on-rails

I am trying to get only odd position in array but unable to do so.I am in short span of time .Can anyone tell me what can i do
data.each_with_index{|i,index| p i if index.odd?}
[ "shivam" "1" "2" "1", "", "2" "1", "", "", "", "", "", ""]
[ "aman", "2", "5", "3", "3", "3", "2", "", "", "", "", "", ""]
["praneet", "1", "1", "1", "8", "1", "6", "", "", "", "", "", ""]
When I am trying to access bizs[0] it is giving me
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
dddddddtitle
and when I am trying to access biz[1] it is giving me this
dddddddaniket
dddddddshivam
dddddddtiwari
dddddddaman
dddddddverma
dddddddpraneet
dddddddsodhi
The output I am getting after this is
ddddddd["title", "aniket", "month1", "200", "month2", "200", "month3", "200", "month4", "200", "month5", "200", "month6", "200", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "shivam", "month1", "1", "month2", "2", "month3", "1", "month4", "", "month5", "2", "month6", "1", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "tiwari", "month1", "1", "month2", "4", "month3", "2", "month4", "2", "month5", "2", "month6", "1", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "aman", "month1", "2", "month2", "5", "month3", "3", "month4", "3", "month5", "3", "month6", "2", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "verma", "month1", "3", "month2", "6", "month3", "4", "month4", "5", "month5", "3", "month6", "3", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "praneet", "month1", "1", "month2", "1", "month3", "1", "month4", "8", "month5", "1", "month6", "6", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
ddddddd["title", "sodhi", "month1", "1", "month2", "", "month3", "1", "month4", "", "month5", "4", "month6", "", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
Now All I have to do is just ignore odd position values and store in a new array
Final output should be like this
[ "aniket", "200", "200", "200", "200", "200", "200", "", "", "", "", "", ""]
[ "shivam" "1" "2" "1", "", "2" "1", "", "", "", "", "", ""]
[ "tiwari", "1", "4", "2", "2", "2", "1", "", "", "", "", "", ""]
[ "aman", "2", "5", "3", "3", "3", "2", "", "", "", "", "", ""]
[ "verma", "3", "6", "4", "5", "3", "3", "", "", "", "", "", ""]
["praneet", "1", "1", "1", "8", "1", "6", "", "", "", "", "", ""]
["sodhi", "1", "", "1", "", "4", "", "", "", "", "", "", ""]
I am unable to do so Can anyone help me out with this???

This one works
bizs.select.with_index { |_, index| index.odd? }
irb output
bizs = ["title", "aniket", "month1", "200", "month2", "200", "month3", "200", "month4", "200", "month5", "200", "month6", "200", "month7", "", "month8", "", "month9", "", "month10", "", "month11", "", "month12", ""]
bizs.select.with_index { |_, index| index.odd? }
#=> ["aniket", "200", "200", "200", "200", "200", "200", "", "", "", "", "", ""]

result = []
array.each_with_index{|i,index| result << i if index.odd?}

Please find my proposed code as shown below,
even_items = []
odd_items = []
data.each_with_index do |bizs, index|
if index % 2 == 1
odd_items << bizs[index]
else
even_items << bizs[index]
end
end
This will result in two arrays one inserting all the elements with an odd index in odd_items array and the inserting all the even elements in the even_items array.

Related

Post Data Into this type of format?

I am not getting how to POST this type of data into webservice
{
"customer_id":"",
"customer_message": " entered by user",
"discount_amount": "",
"ip_address":"1.0.10.22",
"billing_address": {
"first_name": "hello",
"last_name": "world",
"company": "",
"street_1": "45 W test",
"street_2": "",
"city": "London",
"state": "Texas",
"zip": "123456",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyz#example.com"
},
"shipping_addresses": [
{
"first_name": "rest",
"last_name": "Mctest",
"company": "Test Address",
"street_1": "rest test",
"street_2": "",
"city": "test",
"state": "test",
"zip": "12345",
"country": "United States",
"country_iso2": "US",
"phone": "",
"email": "xyzer#example.com"
}
],
"products": [
{
"product_id": 5448,
"quantity": 2
}
]
I am not getting how to POST this type of data into webservice.
Please help me
i am doing this type of data now i have to post this type. Can anyone post this type.
let aParam = ["email": Email, "password":Password] as [String:Any]
You are taking parameters as [String: Any], where Any denotes each type of data types. Say String, Array and Dictionary.
For example,
let aParam: [String: Any] = ["customer_id": "",
"customer_message": "",
"billing_address": ["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"],
"shipping_addresses": [
["first_name" : "hello",
"last_name" : "world",
"company" : "",
"street_1" : "45 W test"]
],
"products": [
["product_id" : 5448,
"quantity" : 2],
["product_id" : 5450,
"quantity" : 1]
]
]
And complex json structure would be handle with content-type as JSON, so:
You just need to update your header as:
let aHeader = ["Content-Type" : "application/json"]
Hope this will help

How to convert string array into array and remove empty elements?

I have a string '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'. i want to convert it into array and remove empty values from that array. my desired output is abc;def;mno.
Can someone help me to do this?
You could use JSON.parse and select method:
str = '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'
arr = JSON.parse(str).select(&:present?)
Output array: ["abc", "def", "mno"]
If you want to get abc;def;mno:
joined = arr.join(';')
Output string: "abc;def;mno"
Hope this helps
Use this code:
str = YAML.load('["", "abc", "", "def", "", "mno", "", "", "", "", ""]')
str.select{|a| a if a != ""}.join(";")
You can parse your string with JSON#parse and use delete with join:
str = '["", "abc", "", "def", "", "mno", "", "", "", "", ""]'
JSON.parse(str).tap { |arr| arr.delete('') }.join(';')
# => "abc;def;mno"

Accessing Data Members after using expand in Odata

I currently have an odata service with 2 entity sets. I am using expand to navigate to second entity set, and binding values from both in a table.
However, I am unable to bind data from second entity set as {to_PurchaseOrderItem/PurchaseOrderQuantityUnit}
Am I doing this wrong?
The JSON reply looks something like this, so I am really confused what I am doing wrong.
"d": {
"results": [
{
"PurchaseOrder": "4500000001",
"Language": "EN",
"PaymentTerms": "0004",
"CashDiscount1Days": "0",
"CashDiscount2Days": "0",
"NetPaymentDays": "0",
"CashDiscount1Percent": "0.000",
"CashDiscount2Percent": "0.000",
"PurchasingOrganization": "1710",
"PurchasingDocumentOrigin": "9",
"PurchasingGroup": "001",
"CompanyCode": "1710",
"PurchaseOrderDate": "/Date(1468454400000)/",
"DocumentCurrency": "USD",
"ExchangeRate": "1.00000",
"ValidityStartDate": null,
"ValidityEndDate": null,
"SupplierQuotationExternalID": "",
"SupplierRespSalesPersonName": "",
"SupplierPhoneNumber": "",
"SupplyingSupplier": "",
"SupplyingPlant": "",
"PurchaseOrderType": "NB",
"IncotermsClassification": "",
"InvoicingParty": "17300001",
"ReleaseIsNotCompleted": false,
"PurchasingCompletenessStatus": false,
"IncotermsVersion": "",
"IncotermsLocation1": "",
"IncotermsLocation2": "",
"ManualSupplierAddressID": "",
"AddressCityName": "",
"AddressFaxNumber": "",
"PurchasingDocumentDeletionCode": "",
"AddressHouseNumber": "",
"AddressName": "",
"AddressPostalCode": "",
"AddressStreetName": "",
"AddressPhoneNumber": "",
"AddressRegion": "",
"AddressCountry": "",
"AddressCorrespondenceLanguage": "",
"PurchasingProcessingStatus": "02",
"CreatedByUser": "CB9980000025",
"CreationDate": "/Date(1468454400000)/",
"Supplier": "17300001",
"PurchaseOrderSubtype": "",
"to_PurchaseOrderItem": {
"results": [
{
"PurchaseOrder": "4500000001",
"OrderQuantity": "100",
"PurchaseOrderQuantityUnit": "ES",
"OrderPriceUnit": "ES",
"OrderPriceUnitToOrderUnitNmrtr": "1",
"OrdPriceUnitToOrderUnitDnmntr": "1",
"NetPriceAmount": "0.21",
"DocumentCurrency": "USD",
"NetPriceQuantity": "1",
"TaxCode": "",
"PriceIsToBePrinted": true,
"PurchaseOrderItem": "1",
"OverdelivTolrtdLmtRatioInPct": "10.0",
"UnlimitedOverdeliveryIsAllowed": false,
"UnderdelivTolrtdLmtRatioInPct": "10.0",
"ValuationType": "",
"IsCompletelyDelivered": false,
"IsFinallyInvoiced": false,
"PurchaseOrderItemCategory": "0",
"AccountAssignmentCategory": "",
"MultipleAcctAssgmtDistribution": "",
"PartialInvoiceDistribution": "",
"PurchasingDocumentDeletionCode": "L",
"GoodsReceiptIsExpected": true,
"GoodsReceiptIsNonValuated": false,
"InvoiceIsExpected": true,
"InvoiceIsGoodsReceiptBased": false,
"PurchaseContract": "",
"PurchaseContractItem": "0",
"Customer": "",
"ItemNetWeight": "0.000",
"ItemWeightUnit": "",
"TaxJurisdiction": "0508525201",
"PurchaseOrderItemText": "RAW15,PD",
"PricingDateControl": "",
"ItemVolume": "0.000",
"ItemVolumeUnit": "",
"SupplierConfirmationControlKey": "",
"IncotermsClassification": "EXW",
"IncotermsTransferLocation": "VENDOR",
"EvaldRcptSettlmtIsAllowed": false,
"PurchaseRequisition": "",
"PurchaseRequisitionItem": "0",
"IsReturnsItem": false,
"Plant": "1710",
"RequisitionerName": "",
"ServicePackage": "0",
"EarmarkedFunds": "",
"EarmarkedFundsItem": "0",
"IncotermsLocation1": "VENDOR",
"IncotermsLocation2": "",
"Material": "RM15",
"ManufacturerMaterial": "RM15",
"ServicePerformer": "",
"ProductType": "1",
"StorageLocation": "",
"DeliveryAddressID": "",
"DeliveryAddressName": "",
"DeliveryAddressStreetName": "",
"DeliveryAddressHouseNumber": "",
"DeliveryAddressCityName": "",
"DeliveryAddressPostalCode": "",
"DeliveryAddressRegion": "",
"DeliveryAddressCountry": "",
"MaterialGroup": "L002",
"PurchasingInfoRecord": "5300000201",
"SupplierMaterialNumber": ""
I am trying to access Order Quantity to be specific.
Thanks!
I think you are missing "results" while binding the control. Please see below and try
{to_PurchaseOrderItem/results/0/PurchaseOrderQuantityUnit}

How to send this type of parameter to server call in swift 3.0

I need to send this below parameters to a server in swift 3.0. How can I build the request parameters?
"step_1": [
{
"name": "",
"mobile": "",
"line_1": "",
"line_2": "",
"city": "",
"state": "",
"pincode": "",
"fax": "",
"gst": "",
"owner_name": "",
"dob": "",
"anniversary": ""
}
]
For upper json data below is Dictionary code:
let param = [["name": "abc",
"mobile": "123",
"line_1": "line_1",
"line_2": "line_2",
"city":"xyz",
"state":"abc",
"pincode":"000",
"fax":"123",
"gst":"gst",
"ownwer_name":"ownwer_name",
"dob":"xx-xx-xxxx",
"anniversary":"xx-xx-xxxx"]]
//Print data
print(param[0]["name"] as! String)
Hi I'm not sure if it will work, but anyway, try this
let params: ["step_1": [["name": "Some name",
"mobile": "12345678",
"line_1": "line_1 stuff",
"...": "..."]]]

Webmock JSON response for geocoding

I try to stub a geocoding HTTP request with webmock.
But I always get this error:
NoMethodError: undefined method `[]' for nil:NilClass
context "geocoding" do
before :each do
#user = Fabricate :user
stub_request(:get, /.*yboss.yahooapis.com*/).to_return(:body => File.read(File.join("spec", "fixtures", "geocoder", "yahoo_maps_data.json")))
end
it 'should geocode if coordinates missing' do
without_coordinates = Location.new(name: "Test",
street: "Revierstrasse 1",
zipcode: "5020",
country: "AT",
user: #user, affiliate: #user)
result = without_coordinates.geocode
without_coordinates.coordinates.should == [47.8029, 13.0472]
end
end
JSON FILE
/spec/fixtures/geocoder/yahoo_maps_data.json
{"quality": "59",
"latitude": "47.8029",
"longitude": "13.0472",
"offsetlat": "47.802898",
"offsetlon": "13.04185",
"radius": "8600",
"boundingbox": {
"north": "47.854401",
"south": "47.7514",
"east": "13.1075",
"west": "12.9869"
}, "name": "",
"line1": "",
"line2": "5020 Salzburg",
"line3": "",
"line4": "Austria",
"cross": "",
"house": "", "street": "",
"xstreet": "",
"unittype": "",
"unit": "",
"postal": "5020",
"neighborhood": "",
"city": "Salzburg",
"county": "Salzburg",
"state": "Salzburg",
"country": "Austria",
"countrycode": "AT",
"statecode": "5",
"countycode": "5",
"timezone": "Europe/Vienna",
"uzip": "5020",
"hash": "",
"woeid": "12816173",
"woetype": "11"}
What's the correct way to implement it successfully?
Thank you!
I found the error. My JSON File was not complete:
{"bossresponse": {
"responsecode": "200",
"placefinder": {
"start": "0",
"count": "1",
"request": "flags=JXTSR&gflags=AC&locale=en_US&location=Salzburg",
"results": [
{
"quality": "40",
"latitude": "47.80067",
"longitude": "13.04338",
"offsetlat": "47.80067",
"offsetlon": "13.04338",
"radius": "8600",
"boundingbox": {
"north": "47.85383",
"south": "47.75096",
"east": "13.12707",
"west": "12.98608"
},
"name": "",
"line1": "",
"line2": "Salzburg",
"line3": "",
"line4": "Austria",
"cross": "",
"house": "",
"street": "",
"xstreet": "",
"unittype": "",
"unit": "",
"postal": "",
"neighborhood": "",
"city": "Salzburg",
"county": "Salzburg",
"state": "Salzburg",
"country": "Austria",
"countrycode": "AT",
"statecode": "5",
"countycode": "5",
"timezone": "Europe\/Vienna",
"uzip": "5020",
"hash": "",
"woeid": "547826",
"woetype": "7"
}
]
}
}}

Resources