0

Loop control from a subset

 2 years ago
source link: https://www.codesd.com/item/loop-control-from-a-subset.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Loop control from a subset

advertisements

I want to use subshells for making sure environment changes do not affect different iterations in a loop, but I'm not sure I can use loop control statements (break, continue) inside the subshell:

#!/bin/sh
export A=0
for i in 1 2 3; do
  (
  export A=$i
  if [ $i -eq 2 ]; then continue ; fi
  echo $i
  )
done
echo $A

The value of A outside the loop is unaffected by whatever happens inside, and that's OK. But is it allowed to use the continue inside the subshell or should I move it outside? For the record, it works as it is written, but maybe that's an unreliable side effect.


Just add

echo "out $i"

after the closing parenthesis to see it does not work - it exits the subshell, but continues the loop.

The following works, though:

#! /bin/bash
export A=0
for i in 1 2 3; do
    (
        export A=$i
        if [ $i -eq 2 ]; then exit 1 ; fi
        echo $i
    ) && echo $i out      # Only if the condition was not true.
done
echo $A

Related Articles

I need to determine what action to take based on the loop control variable

Inside of a for loop, I want to determine what action to take based on loop control variable, it get's tricky as you can see from my comments public void someFunction(int numberOfReps) { for (int i = 0; i < numberOfReps; i++) { //if it's first execut

How to create controls from code in a custom control?

In MainPage.xaml.cs (Silverlight Application) I can do something like this: StackPanel myStackPanel = new StackPanel(); Button myButton = new Button(); myButton.Content = "Button"; myButton.Width = 200; myButton.Height = 30; Button myButton1 = n

How to assign HTML content to the iframe control from asp.net codebehind?

I want to assign the html content to iframe control from asp.net code behind page; this is working fine myIframe.Attributes.Add("src", "pathtofilewith.html"); but, i don't want to give the path of html file to display i just want to as

Algorithms for selecting a uniformly random number from a subset of integers

Suppose that int array a[i]=i, i=0, 1, 2, ..., N-1. Now given that each integer would associate with a bit. I need an algorithm to uniformly randomly select an integer from the subset of integers whose associated bit are 0. It is assumed that the tot

Unable to access User Control child controls from code behind

I am trying to access some of the user controls' child controls from code behind. Some of them is working while others not. Faulty user controls don't have designer files. I want to know if this is the reason? If so then I can't create some of the co

Removing User Controls from the Parent Form

I use this code to show user control on main form private void MainForm_Load(object sender, EventArgs e) { Sell sell = new Sell(); sell.Dock = DockStyle.Fill; this.Controls.Add(sell); } I want to remove this user control from main form but this code

Creating Web User Controls from ascx.cs

I am trying to create a instance of a user web control from the code behind of another user web control. using ASP.NET; placeholder1.Controls.Clear(); module_userWebControl_ascx slModule = (module_userWebControl_ascx)LoadControl("~/module/module_user

Using a stopclock-based func as a loop control variable

Trying to use a Func<bool> as a loop control variable as follows seems to upset Resharper. Given the following Stopwatch: var executionTimeStopwatch = new Stopwatch(); executionTimeStopwatch.Start(); This raises a "Loop control variable is neve

How do I remove my control from the action sheet without clicking on the buttons in the iPhone?

how to remove my control from the actionsheet without click on actionsheet's buttons in iphone ?i think you want to dismiss the Action Sheet without tapping any buttons from Action Sheet. am i right or not ? You can do it by putting a CancelButton in

I renamed the aspx file name but now I can not access the controls from the cs file

I renamed aspx file name but now i can't reach to controls from cs file. I deleted designer.cs file and created by right click on aspx file with Convert To Web Application. But still can't find controls while i press Ctrl+Space (code intellisense).Ch

GREP loop control

I have about 3000 files in a folder. My files have data as given below: VISITERM_0 VISITERM_20 VISITERM_35 ..... and so on Each files do not have the same values like as above. They vary from 0 till 99. I want to find out how many files in the folder

Access Windows Control from Backgroundworker DoWork

my issue is the following: I have a windows form in which I've placed a LayoutPanel, when the forms Loads, multiple controls like: textboxes and labels are being added to the LayoutPanel. Then on a button click, I need to process the data entered by

How to prevent the control from stealing the focus?

We have a 3rd party control loaded in our C# WinForms app. When we call a method on this 3rd party ActiveX control, it asynchronously steals focus. For example: // This call causes 3rd party to spawn a thread and steal focus // milliseconds later. fo

WinForms: How do I include a user control from an external library?

I have a class in an external library subclassing UserControl and set to public. Now I want to use this usercontrol in my main WinForms app. I have added this external library to the references of the main app, but the user control haven't appeard in

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK