Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Flat File Comment

by Kenosis (Priest)
on Dec 09, 2013 at 23:19 UTC ( #1066351=note: print w/ replies, xml ) Need Help??


in reply to Flat File Comment

You don't mention the size of your files. If not overwhelming, consider creating a hash of hashes (HoH) from each file's contents. When you've used one of the Makes (for example), remove it by deleting it from the hash. For example:

use strict; use warnings; my %hash; while (<DATA>) { chomp; $hash{Makes}{$_} = undef; } print 'Before:', "\n"; print "$_\n" for keys %{ $hash{Makes} }; my $item = getCategoryItem( 'Makes', \%hash ); print "\n", 'Selected:', "\n", $item, "\n\n"; print 'After:', "\n"; print "$_\n" for keys %{ $hash{Makes} }; sub getCategoryItem { my ( $category, $hashRef ) = @_; my @items = keys %{ $hashRef->{$category} } or return undef; # undef is returned if no items my $item = $items[ rand $#items + 1 ]; delete $hashRef->{$category}{$item}; return $item; } __DATA__ Ford Chevrolet KIA

Sample output:

Before: KIA Chevrolet Ford Selected: KIA After: Chevrolet Ford

Hope this helps!


Comment on Re: Flat File Comment
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2015-07-02 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls