foreach

Best way to check an array value with for statement

Hi, I'd like to check if there is a value on an array like this: function check_value_new ($list, $message) { foreach ($list as $current) { if ($current == $message) return true; } return false; } function check_value_old ($list, $message) { for ($i = 0; $i < count ($status_list); $i ++) { if ($status_li...

A better way than nesting foreach?

I've got a list of Radios that I'm trying to produce a more user-friendly version of. Behind the scenes all descriptions are stringIds and all part numbers are DB Ids. Right now, I've got this code: var z = (from w in wireless join s in allStrings on w.DescriptionId equals s.StringId ...

Can I use 'and' operator in xsl for-each?

Simply can I execute the following in xsl? <xsl:for-each select="trip/instance[.!=''] and trip/result[.!='']"> </xsl:for-each> Q: When I use select="" in for-each does it change the scope of my selector for the code I use inside for-each? ...

Silverlight 3: iteration order of in generic Dictionary using foreach KeyValuePair

In Silverlight3, when a generic Dictionary is iterated with foreach and a KeyValuePair, is the iteration guaranteed to visit the items in the dictionary in the order in which the items were added? That is, the iteration sequence has nothing to do with the key's datatype or its value? The documentation is less than explicit on this: This...

boost::trim each string in std::vector<std::string>

I'm currently stuck finding the correct syntax for trimming each string in a std::vector. I tried std::vector<std::string> v; std::for_each(v.begin(), v.end(), &boost::trim); which gave me the following error messages in MSVC7.1. error C2784: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : could not deduce template argument for 'T1' from ...

php sql code, foreach warning

Hi everyone, I have the following code, not written bymyself, but I am wondering if you could spot anything wrong with it? $query = "SELECT * from #__properties_type where published = 1 AND parent = ".$Category_id." OR parent = 0"; $db->setQuery( $query ); $types = $db->loadObjectList(); $nP = count($types)...

foreach(... in ...) or .ForEach(); that is the question

This is a question about coding for readability. I have an XDocument and a List<string> of the names of the elements that contain sensitive information that I need to mask (replace with underscores in this example). XDocument xDoc; List<string> propertiesToMask; This can be written in two ways, using traditional foreach loops, or usi...

What is the default scope of foreach loop in Perl?

In Perl, does using 'my' within a foreach loop have any effect? It seems that the index variable is always local whether or not 'my' is used. So can you drop the 'my' within the foreach loop and still have private scope within the body of the loop? As can be seen, using the 'for' loop there is a difference between using / not using 'm...

While and foreach mixed loop issue.

Hi my test.pl as below #!C:\Perl\bin\perl.exe use strict; use warnings; my $numArgs = $#ARGV + 1; print "thanks, you gave me $numArgs command-line arguments.\n"; while (my $line = <DATA> ) { foreach my $argnum (0 .. $#ARGV) { if ($line =~ /$ARGV[$argnum]/) { print $line; } } ...

Powershell filter files by SQL Server result set and copy

I am executing a query in SQL Server and returning a single column result set. I need to loop through the result set and find file names matching the record from the result set. Part of the file name is a sequence number. I need to sort the files in ascending order, select the first file, and then copy only that file to a subdirectory. T...

c# exit generic ForEach that use lambda

Does anyone know if it is possible to exit a generic ForEach that uses lambda? e.g. someList.ForEach(sl => { if (sl.ToString() == "foo") break; // continue processing sl here // some processing code } ); This code itself won't compile. I know I could use a regular foreach but for consistency I want to use lamb...

Foreach statement in listbox c#

Hey, I have a problem with a foreach statement in my project. So far I have the code: foreach(object i in listboxFiles.Items) { if (i == ".ftpquota") { listboxFiles.Items.Remove(i); } if (i == ".") { listboxFiles.Items.Remove(i); } if (i == "..") ...

Trouble using 'foreach' with a generic list

I have not until now tried to use a foreach clause in a generic list. The compile error I get is: foreach statement cannot operate on variables of type 'DMS.OrderNodeList' because 'DMS.OrderNodeList' does not contain a public definition for 'GetEnumerator' Any suggestions what to do next? Thanks, ...

Correct BOOST_FOREACH usage?

When using BOOST_FOREACH, is the following code safe? BOOST_FOREACH (const std::string& str, getStrings()) { ... } ... std::vector<std::string> getStrings() { std::vector<std::string> strings; strings.push_back("Foo"); ... return strings; } Or should I grab a copy of the container before calling BOOST_FOREACH, e.g.: cons...

Does Foreach Item in LinkedList give items in order?

Does Foreach Item in LinkedList give items in strict order? Is the strict order First=>Next=>Next=>...=>Last respected in foreach or maybe is better to use while (item != null) ... item = item.Next? ...

case-insensitive array_unique

I'm trying to write a few lines of code to make a case insensitive array unique type function. Here's what I have so far: foreach ($topics as $value) { $lvalue = strtolower($value); $uvalue = strtolower($value); if (in_array($value, $topics) == FALSE || in_array($lvalue, $topics) == FALSE || in_array($uvalue, $topics) == FA...

What does '=>' sign in php means?

Is this an operator? I can't find it here. What does the part "$_POST as $response_id => $response" in the following code means? // If the questionnaire form has been submitted, write the form responses to the database if (isset($_POST['submit'])) { // Write the questionnaire response rows to the response table foreach ($_P...

Counting number of 'hits' in for-each

I have the code below. I would the for-each to stop running after the if-sentenses have returned true and executed the code block 4 times. As i have no idea when the code block has been executed 4 times i can't use position() which was my first idea. Any help would be greatly appreciated. <xsl:for-each select="$itm//item[@template='new...

How are foreach executed in PHP?

foreach(func() as $item)... The above will only call func() once,but why?What's happening exactly? ...

How to delete object from array inside foreach loop?

Hi, I iterate through an array of objects and want to delete one of the objects based on it's 'id' property, but my code doesn't work. foreach($array as $element) { foreach($element as $key => $value) { if($key == 'id' && $value == 'searched_value'){ //delete this particular object from the $array uns...