Answers for "perl - for ($i=0; $i<scalar(@array); $i++)"

0

perl - for ($i=0; $i<scalar(@array); $i++)

@array = (1, 2, 'three', $file_handle, undef);
$array[1]                  #=> 2    ## Second element array (start counting at 0)
@array[1,2]                #=> (2, 'three') ## A "slice" as list of elements selected from array
@array[1,2] = (12, 13);    #=> Assigns multiple array items

$#array                    #=> 4    ## Last element index (0..n) of array, -1 if empty.
scalar(@array)             #=> 5    ## Number of elements in the array, 0 if empty.

push @array, 123;                   ## Appends value(s) to the end of the array
pop  @array;               #=> 123  ## Removes and returns the last element of the array
unshift @array, 123;                ## Prepends value(s) onto the head of the array
shift @array               #=> 123  ## Removes and returns the first element from the array

foreach $element (@array) { # The foreach structure iterates over the loop
  print $element
}

for ($i=0; $i<scalar(@array); $i++) { # The famous C-style for loop can be used
  print $array[$i];
}

foreach my $i (0..$#array) { # Use the Range operator to iterate over the indexes of the array
  print "Element $i is $array[$i]n";
}

# Use map to iterate over the array, returning new array ($_ is the temp variable in block)
@array = map { $_ * 2 } @array; # This example doubles the values of each array element

# Use grep to generate list of elements matching your conditional expression ($_ is temp var)
@array = grep { $_ % 2 } @array; # Returns the odd numbers from an array of numbers
Posted by: Guest on January-11-2022

Code answers related to "perl - for ($i=0; $i<scalar(@array); $i++)"

Browse Popular Code Answers by Language