We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am constantly running into problems regarding scope (at least I think this is my problem).
I am running a function called getLink (); and when i've found my link, I want to enter FoundLink(); BUT, I want to pass information from getLink() into FoundLink(); e.g. I want to use foundLink2 inside FoundLink() which isn't available.
Do I have to use a method instead? How do I get around this?
code:
import prohtml.*;
HtmlList htmlList;
String FoundLink = "http://en.wikipedia.org/wiki/coast_guard";
void setup(){
getLink();
}
void getLink(){
//enter your url here
try {
String URL = "http://en.wikipedia.org/wiki/Kevin_Bacon";
htmlList = new HtmlList(URL);
ArrayList links= (ArrayList) htmlList.getLinks();
//******************************* 1st LOOP SEARCH
for(int i = 5;i<20;i++){
String temp = links.get(i).toString();
String [] wikiLinkTemp = split(temp, "url:");
// String [] wikiLinkTemp.replace('%27',''s');
String []wikiLink = trim(wikiLinkTemp);
String baseURL = "http://en.wikipedia.org";
//String request = baseURL+ wikiLink;
String findLink = baseURL + wikiLink[1];
println( "First search, item " + i +" " + findLink);
// returned 6 wikiLinks
//******************************* 2nd LOOP SEARCH
try {
htmlList = new HtmlList(findLink);
ArrayList links2= (ArrayList) htmlList.getLinks();
for(int j = 5;j<20;j++){
String temp2 = links2.get(j).toString();
String [] wikiLink2Temp = split(temp2, "url:");
//println(wikiLink2[1]);
String []wikiLink2 = trim(wikiLink2Temp);
String findLink2 = baseURL + wikiLink2[1];
println( "Second search, item " + j +" " + findLink2);
if (findLink2.equals(FoundLink) == true) {
foundLink();
exit();
}
//****************************** 3rd LOOP SEARCH
try {
htmlList = new HtmlList(findLink2);
ArrayList links3= (ArrayList) htmlList.getLinks();
for(int k = 5;k<20;k++){
String temp3 = links3.get(k).toString();
String [] wikiLink3Temp = split(temp3, "url:");
//println(wikiLink2[1]);
String []wikiLink3 = trim(wikiLink3Temp);
String findLink3 = baseURL + wikiLink3[1];
println( "Third search, item " + k +" " + findLink3);
if (findLink3.equals(FoundLink) == true) {
foundLink();
exit();
}
//****************************** 4th LOOP SEARCH
try {
htmlList = new HtmlList(findLink3);
ArrayList links4= (ArrayList) htmlList.getLinks();
for(int l = 5;l<20;l++){
String temp4 = links4.get(k).toString();
String [] wikiLink4Temp = split(temp4, "url:");
//println(wikiLink2[1]);
String []wikiLink4 = trim(wikiLink4Temp);
String findLink4 = baseURL + wikiLink4[1];
println( "Fourth search, item " + k +" " + findLink4);
if (findLink4.equals(FoundLink) == true) {
println(" LOOK LOOK LOOK LOOK Found it");
println(findLink);
println(findLink2);
println(findLink3);
println(findLink4);
foundLink();
noLoop();
}
}// end 4th loop
} // end 4th Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 4th Search");
} // exception
//****************************** end of 4th loop
}// end 3rd loop
} // end 3rd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 3rd Search");
} // exception
//****************************** end of 3rd loop
} // end 2nd Loop
} // end 2nd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 2nd Search");
} // exception
//****************************** end of 2nd loop
} // end first loop
} // end 2nd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 1st Search");
} // exception
} // get links, first RUN
void foundLink(){
println(" LOOK LOOK LOOK LOOK Found it");
// println(findLink);
//println(findLink2);
//println(findLink3);
//println(findLink4);
noLoop();
exit();
}import prohtml.*;
HtmlList htmlList;
String FoundLink = "http://en.wikipedia.org/wiki/coast_guard";
void setup(){
getLink();
}
void getLink(){
//enter your url here
try {
String URL = "http://en.wikipedia.org/wiki/Kevin_Bacon";
htmlList = new HtmlList(URL);
ArrayList links= (ArrayList) htmlList.getLinks();
//******************************* 1st LOOP SEARCH
for(int i = 5;i<20;i++){
String temp = links.get(i).toString();
String [] wikiLinkTemp = split(temp, "url:");
// String [] wikiLinkTemp.replace('%27',''s');
String []wikiLink = trim(wikiLinkTemp);
String baseURL = "http://en.wikipedia.org";
//String request = baseURL+ wikiLink;
String findLink = baseURL + wikiLink[1];
println( "First search, item " + i +" " + findLink);
// returned 6 wikiLinks
//******************************* 2nd LOOP SEARCH
try {
htmlList = new HtmlList(findLink);
ArrayList links2= (ArrayList) htmlList.getLinks();
for(int j = 5;j<20;j++){
String temp2 = links2.get(j).toString();
String [] wikiLink2Temp = split(temp2, "url:");
//println(wikiLink2[1]);
String []wikiLink2 = trim(wikiLink2Temp);
String findLink2 = baseURL + wikiLink2[1];
println( "Second search, item " + j +" " + findLink2);
if (findLink2.equals(FoundLink) == true) {
foundLink();
exit();
}
//****************************** 3rd LOOP SEARCH
try {
htmlList = new HtmlList(findLink2);
ArrayList links3= (ArrayList) htmlList.getLinks();
for(int k = 5;k<20;k++){
String temp3 = links3.get(k).toString();
String [] wikiLink3Temp = split(temp3, "url:");
//println(wikiLink2[1]);
String []wikiLink3 = trim(wikiLink3Temp);
String findLink3 = baseURL + wikiLink3[1];
println( "Third search, item " + k +" " + findLink3);
if (findLink3.equals(FoundLink) == true) {
foundLink();
exit();
}
//****************************** 4th LOOP SEARCH
try {
htmlList = new HtmlList(findLink3);
ArrayList links4= (ArrayList) htmlList.getLinks();
for(int l = 5;l<20;l++){
String temp4 = links4.get(k).toString();
String [] wikiLink4Temp = split(temp4, "url:");
//println(wikiLink2[1]);
String []wikiLink4 = trim(wikiLink4Temp);
String findLink4 = baseURL + wikiLink4[1];
println( "Fourth search, item " + k +" " + findLink4);
if (findLink4.equals(FoundLink) == true) {
println(" LOOK LOOK LOOK LOOK Found it");
println(findLink);
println(findLink2);
println(findLink3);
println(findLink4);
foundLink();
noLoop();
}
}// end 4th loop
} // end 4th Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 4th Search");
} // exception
//****************************** end of 4th loop
}// end 3rd loop
} // end 3rd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 3rd Search");
} // exception
//****************************** end of 3rd loop
} // end 2nd Loop
} // end 2nd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 2nd Search");
} // exception
//****************************** end of 2nd loop
} // end first loop
} // end 2nd Loop try
catch (Exception e){
println("Hey, that’s not a valid index!, skip 1st Search");
} // exception
} // get links, first RUN
void foundLink(){
println(" LOOK LOOK LOOK LOOK Found it");
// println(findLink);
//println(findLink2);
//println(findLink3);
//println(findLink4);
noLoop();
exit();
}
Answers
Without looking to the code, I would either assign the the return of the function to a global var and use it to call the next, or call it using the function itself like:
the functions:
String getLink(){ //get the link and return theLink; }
void doSomeWithLink(String link){ // use link passed as a parameter }
using:
//global var String foundLink; foundLink = getLInk(); doSomeWithLink(foundLInk); //or doSomeWithLInk(getLInk());
side note, by convention functions begins with a small letter. And some people prefers that they have an action as a name as they do things, like openLink() or parseLink()