How do I write a for
loop in gremlin? This is what I tried:
for i=0;i<10:i++:println hello number i
I also tried to iterate over nodes and call a function on each one e.g.(count
):
gremlin> g.v(782).in==> v[2746934]
==> v[2581232]
==> v[1554286]
==> v[780]
gremlin> g.v(78).in.loop(2){it.loops < 4}.count()
==> class java.lang.NullPointerException : null
My main aim is to execute a for loop and print the value of nodes that have no inE
. Which means the in degree of them is zero.
If you want to iterate over all the nodes in the graph to get a count of how many didn't have in edges you could do something like:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.filter{!it.inE.hasNext()}.count()
==>2
If you want to perform some side-effect like doing a "print", then use the side-effect step:
gremlin> g.V.filter{!it.inE.hasNext()}.sideEffect{println it.name}
marko
==>v[1]
peter
==>v[6]
Note that it's printing twice because the Gremlin terminal is already printing for you, so if you're in the terminal you might as well just do:
gremlin> g.V.filter{!it.inE.hasNext()}.name
==>marko
==>peter
filter{!it.inE.hasNext()}
is more efficient as a full count/iteration is not required.