Compare 2 arrays consisting of hashes

Discussion in 'Perl' started by Frodo Larik, May 30, 2004.

  1. Frodo Larik

    Frodo Larik Guest

    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
    Frodo Larik, May 30, 2004
    #1
    1. Advertising

  2. 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
    Jürgen Exner, May 30, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tolisss
    Replies:
    1
    Views:
    356
    =?Utf-8?B?dGlta2xpbmc=?=
    Nov 12, 2005
  2. Andrew Smith

    String consisting of spaces

    Andrew Smith, Oct 17, 2003, in forum: Java
    Replies:
    25
    Views:
    733
    Raymond DeCampo
    Oct 19, 2003
  3. learningjava
    Replies:
    6
    Views:
    4,337
    learningjava
    Dec 15, 2003
  4. Replies:
    2
    Views:
    694
    Beat Bolli
    Jun 21, 2005
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    201
Loading...

Share This Page