3 Replies - 710 Views - Last Post: 05 November 2009 - 06:33 PM

#1 winracer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 02-March 09

perl array If help

Posted 05 November 2009 - 06:45 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 the data. but there are rowsof data that have the same thing (copy). What I am wating to do if the row is the same don't join both rows. only one. and if data in one list and not in other then am am going to do something...



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>~;




This post has been edited by winracer: 05 November 2009 - 06:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: perl array If help

#2 tivrfoa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 98
  • Joined: 25-January 09

Re: perl array If help

Posted 05 November 2009 - 07:10 AM

post an indented and working code at least.
Was This Post Helpful? 0
  • +
  • -

#3 winracer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 02-March 09

Re: perl array If help

Posted 05 November 2009 - 07:24 AM

View Posttivrfoa, on 5 Nov, 2009 - 06:10 AM, said:

post an indented and working code at least.



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 
}





Was This Post Helpful? 0
  • +
  • -

#4 winracer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 02-March 09

Re: perl array If help

Posted 05 November 2009 - 06:33 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);





Was This Post Helpful? 0
  • +
  • -

Page 1 of 1