cycle foreach and next if a condition is met help in ruby on rails

0

Cycle foreach and next if a condition is met, I am analyzing a line in a txt and I want to verify if the next line complies with a pattern to do one thing and if not to do something else.

Example:

next if line.include?("UNSS")
if next line.start('UNSS') or next line.start('CNT')

Although I do not get an error, it does not give me the expected result, I know it's something basic but I found an error at the last minute in development and interpreted that way.

My intention is after verifying that the present line starts with a pattern and verifying if the next one has a pattern to know if it reached the end or not and then save.

Suppose I have the following structure: enter the code here

And this one:

LIN00000108410036009090EN 
IMDF  CU ESPUM FREIXENET C.NEGRO 12G BOT 750
QTY 21000000000000002CS 
QTY129000000000000006SIN
MOA203000000000055514.00
PRIAAA000000042968.00LIUCS 
ALCA  DE1
PCD  10000024.00
MOA204000000000010313.00
ALCA  DE1
PCD  10000015.00
MOA204000000000004899.00
ALCC  CA1
MOA 23000000000001080.00
aqui la proxima linea puede ser un LIN o UNSS

Suppose I'm on the line PRIAAA000000042968.00LIUCS (Once I'm here I want to know if my next line starts with ALCA or if it starts with UNSS ), because if it starts with UNSS it's a different format and I need to save at that moment, but if it's not let it go on and save in MOA 23 .

  a = Array.new
a = File.read(item)
oc = a.split("\n") 
#File.foreach(item).with_object([]) do |line, result|

 oc.each_cons(2) do |line, line2|
       puts "procensando linea #{line} ..."        



      if line.start_with?('LIN') # indica el numero item y codigo
                @cproducto = line[9..-2]
                @numeroitem = line[7..8]
                @numeroitem2 = @numeroitem.to_s
      end          
      line.start_with?('IMD') #indica la descripcion
                @desproducto = line[8..-1]
      if line.start_with?('QTY 21')  # indica la cantidad pedida y el tipo de unidad de caja                      
                @cantidadpedida = line[19..20]              
                @caja   = line[-3..-1]
                pedido = '?????????????????????'
      end          
      if line.start_with?('QTY129') # cantidad de unidades que trae una caja
                @cantidadunidades = line[-6..-4]                               
                @tunidad = 'Unidades Simples'
                #Detalle.last.destroy 
      end                                 
      if line.start_with?('MOA203') #precio neto del producto               
                @precioneto = line[7..-1] # not [-10..-1
                @precioneto2 = @precioneto.to_f.round(3).to_f 
      end                       
      if line.start_with?('MOA 204')
                 if !flagmoa
                @valordescu1 = line[-10..-1]
                flagmoa = true                
                else                                            #monto del descuento pueden haber hasta dos descuentos en una oc          
                @valordescu2 = line[-10..-1]
                end
      end

      if line.start_with?('PRIAAA') #precio neto de lista de precio por unidad
                pedido = line[-3..-1]
                @valorcd = line[-16..-8]  
               if  line.start_with?("ALCA")
                    # código para líneas que empiezan con "ALCA"
               elsif line2.start_with?("UNSS")
                @p.detalles.create!(cod_prod: @cproducto, descripcion: @desproducto, numero_item: @numeroitem2, cantidad_pedida: @cantidadpedida , precio_unit: @precioneto2 , tipo_caja: @cantidadunidades ,  total_linea: @precioneto2, p_descu1: @porcentaje , monto_descu1: @valordescu1, p_descu2: @porcentaje2, monto_descu2:@valordescu2 , p_cargo1:"" , monto_cargo1: @valorcargo)           
               end

      end      

      if line.start_with?('PCD')   
            if !flagpcd
              @porcentaje   = line[-6..-1] 
              flagpcd = true
            else
            @porcentaje2   = line[-6..-1]
            end
      end      
      if line.start_with?('MOA 204')
                 if !flagmoa
                @valordescu11 = line[-10..-1]
                @valordescu1 = @valordescu1.to_s
                flagmoa = true                
                else                                            #monto del descuento pueden haber hasta dos descuentos en una oc          
                @valordescu22 = line[-10..-1]
                @valordescu2 = @valordescu22.to_s
                end      
      end         # 

      if line.start_with?('MOA 23') # or line.start_with?('UNSS') #valor en monto del cargo
               @tcargo = 'Cargo'
               @valorcargo = line[-10..-1] 
           @p.detalles.create!(cod_prod: @cproducto, descripcion: @desproducto, numero_item: @numeroitem2, cantidad_pedida: @cantidadpedida , precio_unit: @precioneto2 , tipo_caja: @cantidadunidades ,  total_linea: @precioneto2, p_descu1: @porcentaje , monto_descu1: @valordescu1, p_descu2: @porcentaje2, monto_descu2:@valordescu2 , p_cargo1:"" , monto_cargo1: @valorcargo)         
           flagpcd = false
           flagmoa = false  
      end          

    
asked by Jonathan Zambrano 14.12.2017 в 15:39
source

1 answer

0

Assuming the lines are in the variable lines (as a collection), you could use Enumerable#each_cons ; for example:

lines.each_cons(2) do |current_line, next_line|
  if current_line.start_with?("PRIAA")
    if next_line.start_with?("ALCA")
      # código para líneas que empiezan con "ALCA"
    elsif next_line.start_with?("UNSS")
      # código para líneas que empiezan con "UNSS"
    end
  end
end
    
answered by 14.12.2017 / 16:42
source