AlertPowerMT        AlertHostsDown        Change Settings Network Devices        Upgrade Network Devices       

inne

 

przerywanie pętli [for, foreach] [PHP, Perl, Bash]

   23-12-2011 00:00:00       Odwiedziny: 951

## Kod: PHP [*.php]

<?php
$array = array("jablko", "gruszka", "banan", "truskawka", "mandarynka");

   for($i=0; $i<4; $i++) {
	echo $array[$i];
	   if($i == 2) { echo "."; break; } else { echo ", "; }
   }

# -----------------------------------------------------------------------------

$array = array("jablko", "gruszka", "banan", "truskawka", "mandarynka");

   foreach ($array as $key => $var) {
	echo $array[$key];
	   if($key == 2) { echo "."; break; } else { echo ", "; }
   }


# wynik: jablko, gruszka, banan.

?>


## Kod: Perl [*.pl]

#!/usr/bin/perl
my @array = ("jablko", "gruszka", "banan", "truskawka", "mandarynka");
my $i=0;
   for($i=0; $i<4; $i++) {
	print $array[$i];
	   if($i == 2) { print "."; last; } else { print ", "; }
   }

# -----------------------------------------------------------------------------

my %array = (0 => "jablko", 1 => "gruszka", 2 => "banan", 3 => "truskawka", 4 => "mandarynka");

   foreach my $key (sort keys %array) {
	print $array{<$key>};
	   if($key == 2) { print "."; last; } else { print ", "; }
   }


# wynik: jablko, gruszka, banan.



## Kod: Bash [*.sh]

#!/bin/bash
array=("jablko" "gruszka" "banan" "truskawka" "mandarynka");

   for (( i = 0; i < ${#array[@>}; i++ )); do
	echo -n ${array[$i>};
	   if (($i == 2)); then echo -n "."; break; else echo -n ", "; fi
   done

# wynik: jablko, gruszka, banan.



Copyright seszu © 30.10.2010-2020.