We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Possible array help
Page Index Toggle Pages: 1
Possible array help? (Read 1091 times)
Possible array help?
Dec 19th, 2009, 2:52pm
 
Hi all, I've been wrestling with my sketch for days trying to get it to do what I want, but no joy, hopefully someone can spare a little time to help (& highlight my novice mistakes)!

Currently my sketch checks a webpage for a number twice each minute and adds it to an array.

Next it draws 60 cubes in a line, each cube sits at a height depending on the corresponding number in the array (if the webpage number at minute 38 was 2, array[38] = 2 etc).

This all works fine, but I want to add a bit more complexity to it by making the blocks drawn for 'hour 2' not over the top of the last but  a block's width behind, so over 3 hours i'd have 3rows of blocks in a 'wall'.

I'd really appreciate anyone helping me achieve this, or helping me tidy up my code, I've been learning Processing (as my first programming language) in isolation, and it seems as soon as I start getting somewhere it suddenly becomes incomprehensible! :-[

Many thanks, L.

(I have only used simple code, except the regex bit - returns an int. I will explain anything i have neglected to mention if this doesnt make sense! thanks again).

Code:
PFont font;
String workCountStringh;
int workCountStringInt1h;
int workCountStringInt2h;
int workCountInth;
int ch = 255;
int lastHourHitsh;
int thisMinuteHitsh;
int endHourHitCounth;
int[] hitsThisMinuteh = new int[60];
int[] rateArrayh = new int[60];
float xh;
float yh;
float z = 1;
int backSpace;
int a;
int b;
int startHourHitCounth = 110561;


void setup() {
 size(3500, 2000, P3D);
 font = createFont("ArialMT", 15);
 textFont(font);
 background(0);
 text("a, w, s, d, r, f, p", 10, 10);  
 String urlh = "http://cagd.leedsmet.ac.uk/";
 if(urlh != null){
   String[] htmlTexth = loadStrings(urlh);    
   String htmlTextJoinedh = join(htmlTexth, " ");
   int starth = htmlTextJoinedh.indexOf("relative; top: 8px\">"); //where in html string to start.
   int endh = htmlTextJoinedh.indexOf("</p>", starth); //where in html string to end.
   String selectedTexth = htmlTextJoinedh.substring(starth, endh); //make new string of my selection from all html text.
   String pageh = htmlTextJoinedh;
   String regexh = "id=\"total_count\".*?>([\\d,]+)</p>";
   String valueh = null;
   Matcher m1 = Pattern.compile(regexh).matcher(pageh);
   if (m1.find()){
     valueh = m1.group(1);
   }  
   workCountStringInt1h = int(valueh.substring(0,3));
   workCountStringInt2h = int(valueh.substring(4,7));
   workCountInth = ((workCountStringInt1h*1000)+(workCountStringInt2h));

   for(int i = 0; i < hitsThisMinuteh.length; i++){  //initialise my arrays to zero's.
     hitsThisMinuteh[i] = 0;
   }
 }
}

void draw() {
 smooth();
 lights();

 hourtest();

 if(second() == 05){
   save("shotauto"+hour()+":"+minute()+":"+second()+".jpg"); //this makes it stop working!
 }

}//end of draw.





void hourtest(){


 if((second() == 1) || (second() == 51)){
   String urlh = "http://cagd.leedsmet.ac.uk/";
   if(urlh != null){
     String[] htmlTexth = loadStrings(urlh);    
     String htmlTextJoinedh = join(htmlTexth, " ");
     int starth = htmlTextJoinedh.indexOf("relative; top: 8px\">"); //where in html string to start.
     int endh = htmlTextJoinedh.indexOf("</p>", starth); //where in html string to end.
     String selectedTexth = htmlTextJoinedh.substring(starth, endh); //make new string of my selection from all html text.
     String pageh = htmlTextJoinedh;
     String regexh = "id=\"total_count\".*?>([\\d,]+)</p>";
     String valueh = null;
     Matcher m1 = Pattern.compile(regexh).matcher(pageh);
     if (m1.find()){
       valueh = m1.group(1);
     }  
     workCountStringInt1h = int(valueh.substring(0,3));
     workCountStringInt2h = int(valueh.substring(4,7));
     workCountInth = ((workCountStringInt1h*1000)+(workCountStringInt2h));

   }
 }

 if((second() == 01)){//finds work count at start of each minute.
   startHourHitCounth = workCountInth;
   println(startHourHitCounth+"startminutecount");

   if(minute()>0){
     String[] rateArrayStringsArrayh = loadStrings("rateArrayMinutes"+(minute()-1)+".txt");//should mean it can run over an hour break.
     String joinedRateArrayh = join(rateArrayStringsArrayh, ",");
     rateArrayh = int(split(joinedRateArrayh, ","));
   }
   else if(minute() == 0){
     String[] rateArrayStringsArrayh = loadStrings("rateArrayMinutes59"+".txt");
     String joinedRateArrayh = join(rateArrayStringsArrayh, ",");
     rateArrayh = int(split(joinedRateArrayh, ","));    
   }


   b = a+1;
   if(a<b){//makes it draw only once each refresh?
   background(0);

     for(int i=0; i<rateArrayh.length-1; i++){// need to stop this from repeating.
       noFill();
       rotateX(xh);
       rotateY(yh);//row controls.

       pushMatrix();  
       for(int n=0;n<rateArrayh.length;n++){
         pushMatrix();
         translate(50, height-200, 100);
         translate((width/2 + (( (n-1)-30)*50)),  -(rateArrayh[n]*50), -a);

         fill(255,255, 3*(a/50));
         box(50, 50, 50);
         text(n+1, 0, 25);
         popMatrix();
       }

       popMatrix();
     }


     println("drawn");

   }
 }



 if((second() == 56)){//finds work count at end of each minute. MAKE CORRECT WHEN DONE!
   endHourHitCounth = workCountInth;
   println(endHourHitCounth+"endminutecount");

   thisMinuteHitsh = endHourHitCounth-startHourHitCounth;           //FINDS NUMBER OF HITS THIS minute. - ADDS TO 'LAST minute' ARRAY ETC 60 LONG.
   hitsThisMinuteh[minute()] = thisMinuteHitsh;
   println(thisMinuteHitsh +"thisminuteHitcount");
   println(hitsThisMinuteh);


   String hitsThisMinuteJoinedh = join(nf(hitsThisMinuteh, 0),  ",");
   String[] hitsThisMinuteJoinedListh = split(hitsThisMinuteJoinedh, ",");//EXPORTS RATE ARRAY.
   saveStrings("rateArrayMinutes"+minute()+".txt", hitsThisMinuteJoinedListh);//how to draw these each time!?
 }

}
Re: Possible array help?
Reply #1 - Dec 20th, 2009, 12:26am
 
If you can provide us with a working sketch, which only contains the code you need help with (e.g. with fake input data). It is a lot easier to help you out Smiley
Re: Possible array help?
Reply #2 - Dec 20th, 2009, 1:43am
 
Some remarks, as I read your code, if you don't mind:
- if(urlh != null) isn't really necessary as the urlh is a literal string).
- //initialise my arrays to zero's loop isn't necessary, Java initializes primitives variables and arrays with default values (0, false, etc.).
- smooth() call can be moved to setup.
- if(second() == 05) beware of numbers starting with zero! They have a special meaning in Java: they denote octal number (an old, silly heritage of C). Here, it is innocuous, but you get a compilation error if you tested against 09 for example... Smiley
- urlh: should be defined at global level to avoid duplicate definition.
- Duplicate code: move it in a function. Duplicate code is bad. Smiley
- if(minute()>0) [...] else if(minute() == 0){ Mmm, a simple else is probably enough here...
- I fail to see the point of joining on a char and splitting on the same char, unless this char is also in the loaded strings.
- Just set the string (file name) in the tests, and do the loadStrings, join & split after them. Duplicate code is badWink
- b = a+1; if(a<b) I guess it is always true...
- No need to pushMatrix around the loop, as it is done on each loop.

Now to your question, perhaps you can use translate() to move down the whole drawing process.
Re: Possible array help?
Reply #3 - Dec 20th, 2009, 3:07am
 
OK JR good point, I'll pretend the regex isnt there and make a better example now, thanks!

No PhiLo, I dont mind at all! - this forum is the only place I can get any kind of feedback on my work so its more than helpful! - I'll get those changes made and get back with a better example. L.
Re: Possible array help?
Reply #4 - Dec 20th, 2009, 5:15am
 
Quote:
Posted by: JR      Posted on: Today at 12:26am
If you can provide us with a working sketch, which only contains the code you need help with (e.g. with fake input data). It is a lot easier to help you out

Right, I've cut it down, so this should work for you now Smiley
Quote:
Posted by: PhiLho      Posted on: Today at 1:43am
- I fail to see the point of joining on a char and splitting on the same char, unless this char is also in the loaded strings.

Yes I know, I had trouble converting my array of ints into strings to enable me to use saveStrings on its own, I realise its a bit clumsy, but  I think it'll have to do for now!
Quote:
Now to your question, perhaps you can use translate() to move down the whole drawing process.

I was thinking of using translate(), but I am aiming to make the sketch draw 'all previous blocks' each time, so I think I need to make a loop that counts and reads any previously exported arrays (created each minute @ 56secs).
The files the sketch is creating ( saveStrings("rateArrayMinutes"+minute()+".txt", hitsThisMinuteJoinedListh); ) are numbered, is there a way I can make a for loop 'look through sketch folder for files named "rateArrayMinutes"+NUMBER+".txt", load the file into an array, draw it, then move onto the next suitable file until no more can be found?


Re: Possible array help?
Reply #5 - Dec 20th, 2009, 5:16am
 
Code:
PFont font;
String urlh = "http://cagd.leedsmet.ac.uk/";

String workCountStringh;
int workCountStringInt1h;
int workCountStringInt2h;
int workCountInth;
int ch = 255;
int lastHourHitsh;
int thisMinuteHitsh;
int endHourHitCounth;
int[] hitsThisMinuteh = new int[60];
int[] rateArrayh = new int[60];
float xh;
float yh;

float z = 1;
int backSpace;
int backSpaceStopper;
int a;
int b;
int startHourHitCounth = 110960;//change to current to stop big column.
int counter;


void setup() {
size(3500, 2000, P3D);
font = createFont("ArialMT", 15);
textFont(font);
background(255);
smooth();

getWorkCountInt_func();//gets my first work count number to stop huge column.

}//end of setup.


void draw() {
lights();

hourtest();

}//end of draw.


(new tab)
Code:
void getWorkCountInt_func() {


String[] htmlTexth = loadStrings(urlh);
String htmlTextJoinedh = join(htmlTexth, " ");
int starth = htmlTextJoinedh.indexOf("relative; top: 8px\">"); //where in html string to start.
int endh = htmlTextJoinedh.indexOf("</p>", starth); //where in html string to end.
String selectedTexth = htmlTextJoinedh.substring(starth, endh); //make new string of my selection from all html text.
String pageh = htmlTextJoinedh;
String regexh = "id=\"total_count\".*?>([\\d,]+)</p>";
String valueh = null;
Matcher m1 = Pattern.compile(regexh).matcher(pageh);
if (m1.find()){
valueh = m1.group(1);
}
workCountStringInt1h = int(valueh.substring(0,3));
workCountStringInt2h = int(valueh.substring(4,7));
workCountInth = ((workCountStringInt1h*1000)+(workCountStringInt2h));

}


(new tab)
Code:
void hourtest(){

if((second() == 5)){
println("saving image..");
save("shotauto"+hour()+":"+minute()+":"+second()+".jpg"); //this makes it stop working!
}

if((second() == 1) || (second() == 55)){
getWorkCountInt_func();
}

if((second() == 1)){//finds work count at start of each minute. MAKE CORRECT WHEN DONE!
startHourHitCounth = workCountInth;
println(startHourHitCounth+"startminutecount");




a = a+50; //should add space to new row once at end of each hour.
println("a = "+a);


if(minute()>0){
String[] rateArrayStringsArrayh = loadStrings("rateArrayMinutes"+(minute()-1)+".txt");//should mean it can run over an hour break, works as long as sketch is started before 56seconds into current minute.
String joinedRateArrayh = join(rateArrayStringsArrayh, ",");
rateArrayh = int(split(joinedRateArrayh, ","));
}else{
String[] rateArrayStringsArrayh = loadStrings("rateArrayMinutes59"+".txt");
String joinedRateArrayh = join(rateArrayStringsArrayh, ",");
rateArrayh = int(split(joinedRateArrayh, ","));
}

background(255);
for(int i=0; i<rateArrayh.length-1; i++){
noFill();
rotateX(xh);
rotateY(yh);//row controls.
translate(0, 0, backSpace);

for(int n=0;n<rateArrayh.length;n++){ //need to writ this to add counter number to file end to load all files it can find... - or make arrays eah time then load?
pushMatrix();
translate(50, height-200, 100);
translate((width/2 + (( (n-1)-30)*50)), -((rateArrayh[n]*50)/2), a);
//fill(255,255, 3*(a/50));
stroke(0);
box(50, 50+(rateArrayh[n]*50), 50);
text(n+1, 0, 25);
popMatrix();
}
}

// if(minute()==33){// THIS ADDS ROW IN SPACE ONCE EACH HOUR - MAKE END OF HOUR WHEN SORTED!
// a = a+50; //should add space to new row once at end of each hour.
// println("a = "+a);
// }
//println("drawn");
}

if((second() == 56)){//finds work count at end of each minute. MAKE CORRECT WHEN DONE!
endHourHitCounth = workCountInth;
println(endHourHitCounth+"endminutecount");

thisMinuteHitsh = endHourHitCounth-startHourHitCounth; //FINDS NUMBER OF HITS THIS minute. - ADDS TO 'LAST minute' ARRAY ETC 60 LONG.
hitsThisMinuteh[minute()] = thisMinuteHitsh;
println(thisMinuteHitsh +"thisminuteHitcount");
println(hitsThisMinuteh);

String hitsThisMinuteJoinedh = join(nf(hitsThisMinuteh, 0), ",");
String[] hitsThisMinuteJoinedListh = split(hitsThisMinuteJoinedh, ",");//EXPORTS RATE ARRAY.
saveStrings("rateArrayMinutes"+minute()+".txt", hitsThisMinuteJoinedListh);
counter = counter+1;
}
}


Page Index Toggle Pages: 1