LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2011, 08:01 PM   #1
ansrewdps
Member
 
Registered: Dec 2010
Posts: 56

Rep: Reputation: 0
Comparing 2 large arrays


Can anone help me in writing some code that compares 2 large arrays. I know we can do that using 2 loops incrementing each element in the outer loop and comparing that to the second array.
But if that's an large array that would be computationally intensive...
So, I'm trying to develop a code that compares two arrays and print equal if two arrays(large) are equal(order of the elements doesn't matter).

any code in C/Perl??

Thanks
Andrew
 
Old 08-05-2011, 08:09 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
As of Perl 5.10, you can simply do:
Code:
if(@foo ~~ @bar)
That's the smart match operator. Details on what it's testing in perlsyn(1).

---

edit: On second thought, given that you said "order of the elements doesn't matter", you may need to sort first and/or try a different approach. (You'll see what I mean when you read perlsyn(1).)

Last edited by anomie; 08-05-2011 at 08:12 PM.
 
Old 08-05-2011, 08:23 PM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
In an unordered array, the computation is always going to be of order n*m for arrays of size n & m.
 
Old 08-05-2011, 08:34 PM   #4
ansrewdps
Member
 
Registered: Dec 2010
Posts: 56

Original Poster
Rep: Reputation: 0
Thanks for the quick reply....I tried writing a code but ended up getting errors...can you help me with the code

#! /usr/bin/perl
use warnings

#initializing queries
@qw=qw ! perl java python Ms.Net !;
@pw= qw ! Ms.Net perl java python !;

if(@qw ~~ @pw)
{
print 'arrays equal';
}
else
{
print 'arrays not equal';
}


#./demo.pl
Unknown warnings category 'perl' at ./demo.pl line 5
BEGIN failed--compilation aborted at ./demo.pl line 5.
 
Old 08-05-2011, 08:44 PM   #5
ansrewdps
Member
 
Registered: Dec 2010
Posts: 56

Original Poster
Rep: Reputation: 0
I got that working but it returns array not equal cause the order of the elements are different...As said,elements are tested not the order which they are in the array...

any idea on how to make it work? and can you give me walkthrough on how exactly this work?

will this work as the same way as implementing in 2 for loops incrementing inner loop first and then outer loop??

Thanks
 
Old 08-05-2011, 09:20 PM   #6
jcmlq
Member
 
Registered: Aug 2009
Posts: 32

Rep: Reputation: 19
Code:
#! /usr/bin/perl
use warnings;

#initializing queries
@qw=qw ! perl java python Ms.Net !;
@pw= qw ! Ms.Net perl java python !;

if(sort(@qw) ~~ sort(@pw))
{
  print 'arrays equal';
}
else
{
  print 'arrays not equal';
}
Since order does not matter, you need to sort the lists first. This is desirable in any case, because you would have to walk the entire compared list for each element otherwise.

Code:
# don't do this, it's wasteful

foreach $i (@pw)
{
  foreach $j (@qw)
  {
    if ($i == $j)
    {
      ...

Last edited by jcmlq; 08-05-2011 at 09:22 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing arrays in bash unihiekka Linux - Newbie 4 06-03-2010 06:08 AM
comparing files in 2 large dirs ufmale Linux - Software 8 12-17-2007 12:45 PM
Fortran Code with Large arrays statically allocated -> segmentation fault eiffel Linux - Kernel 2 07-19-2006 10:14 AM
C++ Segfault with Large Arrays guygriffiths Programming 3 05-15-2005 06:17 AM
Comparing Char arrays n_ick2000 Programming 5 02-25-2003 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration