lib/rbbt/tsv/accessor.rb in rbbt-util-5.21.115 vs lib/rbbt/tsv/accessor.rb in rbbt-util-5.21.116
- old
+ new
@@ -690,14 +690,18 @@
field_pos = self.identify_field field
new.with_unnamed do
if merge
self.through do |key,values|
field_values = values[field_pos]
- values.delete_at(field_pos) if delete
+ if delete
+ values = values.dup
+ values.delete_at(field_pos)
+ end
next if field_values.nil?
zipped = Misc.zip_fields(values)
field_values.zip(zipped).each do |field_value,rest|
+ rest = [nil] * values.length if rest.nil?
k = [key,field_value]*sep
if new.include? k
new[k] = Misc.zip_fields(Misc.zip_fields(new[k]) << rest)
else
new[k] = rest.nil? ? nil : rest.collect{|v| [v]}
@@ -710,10 +714,11 @@
field_values = values[field_pos]
values.delete_at(field_pos) if delete
next if field_values.nil?
zipped = Misc.zip_fields(values)
field_values.zip(zipped).each do |field_value,rest|
+ rest = [nil] * values.length if rest.nil?
k = [key,field_value]*sep
new[k] = rest
end
end
new.type = :list
@@ -728,23 +733,26 @@
end
new
end
- def zip(field = 0, merge = false, sep = ":")
+ def zip(merge = false, field = "New Field", sep = ":")
new = {}
self.annotate new
new.type = :double if merge
new.with_unnamed do
if merge
self.through do |key,values|
new_key, new_value = key.split(sep)
- new_values = values + [[new_value]]
+ new_values = values + [[new_value] * values.first.length]
if new.include? new_key
- new[new_key] = Misc.zip_fields(Misc.zip_fields(new[new_key]) << new_values)
+ current = new[new_key]
+ current.each_with_index do |v,i|
+ v.concat(new_values[i])
+ end
else
new[new_key] = new_values
end
end
else
@@ -759,9 +767,17 @@
if self.key_field and self.fields
new.key_field = self.key_field.partition(sep).first
new.fields = new.fields + [field]
end
+ new
+ end
+
+ def remove_duplicates(pivot = 0)
+ new = self.annotate({})
+ self.through do |k,values|
+ new[k] = Misc.zip_fields(Misc.zip_fields(values).uniq)
+ end
new
end
end