Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem using join 3

Status
Not open for further replies.

ailse

Programmer
Jan 20, 2004
79
GB
OK here is my current scenario -

I have an input data file (test.txt) with pairs of nodes (tab-delimited) -

Code:
G:1	G:2
G:4	G:5
G:7	G:4
G:9	G:10
G:12	G:7

I have a script to read this file and produce for each pair of nodes a statement to add the pair as an edge to a graph (thanks KevinADC!):

Code:
open (EDGELIST, "test.txt") || die ("Could not open file!");

@edgelistarray=<EDGELIST>;

foreach my $edge (@edgelistarray)
   {
  my($from,$to) = split (/\t/, $edge);
    print "\$GRAPH->add_edge(\"${from}\",\"${to}\");\n";
   }

what is driving me crazy is that for each of the pairs, instead of producing -

Code:
$GRAPH->add_edge("G:1","G:2");

I am getting -

Code:
$GRAPH->add_edge("G:1","G:2
");

What I can't figure out is why the statement appears over two lines and breaks after the second node - if anyone has a clue how to fix that, I would be really grateful :)
 
you need to chomp() the input lines, looks like a newline is causing this problem.

Code:
foreach my $edge (@edgelistarray){
   chomp($edge);
   #the rest of code your code here
}
 
Not tested but try.

chomp($from,$to);
print ....
 
You'll need to put a chomp in there somewhere to remove the "\n" character from your input. Try something like:
Code:
foreach my $edge (@edgelistarray) {
    [b]chomp $edge;[/b]
    my($from,$to) = split (/\t/, $edge);
    print "\$GRAPH->add_edge(\"${from}\",\"${to}\");\n";
}
 
thanks to all, it was chomp that did the job :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top