Just can't make this array comparison work

B

bjjnova

This is programming 101, but darned if I don't keep making arrors that
flummox me (my excuse: I am brand new to Perl). I have two arrays, A
and B. A is vastly larger than B (~30000 elements to B's ~1000) but
some of the elements in A are identical to those in B (~1000 of them
actually). I want to loop thorugh A and throw out everything in the
array so that in the end A=B. The two arrays are sorted. The valuse
being compared are of the form aaaaaaa so that at some point any given
value out of A that is being checked against those in B will be <= to
the remainder of the values (not yet checked) in B.
 
U

usenet

bjjnova said:
I want to loop thorugh A and throw out everything in the
array so that in the end A=B.

Why loop at all? Why not just do this:

@A = @B;

If that doesn't do what you want then you need to explain the question
more clearly.
 
X

xhoster

bjjnova said:
This is programming 101, but darned if I don't keep making arrors that
flummox me (my excuse: I am brand new to Perl). I have two arrays, A
and B. A is vastly larger than B (~30000 elements to B's ~1000) but
some of the elements in A are identical to those in B (~1000 of them
actually). I want to loop thorugh A and throw out everything in the
array so that in the end A=B.

I doubt that is really what you want to do, but if it is then wouldn't
"@A=@B" do what you want?

Whatever it is that you actually do want to do, I'm betting you should do
it with hashes, rather than arrays.

Xho
 
B

bjjnova

A and B are arrays of data records. The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).
 
U

usenet

bjjnova said:
A and B are arrays of data records. The unique identifiers

Anytime the phrase "unique identifiers" is mentioned, it signals that a
hash is probably the appropriate data structure.

But, anyway, it's still unclear why @A=@B wouldn't meet your needs.
Maybe if you posted (or ginned up) a small representative sample of
your arrays so we can better understand what you're trying to do?
 
D

Dr.Ruud

bjjnova schreef:
A and B are arrays of data records. The unique identifiers for some
of A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

You forgot to quote from the message that you react on. Since
your User-Agent header field contains G2, read this first:
http://groups.google.co.uk/support/bin/answer.py?answer=14213
http://www.safalra.com/special/googlegroupsreply/


About arrays, hashes:

perldoc -q arrays\|hashes
 
X

xhoster

bjjnova said:
A and B are arrays of data records.

AoA or AoH?
The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

# Make a hash which contains the identifiers that @B contains.
my %B_hash;
@B_hash{map $_->[$whatever_the_id_column_is], @B}=();

# Keep only things in A that have identifiers in %B_hash.
@A = grep exists $B_hash{$_->[$whatever_the_id_column_is]}, @A;

Xho
 
B

bjjnova

this looks promising; tho' I have no idea what AoA and AoH mean.

bjjnova said:
A and B are arrays of data records.

AoA or AoH?
The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

# Make a hash which contains the identifiers that @B contains.
my %B_hash;
@B_hash{map $_->[$whatever_the_id_column_is], @B}=();

# Keep only things in A that have identifiers in %B_hash.
@A = grep exists $B_hash{$_->[$whatever_the_id_column_is]}, @A;

Xho
 
I

it_says_BALLS_on_your forehead

bjjnova said:
A and B are arrays of data records.

AoA or AoH?
The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

# Make a hash which contains the identifiers that @B contains.
my %B_hash;
@B_hash{map $_->[$whatever_the_id_column_is], @B}=();

# Keep only things in A that have identifiers in %B_hash.
@A = grep exists $B_hash{$_->[$whatever_the_id_column_is]}, @A;
bjjnova wrote:
this looks promising; tho' I have no idea what AoA and AoH mean.

AoA - Array of Arrays
AoH - Array of Hashes
 
B

bjjnova

i think I do need hashes (I didn't know they existed before this series
of posts);

if A is an array of everyone's name and address and phone number in NY
and B is an array of everyone's name in NYC, and I want everyone's
address and phone number in NYC, won't setting A=B simply destroy
everything I'm trying to grab? That is, it will make a replica of B,
so I will still have everyone's name in NYC but none of their addresses
or phone #'s.
 
P

Paul Lalli

bjjnova said:
This is programming 101, but darned if I don't keep making arrors that
flummox me (my excuse: I am brand new to Perl). I have two arrays, A
and B. A is vastly larger than B (~30000 elements to B's ~1000) but
some of the elements in A are identical to those in B (~1000 of them
actually). I want to loop thorugh A and throw out everything in the
array so that in the end A=B. The two arrays are sorted. The valuse
being compared are of the form aaaaaaa so that at some point any given
value out of A that is being checked against those in B will be <= to
the remainder of the values (not yet checked) in B.

I'm really confused by your stated goal. If you want to make @A
contain the same elements of @B, why not just make that assignment?

@A = @B;

If your actual goal is more complicated than that, 1) Read the Posting
Guidelines for this group, 2) Read `perldoc -q "difference of two
arrays"` and see if that helps you at all, 3) Post a sample
(short-but-complete) here that demonstrates the problems you're having.

Paul Lalli
 
I

it_says_BALLS_on_your forehead

bjjnova said:
A and B are arrays of data records. The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

use hashes.
 
B

bjjnova

A and B are arrays with data records. The unique identifiers for some
of A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).
 
B

bjjnova

this looks promising; tho' I have no idea what AoA and AoH mean.

bjjnova said:
A and B are arrays of data records.

AoA or AoH?
The unique identifiers for some of
A's records are the same as those in B. I need to capture data
associated with those records in A (the data is not in B), but B is
acting as a check; it contains the list of identifiers in which I am
interested (but not all the data).

# Make a hash which contains the identifiers that @B contains.
my %B_hash;
@B_hash{map $_->[$whatever_the_id_column_is], @B}=();

# Keep only things in A that have identifiers in %B_hash.
@A = grep exists $B_hash{$_->[$whatever_the_id_column_is]}, @A;

Xho
 
T

Tad McClellan

bjjnova said:
i think I do need hashes (I didn't know they existed before this series
of posts);


You should not start Perl programming until you have
read the main docs for the language, including:

perldoc perldata

which describes Perl's data types.
 
S

Scott Bryce

bjjnova said:
if A is an array of everyone's name and address and phone number in NY
and B is an array of everyone's name in NYC, and I want everyone's
address and phone number in NYC, won't setting A=B simply destroy
everything I'm trying to grab?

Yes, but since you haven't described how the data is organized, we have
no way of knowing what is in the A array and what is in the B array.
Your original post suggested that all of the data in the B array was
also contained in the A array (or vice versa?). In other words, it
seemed as though the records in the B array were duplicated in their
entirety in the A array.

I am guessing that the A array and B array both contain records in a tab
(or otherwise) delimited format.

But I am just guessing.

It would be helpful if you could give us either a representative sample
of the data, or a better description of the data.
 
B

bjjnova

i think your earlier post will do it; thank you

I doubt that is really what you want to do, but if it is then wouldn't
"@A=@B" do what you want?

Whatever it is that you actually do want to do, I'm betting you should do
it with hashes, rather than arrays.

Xho
 
S

Scott Bryce

bjjnova said:
A and B are arrays with data records. The unique identifiers for some
of A's records are the same as those in B.

If the records are stored in a relational database, this may be a SQL
question, rather than a Perl question. Where is the data coming from?
 

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

Forum statistics

Threads
474,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top