Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
{"headends"=>
[{"headend"=>
{"id"=>341766992,
"headend_name"=>"Comcast Burlingame Digital",
"dma_code"=>"807",
"dma_rank"=>6,
"system_name"=>"Comcast",
"headend_city"=>"Burlingame",
"headend_state"=>"CA",
"headend_time_zone"=>"PT",
"dma_name"=>"SAN FRANCISCO-OAK-SAN JOSE",
"channel_device"=>"X",
"country"=>"",
"service_type"=>"CA"},
"mso"=>{"id"=>341775346, "mso_name"=>"Comcast Cable Communications"},
"postal_code"=>"94010",
"device_id"=>"5b9a5042"}],
"services"=>
["amazon",
"directv",
"hbogo",
"hulu",
"itunes",
"itunes",
"netflixusa",
"showtime",
"vudu",
"youtube"],
"postal_code"=>nil,
"apps"=>
["cf528ea9",
"ea0f81d1",
"2ba2dc0e",
"50107ad3",
"3c103fa4",
"692bea67",
"557e96d5",
"b2db5e2a",
"0247ee5a",
"f0ad77dc",
"b24c00b1"]}
This is my hash, how can i extract values like "id"=>341766992, "postal_code"=>"94010"
For things that are hashes, e.g. {"foo"=>"bar", "baz"=>"blah"}, index into them with the key, e.g. myhash["foo"] # "baz".
For things that are arrays, e.g. ["hello", "world"], use their 0-based numeric indices, e.g. myarray[1] # "world".
Put those things together to dig through your structure, which I pretty-printed in an edit to your question:
data = {"headends"=>[{"headend"=>{"id"=>341766992, "headend_name"=>"Comcast Burlingame Digital", "dma_code"=>"807", "dma_rank"=>6, "system_name"=>"Comcast", "headend_city"=>"Burlingame", "headend_state"=>"CA", "headend_time_zone"=>"PT", "dma_name"=>"SAN FRANCISCO-OAK-SAN JOSE", "channel_device"=>"X", "country"=>"", "service_type"=>"CA"}, "mso"=>{"id"=>341775346, "mso_name"=>"Comcast Cable Communications"}, "postal_code"=>"94010", "device_id"=>"5b9a5042"}], "services"=>["amazon", "directv", "hbogo", "hulu", "itunes", "itunes", "netflixusa", "showtime", "vudu", "youtube"], "postal_code"=>nil, "apps"=>["cf528ea9", "ea0f81d1", "2ba2dc0e", "50107ad3", "3c103fa4", "692bea67", "557e96d5", "b2db5e2a", "0247ee5a", "f0ad77dc", "b24c00b1"]}
puts data["headends"][0]["headend"]["id"]
puts data["headends"][0]["postal_code"]
# Output:
# 341766992
# 94010
Prior to Ruby 2.3:
input['headends'].map do |e|
[
e['postal_code'],
*e['headend'].values_at(*%w|id|),
*e['mso'].values_at(*%w|id|),
]
end
2.3+
input['headends'].map do |e|
[%w|postal_code|, %w|headend id|, %w|mso id|].map do |key|
e.dig(*key)
end
end
Your question has been answered but I'm posting this to better show the format of the hash and also to point out that the example given could be drastically reduced in size and still make the same point.
h = { "headends"=>
[
{ "headend"=> {
"id" =>341766992,
"channel_device"=>"X",
"service_type" =>"CA"
},
"mso"=> {
"id" =>341775346,
"mso_name"=>"Comcast Cable Communications"
},
"postal_code"=>"94010",
"device_id" =>"5b9a5042"
}
]
}
h["headends"][0]["headend"]["id"] #=> 341766992
h["headends"][0]["postal_code"] #=> "94010"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi I want to create A YML file and load them as instance variables instead. How can I do that.
yml_File.yml File ::
default:
browserversion: 43
dev:
browser_02: iexplore
qa_01:
browser_default: chrome
qa_02:
browser_default: safari
Check the below Code::
p yml_File = YAML.load_file(File.dirname(__FILE__).gsub('/', '\\') + '\\Profiles.yml')
yml_File.each_key {|key_Value|
va = yml_File[key_Value].to_s
var_name = "##{key_Value}" # the '#' is required
self.instance_variable_set(var_name, va)
p "Name of Instance variable '#{key_Value}' is :: " + var_name.to_s + ' - And Key value is : ' + eval("##{key_Value}")
}
p #dev
p #qa_01
p #qa_02
Note - Ruby 1.9+ atleast
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Unable to Write Inline Aggregate function in Matlab.
X1, X2 are array variables. And mb and nb are size of BUS DATA.
V is the voltage function, delta is the angle.
% objf=inline('sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))','mb','nb');
% old code running
objf=inline('4*x1^2-2.1*x1^4+(x1^6)/3+x1*x2-4*x2^2+4*x2^4','x1','x2');**
*Error using inlineeval (line 15)
Error in inline expression ==> sum(V(mb).^2+V(nb).^2-2.*V(mb).*V(nb).cos(delta(mb)-delta(nb)))
Undefined function 'V' for input arguments of type 'double'.
Error in inline/subsref (line 24)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in deeee (line 48)
fx=objf(x(:,1),x(:,2));
where variable aer defined as below..
busdata = bus; % ARRAY OF INPUTs
j=sqrt(-1);
P=[];Q=[];
nb=busdata(:,1);
kb=busdata(:,2);Vm=busdata(:,3);deltad=busdata(:, 4);Pd=0.8*busdata(:,5)/basemva;Qd=.8*busdata(:,6)/basemva;
Pg=busdata(:,7)/basemva;Qg=busdata(:,8)/basemva;Bsh=busdata(:,11);Qmin=busdata(:,9)/basemva;Qmax=busdata(:,10)/basemva;
G=real(Ybus);B=imag(Ybus);slb=find(kb==1);pv=find(kb==2);pq=find(kb==0);pvq=find(kb~=1);npv=length(pv);
npq=length(pq);npvq=length(pvq);nbus=max(nb);
delta(nb) = pi/180*deltad(nb);
V(nb) = Vm(nb).*(cos(delta(nb))+j*sin(delta(nb)))';
P(nb)=(Pg(nb)-Pd(nb));
flag=0;
What you're locking for is anonymous functions
objf = #(mb,nb)sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))
objf =
#(mb,nb)sum(V(mb)^2+V(nb)^2-2*V(mb)*V(nb)*cos(delta(mb)-delta(nb)))
objf(1,2)
There you go (as far as all other variables and functions of this anonymous function are defined).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a method from a long script that creates a hash from genetic sequences, however it is really messy and thus I was wondering whether there was a way to put it more elegantly.
Here is a sample of the script (i.e. it contains an example)...
def make_hash(motif)
main_hash = Hash.new
id = ">isotig00009_f2_3 ~: S.P. Cleavage Site: 22:23 - S.P. D-value: 0.532"
seq = "MLKCFSIIMGLILLLEIGGGCA~IYFYRAQIQAQFQKSLTDVTITDYRENADFQDLIDALQSGLSCCGVNSYEDWDNNIYFNCSGPANNPEALWCAFLLLYTGSSKRSSQHPVRLWSSFPRTTKYFPHKDLHHWLCGYVYNVD"
id_hash = Hash[[[:id_start, :id_end], id.split("~").map(&:strip)].transpose]
seq_hash = Hash[[[:signalp, :seq_end], seq.split("~").map(&:strip)].transpose]
signalp = seq_hash[:signalp]
new_seq_end = seq_hash[:seq_end].gsub(/#{motif}/, '<span class="motif">\0</span>')
new_seq_hash = Hash[:signalp => signalp, :new_seq_end => new_seq_end ]
main_hash[id_hash] = [new_seq_hash]
return main_hash
end
motif = "VT|QAQ|F.D"
main_hash = make_hash(motif)
main_hash.each do |id_hash, seq_hash|
puts id_hash[:id_start]
puts id_hash[:id_end]
puts seq_hash[0][:signalp]
puts seq_hash[0][:new_seq_end]
end
So Is there a more elegant way to write the make_hash method...
Many Thanks
I haven't tested this, but I think this simplification will work:
def make_hash(motif)
id = ">isotig00009_f2_3 ~: S.P. Cleavage Site: 22:23 - S.P. D-value: 0.532"
seq = "MLKCFSIIMGLILLLEIGGGCA~IYFYRAQIQAQFQKSLTDVTITDYRENADFQDLIDALQSGLSCCGVNSYEDWDNNIYFNCSGPANNPEALWCAFLLLYTGSSKRSSQHPVRLWSSFPRTTKYFPHKDLHHWLCGYVYNVD"
id_hash = Hash[[[:id_start, :id_end], id.split("~").map(&:strip)].transpose]
f, s = seq.split("~").map(&:strip)
s.gsub!(/#{motif}/, '<span class="motif">\0</span>')
new_seq_hash = Hash[Hash[:signalp, f], Hash[:new_seq_end, s]]
Hash[id_hash, new_seq_hash]
end
If (as it appears) id and seq both have constant values, you might consider breaking them apart manually, rather than with id.split("~").map(&:strip); i.e.,
id1 = ">isotig00009_f2_3
id2 = ": S.P. Cleavage Site: 22:23 - S.P. D-value: 0.532"
seq1 = "MLKCFSIIMGLILLLEIGGGCA"
seq2 = "IYFYRAQIQAQFQKSLTDVTITDYRENADFQDLIDALQSGLSCCGVNSYEDWDNNIYFNCSGPANNPEALWCAFLLLYTGSSKRSSQHPVRLWSSFPRTTKYFPHKDLHHWLCGYVYNVD"
If there were a need to make seq2 more readable, we could use the "line continuation" character, \ (which even works within strings) like this:
seq2 = "IYFYRAQIQAQFQKSLTDVTITDYRENADFQDLIDALQSGLSCCGVNSYEDWDNNIYFNC"\
"SGPANNPEALWCAFLLLYTGSSKRSSQHPVRLWSSFPRTTKYFPHKDLHHWLCGYVYNVD"
or this:
seq2 = "IYFYRAQIQAQFQKSLTDVTITDYRENADFQDLIDALQSGLSCCGVNSYEDWDNNIYFNC\
SGPANNPEALWCAFLLLYTGSSKRSSQHPVRLWSSFPRTTKYFPHKDLHHWLCGYVYNVD"
If you preferred, you could make 'id' and 'seq' constants ('ID' and 'SEQ', say) and move them outside the method definition. Not surprisingly, line continuation also works for constant strings.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i want to load my image call caltrain, there is 30 img.
i used code
for i = 0:30
imgINumber = i;
imgPNumber = i+2;
if imgINumber < 10
imgIFile = sprintf('C:\sequence01_caltrain_gray\caltrain/gray/%s00%d.ras',imageName, imageName, imgINumber);
elseif imgINumber < 100
imgIFile = sprintf('C:\sequence01_caltrain_gray\caltrain/gray/%s0%d.ras',imageName, imageName, imgINumber);
end
if imgPNumber < 10
imgPFile = sprintf('C:\sequence01_caltrain_gray\caltrain\gray/%s00%d.ras',imageName, imageName, imgPNumber);
elseif imgPNumber < 100
imgPFile = sprintf('C:\sequence01_caltrain_gray\caltrain\gray/%s0%d.ras',imageName, imageName, imgPNumber);
end
imgI = double(imread(imgIFile));
imgP = double(imread(imgPFile));
imgI = imgI(:,1:352);
imgP = imgP(:,1:352);
but error:
Error using ==> imread
Can't open file "C:" for reading;
you may not have read permission.
i need solution for this
thanks
Either double your backslashes or replace all the backslashes with slashes in your sprintf calls.