School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 306,994 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,916 people online right now. Registration is fast and FREE... Join Now!




perl array If help

 

perl array If help

winracer

5 Nov, 2009 - 05:45 AM
Post #1

D.I.C Head
**

Joined: 2 Mar, 2009
Posts: 103


My Contributions
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.



CODE


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: 5 Nov, 2009 - 05:48 AM

User is offlineProfile CardPM
+Quote Post


tivrfoa

RE: Perl Array If Help

5 Nov, 2009 - 06:10 AM
Post #2

D.I.C Head
Group Icon

Joined: 25 Jan, 2009
Posts: 92



Thanked: 6 times
Dream Kudos: 125
My Contributions
post an indented and working code at least.
User is offlineProfile CardPM
+Quote Post

winracer

RE: Perl Array If Help

5 Nov, 2009 - 06:24 AM
Post #3

D.I.C Head
**

Joined: 2 Mar, 2009
Posts: 103


My Contributions
QUOTE(tivrfoa @ 5 Nov, 2009 - 06:10 AM) *

post an indented and working code at least.



sorry I guess what I need help on is this part of the code above...

CODE



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.


CODE


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
}






User is offlineProfile CardPM
+Quote Post

winracer

RE: Perl Array If Help

5 Nov, 2009 - 05:33 PM
Post #4

D.I.C Head
**

Joined: 2 Mar, 2009
Posts: 103


My Contributions
after hours of searching I found what I needed here. if anyone else needs this information

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

CODE

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




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:54AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month