For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2005 > Calling Diff on Arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calling Diff on Arrays
Robert Love

2005-08-28, 9:56 pm

I can read in two text files into arrays of lines, manipulate and sort
the contents. I then want to pass the arrays to diff and manipulate and
print the results of the diff. I have it working using temporary files
that I write the arrays into. My question is how do I do this directly,
without writing to temporary files?

What I have now looks like:

@results = `diff tmp1 tmp2`;

I have read FAQ 4.xx and that is NOT what I want. All tips & advice
appreciated.
Kevin Michael Vail

2005-08-28, 9:56 pm

In article <20050828202318480-0500@news.airmail.net>,
Robert Love <rblove@airmail.net> wrote:

> I can read in two text files into arrays of lines, manipulate and sort
> the contents. I then want to pass the arrays to diff and manipulate and
> print the results of the diff. I have it working using temporary files
> that I write the arrays into. My question is how do I do this directly,
> without writing to temporary files?
>
> What I have now looks like:
>
> @results = `diff tmp1 tmp2`;
>
> I have read FAQ 4.xx and that is NOT what I want. All tips & advice
> appreciated.


Algorithm::Diff

## assume FILE1 and FILE2 are opened to two text files

my @array1 = <FILE1>;
my @array2 = <FILE2>;

my $diff = Algorithm::Diff::new(\@array1, \@array2);

From there you can do almost anything, see the perldoc for details.
--
Kevin Michael Vail | a billion stars go spinning through the night,
kevin@vaildc.net | blazing high above your head.
. . . . . . . . . | But _in_ you is the presence that
. . . . . . . . | will be, when all the stars are dead.
. . . . . . . . . | (Rainer Maria Rilke)
rlove@mac.com

2005-08-29, 6:59 pm


Kevin Michael Vail wrote:
> In article <20050828202318480-0500@news.airmail.net>,
> Robert Love <rblove@airmail.net> wrote:
>
>
> Algorithm::Diff


Thanks but that doesn't seem to be part of my perl installation. I'm
working on a set of RH 7.2 linux machines that I don't adminster. I
can't talk the pointy hair bosses into upgrading either.

Just installing this local to my machine wouldn't help others I want to
run the tool.

So, I repeat the question. Is there a way to call diff directly
without writing the contents of my @rrays to files first?

A. Sinan Unur

2005-08-29, 6:59 pm

rlove@mac.com wrote in
news:1125339048.999212.204000@g44g2000cwa.googlegroups.com:

>
> Kevin Michael Vail wrote:
>
> Thanks but that doesn't seem to be part of my perl installation. I'm
> working on a set of RH 7.2 linux machines that I don't adminster. I
> can't talk the pointy hair bosses into upgrading either.
>
> Just installing this local to my machine wouldn't help others I want
> to run the tool.


perldoc -q lib

If the machines are identical, all you have to do is to install in a
subdirectory to which you have access, and then add the appropriate

use lib 'path to my lib';

statement to your script, then copy to other machines.

> So, I repeat the question. Is there a way to call diff directly
> without writing the contents of my @rrays to files first?


It probably depends on the diff program you are using. Reading man diff
on my Cygwin system points out a possible solution:

FILES are `FILE1 FILE2' or `DIR1 DIR2' or `DIR FILE...' or
`FILE... DIR'. If --from-file or --to-file is given, there are no
restrictions on FILES. If a FILE is `-', read standard input. Exit
status is 0 if inputs are the same, 1 if different, 2 if trouble.

Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
James E Keenan

2005-08-30, 7:56 am

Robert Love wrote:
> I can read in two text files into arrays of lines, manipulate and sort
> the contents. I then want to pass the arrays to diff and manipulate and
> print the results of the diff. I have it working using temporary files
> that I write the arrays into. My question is how do I do this directly,
> without writing to temporary files?
>


You state that you "manipulate" the contents in the temp files and then
"manipulate" the arrays. My hunch is that the solution depends on what
you mean by "manipulate" in each context. Algorithm::Diff, as others
have suggested, may be of use here. For determining the differences
between two or more sets, my CPAN distribution List::Compare may be useful.

jimk
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com