473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FOREACH Loop

I hope i can articulate this question properly. i have been trying to figure this out for over a week.

I am comparing the contents of two files, but the comparison is done inside of a foreach loop (the code that i have so far is below). At the instance the files are different, i want to capture one of the files and use it later on in the script. But it keeps iterating through the loop and is only capturing the last one in the array. Is there a way to do this? I tried to use the 'else' statement after the compare, so that if the compare is false, i could save the file i wanted in another variable and use it later, but for some reason it doesn't like that.

Here is example of what the loop does:
$copiedfile will compare to each of the $file that are saved in the @result_files array.
$copiedfile will compare to $file which is @result_files[0]
and also
$copiedfile will compare to $file which is @result_files[1]


Expand|Select|Wrap|Line Numbers
  1. $known_messages_dir = "C:\\Test Data\\Results\\";
  2.  
  3. $copied_results_dir = "C:\\Test Data\\Copied_Results\\copied_results.txt";
  4.  
  5. for ( my $i = 0; $i <= $#{$testcase{output_messages}}; $i++ )
  6. {
  7.  
  8.       foreach $file (@result_files)
  9.       {
  10.  
  11. my $file1 =  $known_messages_dir.$testcase{output_messages}[$i];
  12.  
  13. my $copiedfile = $copied_results_dir;
  14.  
  15.          $matches_delta = $matches;
  16.  
  17.         if (compare( "$copiedfile","$file" ) == 0)
  18.             {
  19.                   print REPORT_FILE "\nPASS - Results matched for $testcase{output_messages}[$i]\n";
  20.                   matches++;
  21.                   last;
  22.             }
  23.  
  24.        } #End of FOREACH loop
  25.  
  26.  
  27. if ($matches_delta == $matches )
  28. {
  29. .
  30. .
  31. Within this IF statement, execute the code using the $file at the instance that $copiedfile and $file are different (from the above ‘compare’ line)
  32. .
  33. .
  34. }
  35.  
  36. } #End of FOR loop

Thanks,
Terra
Dec 3 '07 #1
1 1838
KevinADC
4,059 Expert 2GB
You need to put your conditional inside the foreach loop.
Dec 3 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.