We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

could you explain this cfscript please?

pigmentarts
pigmentarts asked
on
Medium Priority
378 Views
Last Modified: 2013-12-24
been searching on this site for some info on using CFHTTP, found this code (below) which pulls links, works greate.

could any one explain the bits in the cfscript tag line by line? it seems to do so much in so little code!!!

oany please is in the in array dump it shows the links and the link description, but i dont see where this is in the script.

<cfscript>
function parseURLs(obj)
{
   arr=arraynew(2);set=structnew();
   set.pos=arraynew(1);set.pos[1] = 0;
   while(true){
      set=refindnocase("<a[^>]+href=[""']([^""']*)[""'][^>]*>([^<]*)</a>",obj,set.pos[arraylen(set.pos)]+1,true);
      if(arraylen(set.len) lt 3) break;
           arr[arraylen(arr)+1][1]=mid(obj,set.pos[2],set.len[2]);
           arr[arraylen(arr)][2]=mid(obj,set.pos[3],set.len[3]);
    }
   return arr;
}
</cfscript>

<cfhttp method='get' url='http://google.com'></cfhttp>
<cfset NewArray = parseURLs(cfhttp.FileContent)>
<cfdump var="#NewArray#">  

thanks in advance :)
Comment
Watch Question

<cfscript>
function parseURLs(obj)
{
      <!--- Initialize variables --->
      arr=arraynew(2);set=structnew();
      <!--- Create a one-dimensional array and name it "pos" --->
      set.pos=arraynew(1);
      <!--- Set the first value of the "pos" or position array to 0 --->
      set.pos[1] = 0;
      <!--- Loop --->
      while(true){
            <!--- search for a link (<a href...) tag in the html (passed in as obj to the function) starting at the position+1 specified in the last element of the set.pos array --->
            set=refindnocase("<a[^>]+href=[""']([^""']*)[""'][^>]*>([^<]*)</a>",obj,set.pos[arraylen(set.pos)]+1,true);
            <!--- if it did not find a valid link then end the loop --->
            if(arraylen(set.len) lt 3) break;
            
            <!--- if it did find a valid link --->
            arr[arraylen(arr)+1][1]=mid(obj,set.pos[2],set.len[2]); <!--- add the value of the href attributes --->
            arr[arraylen(arr)][2]=mid(obj,set.pos[3],set.len[3]); <!--- add the string between the starting and ending <a> tags --->
      }
      <!--- Return the links from the page --->
      return arr;
}
</cfscript>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thanks for taking the time to explain this script to me, will review and play around with it. can i just ask with it been a one-dimensional array how come it holds 2 values for each position, or am i gettting this all wrong?
The set.pos variable is a one-dimensional array, but the two values that you're looking at are being stored in the two dimensional arr variable. The set structure is never returned and really serves no other real purpose than to hold the returned values from the REFindNoCase function.

Author

Commented:
o i see, assumed it was a two dimensional array just got set.pos confused with arr. thanks for the help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.