On Jul 9, 6:02 am, [EMAIL PROTECTED] (Brajesh Agrawal) wrote:
> My input file (string) has strings as below
>
> asfd1-asfd2-asfd3
> asfd2-asfd3-asfd1
> asfd1-asfd4-asfd3
>
> (Create a file named string with above lines as a testcase for program)

No.  You are the one asking for help.  You should be the one making
the effort to make it as easy as possible for us to help you.
Therefore, type your data directly into your code, using the __DATA__
filehandle.  If you don't know about this special filehandle, read
about it in
perldoc perldata

> each line represents a loop, for example for line-1 asfd1 to asfd2 to asfd3
> and back to asfd1,
> In that sense line-1 and line-2 represent the same loop.
> I was writing a program in perl to detect same loops in the file.
> Also i am using array of array for this purpose.
> My code is below

There is just no way I'm going to try to pour through that horrible
mess of non-strict, non-commented, single-char variable code.  I'm
sorry, but it's rude of you to ask anyone to do so.  Clean your code,
make it strict-compliant, use real variable names, comment it.  Pare
it down to the shortest complete example that still demonstrates the
problem you're having.

In the meantime, this short-but-complete script seems to do what you
want (though I used spaces instead of dashes):

#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/firstidx/;

#read all sets into an array of array refs
my @sets = map { [ split ] } <DATA>;

#Loop through each set
for my $i (0..$#sets) {
    #Only check subsequent sets, don't recheck sets.
    for my $j ($i+1..$#sets) {
        print "Line $i (@{$sets[$i]}) is the same as Line $j
(@{$sets[$j]})\n"
            if equal_sets($sets[$i], $sets[$j]);
    }
}

#Compare two sets for equivalence.
sub equal_sets {
    my ($set1, $set2) = @_;
    #start first iterator at 0.
    my $idx1 = 0;
    #start second iterator at first match
    my $idx2 = firstidx { $_ eq $set1->[0] } @{$set2};
    #if there is no first match, sets aren't equivalent.
    return unless defined $idx2;
    for ([EMAIL PROTECTED]) {
        #if any position doesn't match, sets aren't equivalent
        #also incrementing each iterator in this test
        return unless $set1->[$idx1++] eq $set2->[$idx2++];
        #Loop back around to start if needed
        $_ = $_ % @{$set1} for $idx1, $idx2;
    }
    #If we get here, sets were equivalent
    return 1;
}

__DATA__
a b c d
a d c b
b c d a
d a b c
a c b d
b a d c

Output:
Line 0 (a b c d) is the same as Line 2 (b c d a)
Line 0 (a b c d) is the same as Line 3 (d a b c)
Line 1 (a d c b) is the same as Line 5 (b a d c)
Line 2 (b c d a) is the same as Line 3 (d a b c)

Note that this program assumes each individual set is unique.  That
is, "a b b c d" is not a valid set.

Hope this helps,
Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to