Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Compare 2 arrays consisting of hashes

Reply
Thread Tools

Compare 2 arrays consisting of hashes

 
 
Frodo Larik
Guest
Posts: n/a
 
      05-30-2004
Hello,

Say I have two arrays, like this


my @master = ( { href => 'http://server1', text => 'This is Server 1'},
{ href => 'http://server2', text => 'This is Server 2'} );


my @slave = ( { href => 'http://server1', text => 'This is Server 1'},
{ href => 'http://server3', text => 'This is Server 3'} );


And I want to check for every instance in @slave if the data is in
@master, if not add data to @master.

The arrays can become very big, so I'm looking for a way to efficiently
do this. So without a lot of for or foreach loops.

Is it possible?

Sincerely,

Frodo Larik
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      05-30-2004
Frodo Larik wrote:
> Hello,
>
> Say I have two arrays, like this
>
>
> my @master = ( { href => 'http://server1', text => 'This is Server
> 1'}, { href => 'http://server2', text => 'This is
> Server 2'} );


The keys "href" and "text" don't add any information.
Convert this into a single-level hash (linear time):

%mymaster = ('http://server1' => 'This is Server',
'http://server2' => 'This is Server 2');

> my @slave = ( { href => 'http://server1', text => 'This is Server 1'},
> { href => 'http://server3', text => 'This is Server
> 3'} );
>
>
> And I want to check for every instance in @slave if the data is in
> @master, if not add data to @master.
> The arrays can become very big, so I'm looking for a way to
> efficiently do this. So without a lot of for or foreach loops.


Then you can write a simple linear loop through @slave where you can test
each element directly if a key in %myhash exists().

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a webcontrol consisting of server controls tolisss ASP .Net 1 11-12-2005 06:14 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
after transfer of data from MS-outlook(mail ids) to application,mail ids are consisting of strange characters vamsikrishna_b@coolgoose.com Python 2 06-21-2005 12:41 PM
sorting a LinkedList consisting of two items learningjava Java 6 12-15-2003 03:41 PM
String consisting of spaces Andrew Smith Java 25 10-19-2003 10:04 PM



Advertisments