Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1066351]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-05-26 23:52 GMT
Find Nodes?
    Voting Booth?