0

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"}]
AsukaMinato
  • 9,758
  • 1
  • 14
  • 40
  • "Any thoughts on why this isn't working?" is it possible to post complete self contained example to reproduce this? what is your list? – Nasser Feb 28 '22 at 10:06
  • 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. –  Feb 28 '22 at 10:12
  • 1
    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. – Nasser Feb 28 '22 at 10:20
  • 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. – Nasser Feb 28 '22 at 10:27
  • Does First /@ alist get you what you need? where alist is your data without the TableForm. – Syed Feb 28 '22 at 10:28
  • @Syed I think OP also wants to remove string from something. Not sure from what though. That is why giving small input and expected output is always best for these types of questions. – Nasser Feb 28 '22 at 10:33
  • @Nasser I agree. The OP needs to show more effort and interest. – Syed Feb 28 '22 at 10:39

1 Answers1

2
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"}}
*)
Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67