0
$\begingroup$

I have an array called Array7 in which a typical element, let's say Array7[5][[22]], looks like this:

{"2021-03-01 00:46", 1.20822, 1.20828, 1.20822, 1.20826} .

So element Array7[5][[22]][[1]] is the string 2021-03-01 00:46. This checks out as a string by giving True in response to StringQ. The enclosing Array7[5][[22]], meanwhile, answers True to ArrayQ.

Now I want to change the month and day in Array7[5][[22]][[1]] to "10-03". I can get the required new element like this:

StringReplacePart[Array7[5][[22]][[1]], {"10-03"}, {6, 10}]] ,

which yields 2021-10-03 00:46 as it should.

But when I try to "save" this as the "new" Array7[5][[22]][[1]] by using

Array7[5][[22]][[1]] = StringReplacePart[Array7[5][[22]][[1]], {"10-03"}, {6, 10}]

I get the error message

Set::setps: Array7[5] in the part assignment is not a symbol.

$\endgroup$
6
  • $\begingroup$ What is Array7[5] ? Why the [5] and not [[5]] ? Is it a list or an Association, or something else? It certainly should not be turning Array7 into Array[7]. Also Array is a built in function so you should use lowercase to avoid confusion - although that doesn't explain the issue here. $\endgroup$
    – flinty
    Aug 28, 2021 at 17:39
  • $\begingroup$ Array7[5] is an array. Array7[5][[22]] is also an array. Array7[5][[22]][[1]] is a string. I made an error in the last line of the question. The error message is Array7[5] in the part assignment is not a symbol. $\endgroup$
    – user59886
    Aug 28, 2021 at 17:43
  • $\begingroup$ Yes I know that, but what I'm asking is how Array7 is defined - for all we know Array7[5] could be a function Array7 called with argument 5, in which case it's not a variable you can assign to. Please clarify what Array7 actually is. $\endgroup$
    – flinty
    Aug 28, 2021 at 17:45
  • $\begingroup$ I do not fully understand your question. Please can you tell me how I can find out what it actually is, and I will tell you. It is a bunch of data I imported from a .csv file. $\endgroup$
    – user59886
    Aug 28, 2021 at 17:47
  • 2
    $\begingroup$ You cannot assign to something like x[1][[22]] = ..., you can only assign to symbols like x1[[22]] = ... . I suggest you change how you're importing the data so that you do mydata = Import["mycsvfile.csv","CSV"] which will put all the data in a nested list mydata, instead of putting it in this cryptic Array7[5] variable $\endgroup$
    – flinty
    Aug 28, 2021 at 17:52

1 Answer 1

1
$\begingroup$

Using a smaller input data with the same structure as the one in OP:

SeedRandom[1];

dates = DateString[#, 
   {"Year", "-", "Month", "-", "Day", " " , "Hour", ":", "Minute"}] & /@ 
  DateRange[DateObject[{2021, 1, 1, RandomReal[{0, 12}], RandomReal[{0, 59}]}], 
     DateObject[{2021, 1, 31, 0, 0}], {1, "Day"}];
 
array7[n_] := Array[Flatten[{RandomChoice[dates], RandomInteger[n, 4]}] &, 10];

array7[5] // Column 

enter image description here

array7[5][[9]]

enter image description here

srp = StringReplacePart[ {"10-03"}, {6, 10}];

We can use ReplacePart to modify array7[5][[9]][[1]] using the function srp:

ReplacePart[array7[5], {9, 1} -> srp[array7[5][[9, 1]]]];

array7[5] // Column

enter image description here

array7[5][[9]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.