Thursday, April 08, 2010

Translate From Hash to Array in ruby

it is code for putting transaction name and its response time in pair , so that it can be used to show on the Google chart afterwords.

#Hash implementation:

@Detail_CHART = Hash.new
if @Detail_CHART.has_key?(transname)
new_name = transname + i.to_s
while @Detail_CHART.has_key?(new_name)
i += 1;
new_name = transname + i.to_s
end
@Detail_CHART.store(new_name, restime);
else @Detail_CHART.store(transname, restime);
end

However, i found ruby Hash keep its own order instead of original order of input, so i have to change hash to Array implementation.

#Array implementation:

@Detail_action = Array.new
@Detail_time = Array.new
if @Detail_action.include?(transname)
new_name = transname + i.to_s;
while @Detail_CHART.has_key?(new_name)
i += 1;
new_name = transname + i.to_s;
end
@Detail_action.push(new_name);
@Detail_time.push(restime);
else
@Detail_action.push(transname);
@Detail_time.push(restime);
end

No comments:

Post a Comment