loop

Scala Maps (Map class examples)

Here's a quick look at how to use the Scala Map class, with a colllection of Map class examples.

The immutable Map class is in scope by default, so you can create an immutable map without an import, like this:

val states = Map("AL" -> "Alabama", "AK" -> "Alaska")

To create a mutable Map, import it first:

Iterating over Scala Maps (for and foreach loop examples)

Scala Map FAQ: How can I iterate/loop over a Scala Map?

There are several different ways to iterate over a Scala Map, and the method you choose depends on the problem you need to solve.

To get started with our examples, let's create a simple Map we can work with:

A simple Arduino Uno LED fade program

Shortly after writing my Arduino Uno Hello, World example, I read my documentation a little more, learned that Pin 9 on the Arduino Uno board is an analog pin, switched the LED wire to that pin, and created the following program, which slowly lights up the LED using an analog programming approach, and then fades the LED light out.

Here's the source code for my LED fade sketch:

The AppleScript for loop (and while loop) examples

AppleScript for loop FAQ: How do I use an AppleScript for loop? (Also, how do you use an AppleScript while loop?)

This is actually a bit of a trick question, as there is no AppleScript for loop or while loop syntax. Instead you use the AppleScript repeat command, as shown in the following examples.

AppleScript for loop examples

Where you might expect an AppleScript for loop to iterate over a list, you use the AppleScript "repeat with" syntax:

An AppleScript list iterate/loop example

AppleScript list FAQ: How do I iterate/loop over an AppleScript list?

As I continue to work on my Mac speech recognition software projects, I've often found the need to loop over an AppleScript list. One way to loop over an AppleScript list is to use the AppleScript repeat with syntax:

AppleScript list examples

AppleScript list FAQ: Can you share some simple AppleScript list examples?

Sure. As I've been doing a lot of work with Mac speech recognition software lately I've been working a lot of with AppleScript lists. Let's take a look at some common AppleScript list examples (and programming needs).

How to create an AppleScript list

A simple AppleScript list can be created like this:

PHP array - How to loop through a PHP array

PHP array FAQ: How do I iterate (loop) through a PHP array?

Answer: The easiest way to loop through a PHP array is to use the PHP foreach operator. If you have a simple one-dimensional array, like the $argv array which lets you access command line arguments, you can loop through it like this:

Shell script counter FAQ - How to increment a counter in a shell script

Unix/Linux shell script FAQ: Can you share a simple Linux shell script that shows how to count, i.e., a shell script that increments a counter in a for loop or while loop?

Sure, I just needed to increment a while loop counter myself, so I thought I'd share my example shell script code here.

The Perl until loop examples and syntax

Perl until loop FAQ: Can you share some examples of the Perl until loop syntax?

The Perl until loop is similar to the Perl while loop, but essentially does the opposite thing. Where the Perl while operator will run this loop forever:

while ('true')
{
  # do stuff here
}

the Perl until loop will never run this loop:

Perl best practice: prefer foreach to for

Perl problem: When you don't use Perl every day, it's hard to remember the combination of the for loop syntax and how to deal with the special $_ variable to refer to each element within the body of the loop.

Solution: As I write this today, I still can't remember how to get this for loop and $_ syntax to work, at least not without pulling out a book, or looking it up on the internet. A much better solution is to just use Perl's foreach syntax, and never look back.

Syndicate content