Where should I put the loops

Discussion in 'Ruby' started by Li Chen, Jul 17, 2008.

  1. Li Chen

    Li Chen Guest

    Hi all,

    I try to write a script to fecth some webpages from the internet, parse
    the webpages and write them into csv format file.

    Here are the formats of webpages:
    website....A001
    ...
    website....A101

    website....B001
    ...
    website....B401

    website....Z001
    ...
    website....Z301

    I use two loops to do the job. 1) one loop is to iterate from letter A
    to Z,
    which I have it done by iterate an array containing letter A to Z.
    2) another loop is to iterate the number from 1 to whatever numbers I
    prefer (or using a while loop).

    If I write the script in a procedural way I have no problem at all but
    they look kind of ugly. Now I try to rewrite them with OOP method. My
    confusion here is that where should I write the loops: in the main
    scripts and define a method for them and call the method in the main
    scripts?

    Thanks,

    Li
    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Jul 17, 2008
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    What you can do is write three loops so

    [A..Z].each do |letter|
    [1...num].each do |number|
    #Process the website in here
    process_website letter number
    end
    end

    As for OOP you could have a Parse class and in and a AppRunner class (which
    holds all the websites). the process_website would be in the AppRunner class
    and would create a new parser for each website, parse the website and then
    either if the websites are indepent (have the parse take care of the writing
    to csv), or if dependent in some manner (have the AppRunner write to cvs)

    Reuben Doetsch



    On Thu, Jul 17, 2008 at 9:47 AM, Li Chen <> wrote:

    > Hi all,
    >
    > I try to write a script to fecth some webpages from the internet, parse
    > the webpages and write them into csv format file.
    >
    > Here are the formats of webpages:
    > website....A001
    > ...
    > website....A101
    >
    > website....B001
    > ...
    > website....B401
    >
    > website....Z001
    > ...
    > website....Z301
    >
    > I use two loops to do the job. 1) one loop is to iterate from letter A
    > to Z,
    > which I have it done by iterate an array containing letter A to Z.
    > 2) another loop is to iterate the number from 1 to whatever numbers I
    > prefer (or using a while loop).
    >
    > If I write the script in a procedural way I have no problem at all but
    > they look kind of ugly. Now I try to rewrite them with OOP method. My
    > confusion here is that where should I write the loops: in the main
    > scripts and define a method for them and call the method in the main
    > scripts?
    >
    > Thanks,
    >
    > Li
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    reuben doetsch, Jul 17, 2008
    #2
    1. Advertising

  3. Li Chen

    Li Chen Guest

    reuben doetsch wrote:
    > What you can do is write three loops so
    >
    > [A..Z].each do |letter|
    > [1...num].each do |number|
    > #Process the website in here
    > process_website letter number
    > end
    > end


    Hi Reuben,

    Thank you very much for the neat script. I have another question:
    within the nested loop the maximum num is not fixed, for example, there
    are A001-A201 for letter A and B001-B333 for letter B.... And most of
    the time I don't know what is the maximum page I can fetch for each
    letter. But I know if the page doesn't exit that Ruby will fetch the
    same papge again and again until it comes to the upper limit of loop. In
    order to break out of the loop what I do is that I compare the website
    address of current page and the previous page. If they are the same I am
    going to break out the current loop and go to next letter. I wonder how
    you implement this idea into the current script.

    Thanks,

    Li


    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Jul 21, 2008
    #3
  4. Li Chen

    Kaldrenon Guest

    On Jul 21, 9:25 am, Li Chen <> wrote:
    > > [A..Z].each do |letter|
    > >        [1...num].each do |number|
    > >               #Process the website in here
    > >                process_website letter number
    > >        end
    > > end


    > In order to break out of the loop what I do is that I  compare the website
    > address of current page and the previous page. If they are the same I am
    > going to break out the current loop and go to next letter. I wonder how
    > you implement this idea into the current script.


    If I understand what you're asking correctly, it would be something
    like this (this is pseudo-code)

    [A..Z].each do |letter|
    old_website = nil
    [1...num].each do |number|
    #Process the website in here
    new_website = fetch_website(letter, number)
    if new_website != old_website
    process_website(new_website)
    else
    #break out of num-loop
    end
    old_website = new_website
    end
    end

    This is assuming that the only time you will ever have duplication is
    when you have reached the end of the loop for a given letter. Is that
    correct?
    Kaldrenon, Jul 21, 2008
    #4
  5. Li Chen

    Li Chen Guest

    Kaldrenon wrote:
    > This is assuming that the only time you will ever have duplication is
    > when you have reached the end of the loop for a given letter. Is that
    > correct?


    Thank you very much. The code really cool.


    Li

    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Jul 21, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,120
    Big and Blue
    Jan 30, 2005
  2. Mickey Segal
    Replies:
    0
    Views:
    347
    Mickey Segal
    May 12, 2004
  3. Gabriel Rossetti
    Replies:
    3
    Views:
    533
    Jerry Hill
    Apr 25, 2008
  4. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl Misc
    Replies:
    34
    Views:
    389
    Charlton Wilbur
    Jan 31, 2005
  5. Me
    Replies:
    2
    Views:
    229
Loading...

Share This Page