Skip to main content
added 130 characters in body
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

Multiple inserts can be made using FoldFold or ReplaceAll, e.g.

list = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Fold[Insert[#1, #2[[1]]First[#2], #2[[2]]]Last[#2]] &, list, {{"a", 2}, {"b", 4}, {"c", 6}}]

{1, a, 2, b, 3, c, 4, 5, 6, 7, 8, 9}

list /. {x_Integer, y_, z_, rest__} :> {x, "a", y, "b", z, "c", rest}

{1, "NovNov 9 2017 13:24"24, "Thursday"Thursday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Resetting allEntries for the multiple insertFold method.

{1, "NovNov 9 2017 13:24"24, "Thursday"Thursday, "Friday"Friday, "Saturday"Saturday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Much easier to use Easier usingReplaceAll.

{1, "NovNov 9 2017 13:24"24, "Thursday"Thursday, "Friday"Friday, "Saturday"Saturday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Multiple inserts can be made using Fold, e.g.

list = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Fold[Insert[#1, #2[[1]], #2[[2]]] &, list, {{"a", 2}, {"b", 4}, {"c", 6}}]

{1, "Nov 9 2017 13:24", "Thursday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Resetting allEntries for the multiple insert method.

{1, "Nov 9 2017 13:24", "Thursday", "Friday", "Saturday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Much easier to use ReplaceAll.

{1, "Nov 9 2017 13:24", "Thursday", "Friday", "Saturday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Multiple inserts can be made using Fold or ReplaceAll, e.g.

list = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Fold[Insert[#1, First[#2], Last[#2]] &, list, {{"a", 2}, {"b", 4}, {"c", 6}}]

{1, a, 2, b, 3, c, 4, 5, 6, 7, 8, 9}

list /. {x_Integer, y_, z_, rest__} :> {x, "a", y, "b", z, "c", rest}

{1, Nov 9 2017 13:24, Thursday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Resetting allEntries for the Fold method.

{1, Nov 9 2017 13:24, Thursday, Friday, Saturday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Easier usingReplaceAll.

{1, Nov 9 2017 13:24, Thursday, Friday, Saturday, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

added 491 characters in body
Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

Much easier to use ReplaceAll.

allEntries = save;

allEntries = allEntries /. {a_Integer, date_, rest__} :> {a, date,
    DateString[{date,
      {"Month", "Day", "Year", "Hour", "Minute"}}, "DayName"],
    DateString[DatePlus[date, {1, "Day"}], "DayName"],
    DateString[DatePlus[date, {2, "Day"}], "DayName"], rest}

allEntries[[1, 1]]

{1, "Nov 9 2017 13:24", "Thursday", "Friday", "Saturday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Much easier to use ReplaceAll.

allEntries = save;

allEntries = allEntries /. {a_Integer, date_, rest__} :> {a, date,
    DateString[{date,
      {"Month", "Day", "Year", "Hour", "Minute"}}, "DayName"],
    DateString[DatePlus[date, {1, "Day"}], "DayName"],
    DateString[DatePlus[date, {2, "Day"}], "DayName"], rest}

allEntries[[1, 1]]

{1, "Nov 9 2017 13:24", "Thursday", "Friday", "Saturday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Source Link
Chris Degnen
  • 31.3k
  • 2
  • 56
  • 109

Multiple inserts can be made using Fold, e.g.

list = {1, 2, 3, 4, 5, 6, 7, 8, 9};

Fold[Insert[#1, #2[[1]], #2[[2]]] &, list, {{"a", 2}, {"b", 4}, {"c", 6}}]

{1, a, 2, b, 3, c, 4, 5, 6, 7, 8, 9}

Applied to the OP's data structure.

shapes = Range[12];
r = ReplacePart[Range[15], 2 -> "Nov 9 2017 13:24"];

save = allEntries = {
    {r, r, r},
    {r, r},
    {r, r, r, r},
    {r},
    {r, r, r, r},
    {r, r, r},
    {r},
    {r, r, r, r},
    {r, r},
    {r, r, r},
    {r},
    {r, r, r, r}
    };

Demonstrating the OP's single insert function.

allEntries = Table[Insert[allEntries[[j, i]],
    DateString[{allEntries[[j, i, 2]],
      {"Month", "Day", "Year", "Hour", "Minute"}}, "DayName"], 3],
   {j, 1, Length[shapes]}, {i, 1, Length[allEntries[[j]]]}];

allEntries[[1, 1]]

{1, "Nov 9 2017 13:24", "Thursday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

Resetting allEntries for the multiple insert method.

allEntries = save;

allEntries = Table[
   Fold[Insert[#1, First[#2], Last[#2]] &, 
    allEntries[[j, i]], {{DateString[{allEntries[[j, i, 2]],
        {"Month", "Day", "Year", "Hour", "Minute"}}, "DayName"], 3},
     {DateString[DatePlus[
        allEntries[[j, i, 2]], {1, "Day"}], "DayName"], 4},
     {DateString[DatePlus[
        allEntries[[j, i, 2]], {2, "Day"}], "DayName"], 5}}],
   {j, 1, Length[shapes]}, {i, 1, Length[allEntries[[j]]]}];

allEntries[[1, 1]]

{1, "Nov 9 2017 13:24", "Thursday", "Friday", "Saturday", 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}