Compare 2 arrays consisting of hashes

F

Frodo Larik

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
 
J

Jürgen Exner

Frodo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top