Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: extract uniques and sort

by scottstef (Curate)
on Jun 15, 2001 at 04:12 UTC ( #88679=note: print w/replies, xml ) Need Help??


in reply to extract uniques and sort

ybiC,

I think you want to look at an if exists statement for your hash. It will look at a primary key and return true if it exists. As for the sort, you are on your own for that, I haven't needed to learn that function yet. %^) Update: Posted this code per ybiC's request. Here is some code I pieced together that compared two files into a hash.


#!/usr/bin/perl -w use strict; use diagnostics; my $file1 = "/home/scott/PerlHacks/post.office/file1"; my $file2 = "/home/scott/PerlHacks/post.office/file2"; my $hashOfLists; my %hashOfLists; open (FILE1, "$file1") or die "Could not open $file1 $!"; open (FILE2, "$file2") or die "Could not open $file2 $!"; ###################################################### #####This block reads all of the items of the file into #####a hash with the item being the key. Since I ##### was comparing 2 files each key received a #####value of 1 if it was in the first file, 2 if #####it was in both , and 3 if it was only in file 2 ###################################################### foreach (<FILE1>) { chomp $_; $hashOfLists{$_} =1; } foreach (<FILE2>) { chomp $_; ###################################################### #####if exists checks the hash to see if $_ exists. ##### If true $_'s value becomes 2, #####if false, $_ gets a value of three ############################## +######################## if (exists $hashOfLists{$_}) { ($hashOfLists{$_}) =2; } else { ($hashOfLists{$_})=3; } } close FILE1 or die "Could not close $file1 $!"; close FILE2 or die "Could not close $file2 $!";

"The social dynamics of the net are a direct consequence of the fact that nobody has yet developed a Remote Strangulation Protocol." -- Larry Wall



Edited by planetscape - added code tags

( keep:0 edit:18 reap:0 )

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://88679]
help
Chatterbox?
[shmem]: Corion: very clear case of missing sequence number
[Corion]: shmem: Yeah. I guess they have a sequence number but distribute the events across threads or machines or whatever.
[karlgoethebier]: choroba: another chapter of "Learning English At The Monastry"?
[shmem]: Corion, well then... next issue, sequence number not a shared resource :P
[Discipulus]: shmem i'm searching it.. but failing i was sure was in Re: Let's Make PerlMonks Great Again! -- suggestions and dreams
erix recommends Vanished Kingdoms
[Corion]: shmem: Yeah, something like that. Not that that would be a solved issue. Simply process all events that come in from a single interface sequentially. Ah well.
[Discipulus]: their

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2017-05-23 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?