Evaluating array and skipping certain results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JackM

    Evaluating array and skipping certain results

    I'm having a brain freeze on this. I have a script where I am reading
    the contents of a file into an array. I have that up and working with no
    problem. Then I'm exploding the array using

    while ($line = fgets($fd, 4096))
    {
    $arrayData = explode("-", trim($line));
    }

    Works fine. But what I want to do now is evaluate arrayData[2] to see if
    it contains the integers "00" (that's zero zero) and if it does, I want
    it to skip that entry. I know I need to set it up with
    if arrayData[2] == "00" {

    But I'm drawing a blank on how to get it to skip this entire line in the
    final results. There may be multiple instances of this happening in each
    file and I want to skip them all. Can anyone point me in the right
    direction for the coding to accomplish this? Thanks.
  • NC

    #2
    Re: Evaluating array and skipping certain results

    JackM wrote:[color=blue]
    >
    > I have a script where I am reading the contents of a file into
    > an array. I have that up and working with no problem. Then I'm
    > exploding the array using
    >
    > while ($line = fgets($fd, 4096))
    > {
    > $arrayData = explode("-", trim($line));
    > }
    >
    > Works fine. But what I want to do now is evaluate arrayData[2]
    > to see if it contains the integers "00" (that's zero zero) and
    > if it does, I want it to skip that entry.[/color]

    That's where continue operator comes in handy:

    while ($line = fgets($fd, 4096)) {
    $arrayData = explode("-", trim($line));
    if (arrayData[2] == '00') {
    continue;
    }
    // do stuff
    }

    Alternatively, you can simply do:

    while ($line = fgets($fd, 4096)) {
    $arrayData = explode("-", trim($line));
    if (arrayData[2] <> '00') {
    // do stuff
    }
    }

    Cheers,
    NC

    Comment

    • Daniel Tryba

      #3
      Re: Evaluating array and skipping certain results

      NC <nc@iname.com > wrote:[color=blue]
      > if (arrayData[2] == '00') {
      > if (arrayData[2] <> '00') {[/color]

      A recipie for disaster. If $arrayData[2] equals '0' this evalutates to
      true and false resp. If OP really wants to check for '00' he should use
      strcmp, === (or !==).

      Comment

      • Ewoud Dronkert

        #4
        Re: Evaluating array and skipping certain results

        On Fri, 20 May 2005 22:21:34 -0400, JackM wrote:[color=blue]
        > while ($line = fgets($fd, 4096)) {
        > $arrayData = explode("-", trim($line));
        > }[/color]

        What might be conceptually simpler is

        function nonzero($line) {
        $a = explode('-', trim($line));
        return strcmp('00', $a[2]);
        }
        $rawdata = file('myfile.ex t'); //read into array by lines
        $filtered = array_filter($r awdata, 'nonzero');

        but it may lead to doing things twice if you need the exploded line
        again later. If you only explode() to check for '00', this is probably
        fine. If you do need to process each line in its exploded form, you
        might want to explode first, then filter:

        function expline($line) {
        return explode('-', trim($line));
        }
        function nonzero($arr) {
        return strcmp('00', $arr[2]);
        }
        $rawdata = file('myfile.ex t');
        $expdata = array_map('expl ine', $rawdata);
        $filtered = array_filter($e xpdata, 'nonzero');

        Then process the elements of $filtered (each element is now an array),
        maybe with array_map() again. Nice if you like functional programming.
        Shortest imperative version:

        $rawdata = file('myfile.ex t');
        foreach ($rawdata as $line) {
        $arr = explode('-', trim($line));
        if (strcmp('00', $arr[2])) { //skip if arr[2] is 00
        //do stuff with $arr[0], $arr[1], etc.
        }
        }


        --
        Firefox Web Browser - Rediscover the web - http://getffox.com/
        Thunderbird E-mail and Newsgroups - http://gettbird.com/

        Comment

        • JackM

          #5
          Re: Evaluating array and skipping certain results

          Ewoud Dronkert wrote:
          [color=blue]
          > Shortest imperative version:
          >
          > $rawdata = file('myfile.ex t');
          > foreach ($rawdata as $line) {
          > $arr = explode('-', trim($line));
          > if (strcmp('00', $arr[2])) { //skip if arr[2] is 00
          > //do stuff with $arr[0], $arr[1], etc.
          > }
          > }[/color]

          Thanks to all who offered solutions. You people are the best. It seems
          that Ewoud's suggestion is the easiest to implement and, better still,
          it works for my purposes. ;) I'll have to read up on the continue
          control structure to see if it would be suitable for other things I do.
          Much obliged to everyone.

          Comment

          Working...