Array Not Pulling Any Information

Recommended Posts

atnextc 0

atnextc 0

I"m trying to create "1" array or more if needed to be able to pull very specific data from HTML page, while running in a loop to generate configuration commands for our team. The problem that I am running into is....There are some portions of the configuration that will only need to be written 1 time no matter how many times the array is being looped for, while at other times I'll need all "6" instances of a particular variable written to a file one right after another.

I believe that this is the correct way to create my array and gather the information but console write isn't displaying anything. I"ve been reading all about _StringBetween and Arrays for about 3 weeks, and i've honestly gotten so much partial stuff working I've decided to start over. The code below is the heart and soul of everything that I need to work off of though so if this doesn't work correctly everytime then there is no point in trying to work out the other stuff as well.

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

_StringBetween returns a zero based array, so the first element array[0] does not contain the number of elements contained in the array. Instead you must use Ubound to get the number of elements in the array. The normal syntax would be something like the following.

For $i = 0 To Ubound($array) -1

Of course you still need to check that the function actually returned an array to begin with.

Share this post

Link to post

Share on other sites

atnextc 0

atnextc 0

czardas....all of this data is being pulled from one HTML page. I need these to be looped each time all the data is "parsed" from one page then move on to the next. so there will always onl be 1 element in any of the arrays.

I guess i'm confused if I need to declare each of the variables as its each own array then use UBOUND for each instance that I need the variables printed in my email form or is there a better "less code" way of writing this or can I declare all of these variables as global instead of having to declare all of the "_StringBetween" under each function that I will be using in this script which will basically be 7 different varations of text using all of the same variables

Edited June 25, 2013 by atnextc

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

Let's suppose you get an array with 1 element and that has a numeric value of 1. You then loop from 0 to $array[0]. That's looping through two elements of an array which only contains one element. This is the first thing wrong with your original code - which will nearly always throw an error. There are some other issues, but this is the main problem you have to solve before you can fix anything else.

You wrote:

For $i = 1 to $projectnextstep[0]

This is the main issue. $projectnextstep[0] should be a string, and not necessarilly numeric. Read the help file about _StringBetween again. It states:

$array[0] contains the first found string.

I'm not quite sure what the rest of your code is about, but instead of using so many arrays you could create a single 2D array to hold all the data. You still need to understand the difference between a 0 based array and an array which contains the number of entries in $array[0].

Share this post

Link to post

Share on other sites

atnextc 0

atnextc 0

so basically all i'm doing is grabbing the interface names and router name from a web page, then moving to the next webpage and grabbing the same date,....router and interfaces, then using those variables to fill in a script written

czardas 1,197

czardas 1,197

I think you are having difficulty because you are trying to solve the problem with your project, instead of understanding the tools and learning how to use them. First make a simple 2D array and play around with it. Then you need to figure out how to use the same methods to solve a more complicated problem (your original problem). If you start learning about 2D arrays with your complicated example, you will just confuse yourself. The first and most important thing is to understand the points I made in my previous, post before messing about with 2D arrays.

As I see it, the elements in the array would be the "variables" posted above and the data in the array would always be "0" as there is always something filled in on these pages.

Please advise if I am on the right track with my thinking. And if so, the wiki doesn't really have a good example of how to set the elements in an array by using string split several times on the same string, which in this case is the html from one webpage.

Edited June 25, 2013 by atnextc

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

Think - how would you lay the information out in a table? In your example, each row should represent one type of return data and have a number of entries. The table has to be large enough to hold all the rows and columns needed. To create this table you will have to loop through each row, and then in each row you have to loop through each column to fill the array with the correct contents. So this involves a loop nested inside another loop.

These will just be meaningless words to you if you don't try out some simple examples to check you understand correctly what is happening. You may also need to use Redim - if you need to add more columns.

Share this post

Link to post

Share on other sites

atnextc 0

atnextc 0

thank you for this information czardas......i'm not sure i'm accurately conveying what exactly i'm hoping to achieve....as I don't believe that I will need more than 1 column, and the number of rows would be the number of the variables that i'm trying to get from the HTML page above.

Here is an example that I wrote that, the "console write" is one of the many things that this script that i'm trying to write will do. In this example below this works perfectly. I just couldn't get it to work properly in my code posted in #1, I"m not a coder or programmer, and I fell based on the results that I have achieved that I'm almost there but as you said not fully understanding the arrays is very hindering, the problem is I have about 5 days to get this whole script working...which hinges on me getting the data pulled from the HTMl pages accurately and looping correctly the array or variable is empty...indicating that the wiki page doesn't exist yet.

I really appreciate your help tonight....I'm honestly not looking for a post consisting of "here ya go" but a start using the actual data that i've provided I think would greatly help my understanding as its what i'm working with, versus using an example that i'm trying to make up as I go along, because i will always default back to " let me try it with a real world example". Again, no disrespect to any of your ideas or suggestions.....its just frustrating when I can get "accurate results" some times, and feel like I have a handle on it, then when I go to "expand/continue" the script to include other scenarios it all breaks........

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

It's not only because it's more useful to you to be able to solve problems, that I focus on the basics - it's also because it's complicated to pull data from html. There is no single solution as far as I know. If _StringBetween is working in this case, then you can use that method. The function returns an array. The syntax you used in the first post was not accessing the array correctly. which I tried to explain in >post #5 . Part of your code should really look something like this:

This first checks if $projectnextstep is an array before trying to access however many elements it contains. Another tip: Instead of writing $L =$L +1, you could also write $L += 1. Perhaps you will understand it better after a good night's sleep - if you're tired right now.

Share this post

Link to post

Share on other sites

atnextc 0

atnextc 0

Below is a direct copy of data i'm working with...this is a mediawiki page in "edit" mode this is what i'm pulling with the top portion of the script.

From there, i'm interested in whats between = and @CRLF on each line.

There is a gui on my script that a user would enter a project name which would pull the "HTML" from our wiki server and parse out said information to basically place in a "find and replace" template so that we don't have to type our configurations. The issue is that there are times where we're doing more than 1 project at a time, which would involve the loop within another loop.

Please let me know if this is helpful in what i'm trying to accomplish.

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

The above information doesn't mean a great deal to me - I understand it's some kind of sales related information. Perhaps someone else knows more about what you are doing. In any case I imagine you will want to use nested loops. Perhaps your method could be FOR each project, FOR each relevant item, replace some information NEXT NEXT. How you organise the data depends on how you want to access it. Just loop through and write the results to a file, or store all the information from all the projects in some kind of table for future reference. It all depends on the intended purpose.

I notice all the categories are unique. What I don't know is whether they always occur in the same order and what exactly you intend to replace. So finding the best solution isn't particularly easy for anyone.

Share this post

Link to post

Share on other sites

atnextc 0

atnextc 0

This code "works" in that the correct value for the variable is written to the console. It's not stopping when the array "read any of them" are empty, it just keeps looping through and i'm not sure why.

Share this post

Link to post

Share on other sites

czardas 1,197

czardas 1,197

It keeps looping because you use $i and $I. They are the same variable. The value for $i keeps getting reset in the For loop, so you never can exit the Do loop. Variables are not case sensitive. You have to choose another name for one of those two variables.