[RESOLVED] A "foreach" loop with NO existing variable?
Results 1 to 8 of 8

Thread: [RESOLVED] A "foreach" loop with NO existing variable?

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    22

    resolved [RESOLVED] A "foreach" loop with NO existing variable?

    On a website I'm learning to scrape, I type in part numbers. The website yields a best price and a worst price. I created the following regex to scrape the data when my inputted part numbers yield valid prices:

    preg_match_all("/BEST.*?(?<best>\d{0,3}\.\d{1,2}).*?,.*?WORST.*?(?<worst>\d{0,3}\.\d{1,2})/",$string,$matches);

    Then, I can happily loop through the matched values like this:

    foreach($matches as $var=>$part_number) {
    ${$part_number} = array (
    'bestprice'=>$matches['best'],
    'worstprice'=>$matches['worst']
    );
    }

    However, when I input an invalid part number, the website's response changes so much that nothing gets preg_matched and my foreach loop fails. I wish that when I input an invalid part number, "0.00" would get assigned to the array keys.

    Here is "pseudo-code" to illustrate what I'm wanting:

    foreach($matches as $var=>$part_number) {
    if(my_preg_match_routine works) {
    ${$part_number} = array (
    'bestprice'=>$matches['best'],
    'worstprice'=>$matches['worst']
    );
    } else {
    ${$part_number} = array (
    'bestprice'=>'0.00',
    'worstprice'=>'0.00'
    );
    }
    }

    I hope my question makes sense. :-)

  2. #2
    Junior Member benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    13
    Ah, a new regex question.....

    If we may, lets back up a bit. How exactly are you getting the data?

  3. #3
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,544
    foreach($matches as $var=>$part_number) {
    if(my_preg_match_routine works) {
    You want to do that the other way around, because you can't do the loop if there is nothing to loop on.

    But still, what benanamen said, here and in the other thread. The formatting of this string looks like something from the 1990s (I'd say 70s but formats for punch cards were more concise).
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  4. #4
    Junior Member
    Join Date
    Dec 2016
    Posts
    22
    Oh god... ROTF -- having PHP problems and reading these answers is actually BETTER than not having any PHP problems at all "the formatting looks like something from the 1990's" hahahaha -- you caught me! THATS when I learned PHP! Actually, more correctly stated: "That's when I learned PHP, enough to really screw things up" hahahah

    Okay I'll try my best to answer your questions WITHOUT giving away the website I'm trying to scrape..... ;-)

    The data, when the data is valid, looks like this:
    $string = 'BEST 25.65, MEDIUM 4.75, WORST 20.5, BEST 4, MEDIUM 8.9, WORST 160.31, BEST 15.65, MEDIUM 5.33, WORST 160.31, FINISHED';

    And my preg_match_all works fine, because it finds those numeric digits.

    However, the data, when SOME results are NOT returned, looks like this:
    $string = 'BEST:NULL:MEDIUM:NULL:WORST:NULL:BEST 4, MEDIUM 8.9, WORST 160.31, BEST 15.65, MEDIUM 5.33, WORST 160.31, FINISHED';

    That first set of "Best:NULL...etc" causes my WHOLE preg_match_all to fail.

    So I'm somehow trying to put an "if(!EMPTY)" line of code in there so the preg_match_all will simply return "0.00" for the first part of the returned data which has those "nulls" in it.

    e.g. array(
    [0] best=>0.00,
    medium=>0.00,
    worst=>0.00
    [1] best=>4
    medium=>8.9
    worst etc., etc.

    Leaving my code as it is now just causes my whole regex to fail and I get nothing out of the scrape.

    Note: I leared a long time ago when posting questions on forums to NEVER give the exact code and the exact script, because then it's like asking the experts (NogDog, Weedpacket, et al) to code for you for free....

    So I'm trying to ask actual "teaching/learning" questions..... like the "give a man a fish, eat for a day, teach a man to fish, etc.,) so I'm always careful to phrase my questions so I can learn how to do stuff on my own

  5. #5
    Junior Member benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    13
    carousel-carrousel-merry-go-round-ride-3d-model-animated-max-obj-fbx-c4d-lwo-lw-lws-ma-mb.jpg

    Note: I leared a long time ago when posting questions on forums to NEVER give the exact code and the exact script, because then it's like asking the experts (NogDog, Weedpacket, et al) to code for you for free....
    You learned wrong. We need to know what your working with. You Keep posting about your attempted solution which is highly likely the wrong one.

    You STILL have not answered HOW you are getting the data. You don't have to say from what site.

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,544
    Quote Originally Posted by Maryanne
    Note: I leared a long time ago when posting questions on forums to NEVER give the exact code and the exact script, because then it's like asking the experts (NogDog, Weedpacket, et al) to code for you for free....
    No - if you have the code you're not needing someone to write it for you.
    If you just dump all the code and say "what's wrong?" then that won't work because that's asking someone else to do all your debugging for you (a bunch of debugging tips is in the Debugging 101 thread.

    The reason we're asking this
    Quote Originally Posted by benanamen
    You STILL have not answered HOW you are getting the data. You don't have to say from what site.
    is because you don't really have a line $string = 'BEST:NULL:MEDIUM:NULL:WORST:NULL:BEST 4, MEDIUM 8.9, WORST 160.31, BEST 15.65, MEDIUM 5.33, WORST 160.31, FINISHED'; in your code. You're doing something else to make it and the real solution to your problem may well lie there.

    If you're scraping this off a web page, then (not ignoring AUPs and copyright) there may have been structure to its content that you could have used if you hadn't thrown it away (if you're having to scrape a web page to get it then either (a) the site may provide the data in a more machine-readable form already, or (b) they don't want you scraping it to begin with).
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  7. #7
    Junior Member
    Join Date
    Dec 2016
    Posts
    22
    Sorry, bye.

  8. #8
    Junior Member benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    13
    The OP's refusal to answer basic questions and wanting to hide the site being scraped from leads me to believe OP is up to something Illegal, in which case, none of us should be trying to help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •