Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: (bbfu) Re: Hmmm... HoA help ...

by talwyn (Monk)
on Nov 29, 2002 at 06:59 UTC ( [id://216434]=note: print w/replies, xml ) Need Help??


in reply to (bbfu) Re: Hmmm... HoA help ...
in thread Hmmm... HoA help ...

However I want to delete ONLY those records that are singletons. I want to generate a list of tapes having more than one record. The problem is that every tape ... even ones I know have duplicate records are being treated as singletons.

Replies are listed 'Best First'.
(bbfu) (oops) Re2: (bbfu) Re: Hmmm... HoA help ...
by bbfu (Curate) on Nov 29, 2002 at 07:08 UTC

    Oh, heh. Delete non-duplicates. Right, sorry. :)

    Anyway, what you have posted works fine (which is what I thought initially, before I got confused about which you were wanting to remove).

    [johnsca@CORY tmp]$ cat tst.pl #!/usr/bin/perl my @Tapes = ( ['Label 1', 'p', 'l', 'P'], ['Label 1', 'p', 'l', 'P'], # Duplicate ['Label 2', 'p', 'l', 'P'], ['Label 3', 'p', 'l', 'P'], ); hash_tapes(@Tapes); sub hash_tapes (@) { my @tapes = @_; my %Tape_Cat =(); foreach my $tape (@tapes) { my ( $label, $protection, $location, $pool ) = @{$tape}; push @{ $Tape_Cat{$label} }, [ $protection, $location, $pool ] +; } # Get rid of non-duplicates my $count = 0; foreach my $tape (@tapes) { my ($label,$protection,$location,$pool) = @{$tape}; if ( @{$Tape_Cat{ $label}} < 2 ) { print "Deleting ". $label. "Because it has ".@{$Tape_Cat{ $label }}. " record(s)\n"; $count++; delete $Tape_Cat{$label} } } my @keys = keys %Tape_Cat; print "Deleted $count keys, keys: @keys\n"; return \%Tape_Cat; } [johnsca@CORY tmp]$ ./tst.pl Deleting Label 2Because it has 1 record(s) Deleting Label 3Because it has 1 record(s) Deleted 2 keys, keys: Label 1

    Correct, no? You should probably double-check the data that this function is actually getting. Use Data::Dumper at the begining of the function, or the built-in perl debugger. Update: If the data is coming from an outside source, check for strange things like leading or trailing newlines or linebreaks, or non-printable characters, or such.

    Good luck. :)

    bbfu
    Black flowers blossum
    Fearless on my breath

      Thanks! It looks like the problem was the data .. not the code! That's a first :) Anyway I appreciate the sounding board you provided. --Talwyn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found