0
$\begingroup$

I'm currently trying to code a For Loop that includes StringDrop:

For[i=0, i<=100,i++, Print[StringDrop[list[[i,1]],4]]]

Where list is an array and the first column (which I'm trying to extract), is called node0, node1,...

I'm getting Part::partd: Part specification <<1>> is longer than depth of object." and "StringDrop::strse: String or list of strings expected at position 1 in StringDrop[{{node0,node1 0.04,node8 11.11,node14 72.21},{node1,node46 1247.25,node6 20.59,node13 64.94},{node2,node66 54.18,node31 166.80,node45 1561.45},{node3,node20 133.65,node6 2.06,node11 42.43},<<3>>,{node7,node50 478.14,node9 3.15,node10 5.85},{node8,node69 577.91,node11 7.45,node12 3.18},{node9,node70 2454.28,node13 4.42,node20 16.53},<<90>>}[[0,1]],4].

Any thoughts on why this isn't working? Thanks

EDIT:

    For[
 i = 0, i <= 100, i++,
 node = StringDrop[list[[i, 1]], 4] ;(*This is the node in question*)

Here is the list (first few entries):

TableForm[{{"node0", "node1 0.04", "node8 11.11", "node14 72.21"}, {
  "node1", "node46 1247.25", "node6 20.59", "node13 64.94"}, {
  "node2", "node66 54.18", "node31 166.80", "node45 1561.45"}, {
  "node3", "node20 133.65", "node6 2.06", "node11 42.43"}, {
  "node4", "node75 3706.67", "node5 0.73", "node7 1.02"}, {
  "node5", "node45 1382.97", "node7 3.33", "node11 34.54"}, {
  "node6", "node31 63.17", "node9 0.72", "node10 13.10"}]
$\endgroup$
7
  • $\begingroup$ "Any thoughts on why this isn't working?" is it possible to post complete self contained example to reproduce this? what is your list? $\endgroup$
    – Nasser
    Feb 28, 2022 at 10:06
  • $\begingroup$ Added list - hope that helps. In hindsight, I think it might be easier if I just drop the first column of the array and add a new one with just numbers. $\endgroup$
    – user85312
    Feb 28, 2022 at 10:12
  • 1
    $\begingroup$ try changing i = 0 to i=1 as Mathematica index starts from 1. And try to do it without TableForm as that is a wrapper. And change i <= 100 to i <= Length@list. Also there are better ways to do this without For using more functional programming. $\endgroup$
    – Nasser
    Feb 28, 2022 at 10:20
  • $\begingroup$ btw, it is much to explain what is it you are actually trying to do. You just post code and error. It is better to post a small example of and input, and what is the output needed. That is all. $\endgroup$
    – Nasser
    Feb 28, 2022 at 10:27
  • $\begingroup$ Does First /@ alist get you what you need? where alist is your data without the TableForm. $\endgroup$
    – Syed
    Feb 28, 2022 at 10:28

1 Answer 1

2
$\begingroup$
data = {{"node0", "node1 0.04", "node8 11.11", 
    "node14 72.21"}, {"node1", "node46 1247.25", "node6 20.59", 
    "node13 64.94"}, {"node2", "node66 54.18", "node31 166.80", 
    "node45 1561.45"}, {"node3", "node20 133.65", "node6 2.06", 
    "node11 42.43"}, {"node4", "node75 3706.67", "node5 0.73", 
    "node7 1.02"}, {"node5", "node45 1382.97", "node7 3.33", 
    "node11 34.54"}, {"node6", "node31 63.17", "node9 0.72", 
    "node10 13.10"}};

StringDrop[#, 4] & /@ data

(*
{{"0", "1 0.04", "8 11.11", "14 72.21"}, {"1", "46 1247.25", 
  "6 20.59", "13 64.94"}, {"2", "66 54.18", "31 166.80", 
  "45 1561.45"}, {"3", "20 133.65", "6 2.06", "11 42.43"}, {"4", 
  "75 3706.67", "5 0.73", "7 1.02"}, {"5", "45 1382.97", "7 3.33", 
  "11 34.54"}, {"6", "31 63.17", "9 0.72", "10 13.10"}}
*)
$\endgroup$

Your Answer

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