Scala字段起始位置计算实现:初学者技术问询
Hey there! Since you're new to Scala, let's break down how to solve this problem of calculating each field's starting position step by step. The core idea is to accumulate the lengths of previous fields to get the starting position for the next one, starting with 0 for the first field.
Step 1: Understand the Input/Output Structure
First, let's clarify the data structure:
- Each input field has 5 attributes:
[identifier, fieldName, format, length, dataType] - The expected output inserts the starting position between the
formatandlengthattributes, making each output field have 6 attributes:[identifier, fieldName, format, startPosition, length, dataType]
For example:
Input field:
11 DFI1-MONT_TT_13 9(18) 14 IntegerType
Output field:11 DFI1-MONT_TT_13 9(18) 0 14 IntegerType
Step 2: Scala Implementation
Here's a beginner-friendly implementation with detailed comments:
object FieldPositionCalculator extends App { // Your input data as a single string val input = "11 DFI1-MONT_TT_13 9(18) 14 IntegerType 11 SERI1-SENS_13 X(01) 06 StringType 11 DDRI1-MONT_TT_14 9(18) 12 IntegerType 11 SQRI1-SENS_14 X(01) 14 StringType 11 XCRI1-MONT_TT_15 9(18) 10 IntegerType 11 QSRI1-SENS_15 X(01) 08 StringType 11 WQRI1-DEVISE X(03) 07 StringType" // Split the input string into individual elements (split by spaces) val elements = input.split(" ").toList // Group elements into chunks of 5 (each chunk represents one field) val fields = elements.grouped(5).toList // Use foldLeft to accumulate results and track the current starting position val (outputElements, _) = fields.foldLeft((List.empty[String], 0)) { case ((accElements, currentStart), field) => // Extract the length from the 4th element of the field (0-based index) val fieldLength = field(3).toInt // Build the output field: first 3 elements + start position + length + data type val outputField = field.take(3) :+ currentStart.toString :+ field(3) :+ field(4) // Update accumulated elements and next starting position (accElements ++ outputField, currentStart + fieldLength) } // Convert output elements back to a single string val finalOutput = outputElements.mkString(" ") // Print the result println(finalOutput) }
Step 3: Key Concepts Explained
split(" ").toList: Breaks the input string into a list of individual values, making it easy to process in chunks.grouped(5): Groups the list into sublists of 5 elements each, so each sublist maps directly to one field's attributes.foldLeft: A handy Scala tool for iterative accumulation. We use it to track two things at once:- The growing list of output elements (
accElements) - The current starting position for the next field (
currentStart)
- The growing list of output elements (
:+: Appends an element to the end of a list—perfect for inserting the calculated starting position into the field's attribute set.
When you run this code, it will generate the exact output format you need, with each field's starting position calculated correctly by adding up the lengths of all previous fields.
内容的提问来源于stack exchange,提问作者user9683185

