@f.each do |linea|
cod, desc = linea.chomp.split(/,/) @array1[i] = [ cod.strip.to_i ]
i = i+1
end @asort1 = @array1.sort
An equivalent piece of code that does the same: @array1 = @f.map do |linea|
[ linea.to_i ]
end @asort1 = @array1.sort
String#to_i ignores whitespace at the beginning (so you don't need strip) and
will stop at the first non digit (so you don't have to remove what comes
after the comma - it will just be ignored).
Though I wonder what the purpose of the @array1 variable is and whether it is
intended that @array1 as well as @asort1 only contain one-element arrays.
In case @array1 has no purpose (other than holding the values to be sorted
until they are sorted) and the one-element arrays are not wanted, this
solution would work: @asort1 = @f.map {|line| line.to_i}.sort
@f.each do |linea|
cod, desc = linea.chomp.split(/,/) @array1[i] = [ cod.strip.to_i ]
i = i+1
end @asort1 = @array1.sort
An equivalent piece of code that does the same: @array1 = @f.map do |linea|
[ linea.to_i ]
end @asort1 = @array1.sort
String#to_i ignores whitespace at the beginning (so you don't need
strip) and
will stop at the first non digit (so you don't have to remove what comes
after the comma - it will just be ignored).
Ok
Though I wonder what the purpose of the @array1 variable is and whether
it is
intended that @array1 as well as @asort1 only contain one-element
arrays.
In case @array1 has no purpose (other than holding the values to be
sorted
until they are sorted) and the one-element arrays are not wanted, this
solution would work: @asort1 = @f.map {|line| line.to_i}.sort
My objective is to sort @array2 by numeric code and make a binary
search.
Sort it's ok, and for binary search I use gems --> "bsearch".
"bsearch" with simple array it's ok, but array into array dont't work.
This is because I make @array1 with one element.
Although surely there is some other better form.
there is no "bsearch" gem, at least not in the default repos.
actually, bsearch seems somewhat outdated (website last modified
2001-12-23).
···
On 18 Mai, 23:56, Rg Rg <r.bas...@gmail.com> wrote:
Sebastian Hungerecker wrote:
> Rg Rg wrote:
>> @f.each do |linea|
>> cod, desc = linea.chomp.split(/,/)
>> @array1[i] = [ cod.strip.to_i ]
>> i = i+1
>> end
>> @asort1 = @array1.sort
> An equivalent piece of code that does the same:
> @array1 = @f.map do |linea|
> [ linea.to_i ]
> end
> @asort1 = @array1.sort
> String#to_i ignores whitespace at the beginning (so you don't need
> strip) and
> will stop at the first non digit (so you don't have to remove what comes
> after the comma - it will just be ignored).
Ok
> Though I wonder what the purpose of the @array1 variable is and whether
> it is
> intended that @array1 as well as @asort1 only contain one-element
> arrays.
> In case @array1 has no purpose (other than holding the values to be
> sorted
> until they are sorted) and the one-element arrays are not wanted, this
> solution would work:
> @asort1 = @f.map {|line| line.to_i}.sort
My objective is to sort @array2 by numeric code and make a binary
search.
Sort it's ok, and for binary search I use gems --> "bsearch".
"bsearch" with simple array it's ok, but array into array dont't work.
This is because I make @array1 with one element.
Although surely there is some other better form.