JimJx has asked for the wisdom of the Perl Monks concerning the following question:

This is my hopefully last question.... I am using the following code to read a flat file:
open(FILE,"< tiles.txt") or die "error opening tiles.txt $!"; tie @text, 'Tie::File', 'tiles.txt' or die $!; @text = shuffle(@text); while (<FILE>) { ($File[$i], $Name[$i], $Keywords[$i]) = split(/:/); chomp ($Keywords[$i]); $i++; } close(FILE);
The file has 48 entries in it, and I am only using 24 of these entries at a time. The file contains pairs of very similar entries. For example, $file in one entry is algiz and in another entry it is algiz-r. How do I make sure that only the first of these encountered is put into the list? IOW, if I have already added algiz-r somewhere I do not want algiz added. Hope I didn't confuse everyone too much. :-)

Replies are listed 'Best First'.
Re: Array question
by chromatic (Archbishop) on Jun 16, 2003 at 03:14 UTC

    Keep a hash of $File entries you've seen. You'll have to mangle them to make sure that algiz and algiz-r use the same key, but that's pretty easy.

    my %seen; while (<FILE>) { chomp; my ($file, $name, $keyword) = split( /:/, $_, 3 ); (my $filekey = $file) =~ s/-r$//; next if $seen{$filekey}++; ($File[$i], $Name[$i], $Keywords[$i]) = ( $file, $name, $keyword ) +; $i++; }
Re: Array question
by Zaxo (Archbishop) on Jun 16, 2003 at 03:24 UTC

    You need to define what kind and how much similarity is significant. Does 'axolotl' disqualify 'algiz'?

    Your example suggests that $_ = [split /-/]; may be helpful, in conjunction with (as always with uniqueness problems) a hash, $hash{$_->[0]} = $data if not exists $hash{$_->[0]};.

    Why do you tie the file to @text and then go ahead and work with the redundant *FILE? On many systems, opening *FILE first guarantees that *FILE will not see the results of the shuffle.

    This is my hopefully last question....

    I hope not :-)

    After Compline,
    Zaxo

Re: Array question
by BrowserUk (Patriarch) on Jun 16, 2003 at 12:48 UTC

    Maybe you got this this from Zaxo's answer, in which case, this will be a redundant post, but I think Zaxo was a little gentle in his statement.

    If your going to use Tie::File, don't open and <read> it yourself as well.

    Imagine how confused your dog will get if you start barking at the postman yourself:)

    Either tie the file to an the array, and use the array

    Or

    open the file and <read> it.

    but don't do both.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller