Ken Gillett <[EMAIL PROTECTED]> wrote:

: I have thousands ( <50,000 ) of numerically named files that I
: need to store in 2 different location so that the ratio of
: numbers of files in each location is maintained at a preset
: ratio. The files will be repeatedly created and I want to ensure
: that each time this is done, the same named file is placed in
: the same location it was last time, therefore a simple
: alternating process or one that looks at the existing numbers
: of files would not be suitable, since at each generation of the
: files they would be unlikely to end up in the same place.
: 
: So I really want something that examines the file name and
: makes a decision based on that, so that every time it sees the
: same name, it will be placed in the same location - unless
: there's another way to enforce the same rule? 
: 
: The file names are numbers between 10 and 10,000,000, with no
: grouping/order to the naming that would skew the result -
: assume an even distribution.
: 
: Initially the storage requirements are 2:1, so simply dividing
: the name by 3 would (I think) work, i.e. if the name is exactly
: divisible by 3 or not could determine in which location to place
: it. But if I need to change that to e.g. 3:2 it's not so simple
: and the ratio needs to be flexible so that it can be easily set
: and/or changed for different setups.

    A ratio is a fraction and can be turned into a floating point
fairly easily. We can match this ratio to the file name we
process. We would require the ratio and the maximum number of
files.

my $location = 
        $filename < location_threshold( '3/2', 10_000_000 )
                ? 'larger location' : 'smaller location';

sub location_threshold {
        my( $ratio, $max_files ) = @_;
        my( $ratio_top, $ratio_bottom ) = split m|/|, $ratio;
        return
                int(
                        $max_files * $ratio_top / ( $ratio_top +
$ratio_bottom )
                )
                + 0.5;
}

        We would end up with a value that directs the location by
comparing the file name to the threshold. Testing lower than the
threshold will yield the larger location as long as the ratio was
expressed as larger value over smaller value.

    There is a chance that one or two files will be placed in the
wrong location because this algorithm is not perfect, but no file
name would end up in both locations.

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to