I need to analyze and compare a string with a sub string

0

I want to select a sub-string obtained from a text file and analyze it.

Text file:

NADSU 78000mc0cl0Css
NADBY 7808810008659
PAT 1 21D 089

I need to read and later analyze line by line of a .txt file after that I would like to verify by means of an if or case cycle that the paramentros could be fulfilled. I clarify the txt file has about 300 lines

The first three characters must be letters with a specific format, the next is usually the 4 character or 5 that has a number depends on that number means something and the last is the purchase order number or the amount or type of wrap.

This is a generalized idea I am only giving the minimum to understand.

Example of two real cases of the line to analyze:

NADBY 7808810008659

The letters NAD is the Purchase Order BY is the Supplier and the remainder is the Purchase Order Number .

PAT 1 21D 089

PAT means Type of payment , the first number is the Type of payment where: if% is 1 Type of payment is counted , if 2 is Credit . The middle 21d is the amount of days to issue clearance and the 089 is the number of days to pay.

NADSU 78000mc0cl0Css

The letters NAD are for Purchase Order and BY is Buyer ; Css equals boxes but can be ss , which is equivalent to mini-jacks .

<% fileopen ('nombre_doc.txt',r) do |fichero1| %>
  <% fileopen ('copianombre_doc.txt',w) do |fichero2| %>
    <% while linea= fichero1.gets %>
      <% fichero1=gsub(/\s+/,'') %>
    
asked by Jonathan Zambrano 14.11.2017 в 18:37
source

2 answers

0
  

... would be something like this NADBY = region metropolina, supplier code,   quantity of boxes purchased 59.

If the format is fixed, then you could refer to the positions of each character, for example:

string = "NADBY 7808810008659"
#=> "NADBY 7808810008659"

if string.split(" ").first.size == 5
  clave     = string[0..4]   #=> "NADBY"
  region    = string[6]      #=> "7"
  proveedor = string[7...-2] #=> "8088100086"
  cajas     = string[-2..-1] #=> "59"

  resultado = "#{clave} = región #{region}, #{proveedor}, cantidad de cajas compradas #{cajas}."
  #=> "NADBY = región 7, 8088100086, cantidad de cajas compradas 59."
else
  # código para procesar cadenas que no inician con 5 letras
end

Using [] on your string you can get the character that is in that specific position through its index (starts in 0 ) or, using ranges (eg 0..4 ) you can get a part of the string .

Original reply

You could also use the String#start_with? method to find the strings that start with "ABC-2" . Of course you would have to convert your current string in an array to compare each one; for example:

string  = "ACB-1234567890123 BCD-1234567890123 ABC-2134567890123"
#=> "ACB-1234567890123 BCD-1234567890123 ABC-2134567890123"

string.split(" ").each_with_object([]) do |s, numbers|
  if s.start_with?("ABC-2")
    # aquí tu código para hacer
    # algo específico...

    numbers << s.split("-").last
  end
end
#=> ["2134567890123"]

What this code does is:

  • .split(" ") separates all strings and converts them to an array (uses the variable s to identify each one).
  • each_with_object([]) iterates the array of strings and generates a new array (represented in the variable numbers ).
  • if s.start_with?("ABC-2") evaluates if the string in turn complies with the conditions.
  • numbers << s.split("-").last divides the current string into letters and numbers and adds the numbers to the numbers array (when the if is met).

The result is an array with 1 element, which represents the number of the only string that met the condition ( ") that the first three letters are ABC and the first number is 2 ").

    
answered by 14.11.2017 / 19:26
source
0

You could use a regular expression to extract the value from the string:

  param = 'ACB-1234567890123'
  regex = /ACB-(\d+)/
  if param =~ regex
    param.match(regex)[1] # => "1234567890123"
  end
    
answered by 14.11.2017 в 18:48