1
$\begingroup$

(Minimal example)

I'm dealing with the following array :

array = {
         {""},
         {"Analysis 1"},
         {"1 class TimeDependentAnalysis timeIntegrationScheme 1 solver 1 \*"},
         {"**"},
         {""}
        };

I export this array in a .txt file as the following :

Export["test2.txt", array, "Table"]

wich give me this nice result

enter image description here

now I have to insert some elements between the line "1 class TimeDependentAnalysis timeIntegrationScheme 1 solver 1 *" and "**", these elements are defined as

elements = Table[{StringForm["Text ``", i]}, {i, 1, 3}]

At the end I should get the following result :

enter image description here

NB : The file .txt will be used by an another program such that it can't be written differently

NB : The list "elements" can contain an infinite number of elements

Any ideas ?

$\endgroup$

1 Answer 1

2
$\begingroup$

I finally found a way to do it

addRow[a_, b_, n_] := Join[a[[;; n - 1]], b, a[[n ;;]]]
array = {
         {""}, 
         {"Analysis 1"}, 
         {"1 class TimeDependentAnalysis timeIntegrationScheme 1 solver 1 \*"}, 
         {"**"}, 
         {""}
        };
elements = Table[ToString@StringForm["Text ``", i], {i, 1, 3}];
output = addRow[array, elements, 4];
Export["test2.txt", output, "Table"]

However if there are another ways, I'm interesting !

$\endgroup$
1
  • $\begingroup$ Your original array is a list of lists. However, you insert "Texti" not as lists but as strings. Is this intentional or not? $\endgroup$ Commented Jun 11, 2021 at 8:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.