SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Position In Hash - Not reset?

    I have a sizeable hash that I create by reading a flat file. The hash is already sorted, so I need to access it sequentially. I cannot access the values I need directly because the key does not correspond to the search I am doing.

    Here is the code:
    Code:
    my $branches = 0;
    	
    LOOK: foreach $key ( keys %header )
    {
      
        # Split line into an array using tab delimiter
        my @branch_fld = split(/\t/, $header{$key});
          
        if ( ($branch_fld[13] eq $some_value) && ($branch_fld[14] eq '4') )
        {
        	$branches++;
    
    	print OUTFILE "A whole bunch of stuff goes here...."
    
        }elsif ($branches > 0){
        		
    	last LOOK;
        }
    }
    My thought was that I could save some time...but the result was strange. If the value I was looking for was x, this code would:

    Start reading hash values:
    Key 1: value = a
    Key 2: value = a
    Key 3: value = b
    Key 4: value = b
    Key 5: value = b
    Key 6: value = x ***OK, now we start to print stuff
    Key 7: value = x ***print some more
    Key 8: value = z ***OK, now stop printing, and since $branches is > 0 then exit the loop.

    This works fine for the first value I need, but let's say the next value I want is "z". Since I start the foreach again, I expect the hash to start searching from Key 1. But what happened is that it would start searching from Key 9! This, of course, means that I miss the first "z" value which occurred on Key 8.

    Since Perl does not use a reset on arrays/hashes, I am a little confused here. If I simply change my code to read the entire hash every time, then I get what I want. But it adds unnecessary overhead.

    What think you? Am I missing something? Is my code flawed? Or is this just an anomaly of Perl (I use PHP heavily, so I am more accustomed to resetting array pointers).

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the excerpt from the man page of ActivePerl for the 'each' function.
    When the hash is entirely read, a null array is returned in list context (which when assigned produces a false (0) value), and undef in scalar context. The next call to each after that will start iterating again. There is a single iterator for each hash, shared by all each, keys, and values function calls in the program; it can be reset by reading all the elements from the hash, or by evaluating keys HASH or values HASH.

  3. #3
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well thanks monolitik. I missed that...it is good information to know.


Bookmarks

Posting Permissions

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