locked
Basic Looping with Array Question RRS feed

  • Question

  • I am trying to write a code below to go through a webpage and display all the words part in it
    As this URL have got multiple pages like below

    www.webpage.com/note?page=1
    www.webpage.com/note?page=2

    Below is my code

    $url = www.webpage.com/note
    for ($Page=1; $Page -le 3; $Page++)
    {
    $html = $c.DownloadString($url+"?page=$Page")
    $html = #some code that perform some matching to get all the string starting with Part. This is working.
    [Array]::Sort([array]$html)
    "Here are the parts" | add-content -Path C:\test.txt
    $html | add-content -Path C:\test.txt -Encoding ASCII
    }

    I get the output below
    Here are the parts
    Part 1
    Part 3
    Part 4
    Here are the parts
    Part 2
    Part 6
    Here are the parts
    Part 5

    How do I get the below output sorted?
    Here are the parts
    Part 1
    Part 2
    Part 3
    Part 4
    Part 5
    Part 6


    Thanks



    • Edited by cyw77 Sunday, March 31, 2013 10:09 PM
    Sunday, March 31, 2013 9:28 PM

Answers

  • There are multiple ways to do that. 

    Here's one that is hopefully intuitive:

    $url = www.webpage.com/note
    
    $html = @()
    for ($Page=1; $Page -le 3; $Page++)
      {
       $html += $c.DownloadString($url+"?page=$Page")
      }
    
    $html = #some code that perform some matching to get all the string starting with Part. This is working.
    [Array]::Sort([array]$html)
    "Here are the parts" | add-content -Path C:\test.txt
    $html | add-content -Path C:\test.txt -Encoding ASCII
    


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by cyw77 Monday, April 1, 2013 10:07 AM
    Monday, April 1, 2013 3:15 AM

All replies

  • once you've collected the data, use sort on it. For example, from your file:

    Get-Content C:\test.txt | Sort-Object

    Sunday, March 31, 2013 9:38 PM
  • once you've collected the data, use sort on it. For example, from your file:

    Get-Content C:\test.txt | Sort-Object

    Alex

    Still not the output I wanted

    I got multiple lines of

    Here is the part

    Here is the part

    It only needs to have 1 occurence.


    Thanks

    Sunday, March 31, 2013 9:49 PM
  • Below is my code

    $url = www.webpage.com/note
    for ($Page=1; $Page -le 3; $Page++)
    {
    $html = $c.DownloadString($url+"?page=$Page")
    $html = -match "Part.+?" | select-uniq
    [Array]::Sort([array]$html)
    "Here are the parts" | add-content -Path C:\test.txt
    $html | add-content -Path C:\test.txt -Encoding ASCII
    }

    The syntax for -match is wrong.

    The correct is:

    <string[]> -Match <regular-expression>

    That was a typo. I have trying to illustrate that I have multiple occurance of

    Here is the part


    Thanks

    Sunday, March 31, 2013 9:49 PM
  • Arpoador

    Let's us not worry about the match. I have got it working. I am just trying to illustrate the multiple occurance

    When I take it out of the loop

    Below is my output

    Here are the parts

    Part 5

    Meaning it will only read the from the URL www.webpage.com/note?page=3

    All the URL www.webpage.com/note?page=1 and www.webpage.com/note?page2 are ignored.


    Thanks

    Sunday, March 31, 2013 10:08 PM
  • Can you show us your revised code?
     
     
    Sunday, March 31, 2013 10:11 PM
  • I have updated the initial question.

    Thanks

    Sunday, March 31, 2013 10:18 PM
  • I believe it's doing that because you're doing those matches per page, so you're getting a set of results per page.  To get one set of results, use the loop to download the pages and assemble them into one array, then run the rest of the script against that array.  One array with all the data, one set of results with all the data.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Monday, April 1, 2013 2:10 AM
  • Yes, those results returned per page are stored in an array. With your suggestion, do you have any idea how to modify my code to fit that? I am bad at powershell.

    Thanks

    Monday, April 1, 2013 2:27 AM
  • There are multiple ways to do that. 

    Here's one that is hopefully intuitive:

    $url = www.webpage.com/note
    
    $html = @()
    for ($Page=1; $Page -le 3; $Page++)
      {
       $html += $c.DownloadString($url+"?page=$Page")
      }
    
    $html = #some code that perform some matching to get all the string starting with Part. This is working.
    [Array]::Sort([array]$html)
    "Here are the parts" | add-content -Path C:\test.txt
    $html | add-content -Path C:\test.txt -Encoding ASCII
    


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by cyw77 Monday, April 1, 2013 10:07 AM
    Monday, April 1, 2013 3:15 AM
  • There are multiple ways to do that. 

    Here's one that is hopefully intuitive:

    $url = www.webpage.com/note
    
    $html = @()
    for ($Page=1; $Page -le 3; $Page++)
      {
       $html += $c.DownloadString($url+"?page=$Page")
      }
    
    $html = #some code that perform some matching to get all the string starting with Part. This is working.
    [Array]::Sort([array]$html)
    "Here are the parts" | add-content -Path C:\test.txt
    $html | add-content -Path C:\test.txt -Encoding ASCII
    


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Good work mjolinor. It worked but I had to add the following line below as I am getting duplicates of each results.

    $html | Get-Unique | add-content -Path C:\test.txt -Encoding ASCII


    Thanks

    Monday, April 1, 2013 10:09 AM