Click to See Complete Forum and Search --> : [RESOLVED] perl array sort if help


winracer
11-05-2009, 06:12 AM
My question is I have two arrays that have data and some of the data is the same so I do not want to join the data if the same.

I am not sure how you would code this. here is what I have started and joins and sorts teh data. but there are rolls the same.

I hope I have explained what I need help with.





my %numbers = ();
my $output = "";

open (DATABASE, "$data_file_path_1") || &file_open_error
("$data_file_path", "Display Frontpage", __FILE__, __LINE__);

while (<DATABASE>)
{
$line = $_;
chop $line;
@fields = split (/\|/, $line);
if ($fields[$index_of_status] eq "ok") {
$category = $fields[$index_of_category];
$numbers{$category}++;
}
$numbers1++;
push @goodads2, $line;
}
close (DATABASE);
print qq~
<font size=5><b>$session_username</b> you have <b><font color="#ff0000">$numbers1</font></b> .
</font>
<p>~



open (HITS_FILE, "$location_of_hits_file");

while ($line1 = <HITS_FILE>)
{
chop($line1);
@fields = split(/\|/, $line1);



if ($fields[1] eq "$session_username")
{

push @goodads, $line1;
push @goodads1, $fields[0];

#print qq~ $line1 ~;


}
}

}
else
{


}

close (HITS_FILE);





open(DATAFILE, "$data_file_path_1") ||
&file_open_error("$data_file_path_1",
"Read Database",__FILE__,__LINE__);
while(($line = <DATAFILE>))
{
chop($line);


my @allgoodads = (@goodads2, @goodads); # two sets of data
@sorted = sort {$a <=> $b} @allgoodads; # sort numerically

foreach $line (@sorted)

{



@fields = split(/\|/, $line);

################other code here

last;

} # End of while datafile has data
close(DATAFILE);
print qq~
</table><p>~;

winracer
11-05-2009, 06:50 PM
sorry I guess what I need help on is this part of the code above...




my @allgoodads = (@goodads2, @goodads); # two sets of data
@sorted = sort {$a <=> $b} @allgoodads; # sort numerically

foreach $line (@sorted)




I have tried different thing. what I need is something like this.




if (@goodads2 row is = @goodads)
###do this
my @allgoodads = (@goodads2, @goodads); # two sets of data
@sorted = sort {$a <=> $b} @allgoodads; # sort numerically

foreach $line (@sorted)


else {
#do this row not in both list
push (@allgoodnot, @allgoodads ) #row that is not in both list
}

winracer
11-05-2009, 07:34 PM
after hours of searching I found what I needed here. if anyone else needs this information

http://www.perlmonks.org/?node_id=2461


my @simpsons=("homer","bart","marge","maggie","lisa");
my @females=("lisa","marge","maggie","maude");

my %simpsons=map{$_ =>1} @simpsons;
my %females=map{$_=>1} @females;

# the intersection of @females and @simpsons:
my @female_simpsons = grep( $simpsons{$_}, @females );

# proof it works
print "Female Simpson:\t$_\n" foreach (@female_simpsons);

# the difference of @females and @simpsons

my @male_simpsons=grep(!defined $females{$_}, @simpsons);

# proof it works
print "Male Simpson:\t$_\n" foreach (@male_simpsons);
my %union = ();

# the union of @females and @simpsons
foreach(@females,@simpsons){
$union{$_}=1;
}

my @union2 = keys %union;

# or just do this
# my @union = (@females, @simpsons);