Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Array: inserting what isn't there

by l2kashe (Deacon)
on May 30, 2003 at 14:12 UTC ( [id://261854]=note: print w/replies, xml ) Need Help??


in reply to Array: inserting what isn't there

I just finished working on a similar problem. What I needed was to preinitialize an array so that later if a value was 0 it would report correctly, you can do the same basic thing ala

$max_size = 5; @array = (0) x $max_size; # put $max_size zeros in array # process file here.. for ( $i = 0; $i <= $#array; $i++ ) { # now set the value to lost if it is still zero $array[$i] = "$i.lost" unless ( $array[$i] ); }
Now this assumes that you will never have a 0 in a particular field, but if you can then you can use a different sentinal value

MMMMM... Chocolaty Perl Goodness.....

Replies are listed 'Best First'.
Re: Re: Array: inserting what isn't there
by Bilbo (Pilgrim) on May 30, 2003 at 14:16 UTC

    Why not initialise to undef then use unless (defined $array[$i])?

      In my own code base I referenced, its actually reporting on metrics for some traffic. So instead of me needing to later loop over the array, and do something like
      # isnt actual code but displays idea my $f = $array[$i] || 0; $sql .= '"$f", ';
      I can now simply do
      $sql .= join('", "', @array) . '");';
      And I know my values are sane.

      I guess in the case of the OP this isnt a factor, but in my case I initialize to a number, because all my values are numbers, and I think it makes it slightly easier at run time on perl. If we simply created place holders in the array Im not sure what data type perl would use behind the scenes, or rather how much space would be allocated to each element, nor how much work Perl would have to do behind the scenes to go from undef -> val -> bigger val. I am also dealing with what I consider monster data sets. I process about 22Gb of data, then another 15Gb or so, and correlate info. If I can drop a few tests from loops, or make my run time a little quicker, then I do.

      Or I could just be lazy and didnt want to add another test inside of a loop later, when I could deal with it up front and just go :)

      Update: Heh, I think I miss interpreted the question asked so let me add this.

      I guess it honestly doesn't matter how you test. Looking at the requirements where I assume the info will always be digit.string, then using the value as a boolean is 'OK' to determine what we are gonna do. If the value could be zero then I would have used defined like you suggested.

      MMMMM... Chocolaty Perl Goodness.....

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://261854]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found