Re: Remove first letter of each string in array

From: Tore Aursand (tore_at_aursand.no)
Date: 05/27/04


Date: Thu, 27 May 2004 16:46:43 +0200

On Thu, 27 May 2004 15:07:20 +0100, Julia deSilva wrote:
> my @array = qw(1this 2is 3a 4new 5message);
>
> for (my $x=0;$x<@array;$x++){
> $array[$x] = substr($array[$x],1,length($array[$x]))
> }
>
> foreach (@array){print"$_\n";};
>
> This does indeed remove the first letter of each string in this array, but
> is there a better way ! ?

Use Perl's 'for' or 'foreach';

  for ( @array ) {
      $_ = substr( $_, 1 );
  }

-- 
Tore Aursand <tore@aursand.no>
"War is too serious a matter to entrust to military men." (Georges
 Clemenceau)


Relevant Pages

  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Trouble Using System.Array.ForEach
    ... The reason that the Array.ForEach Generic method is not suitable for your purposes in this case is that on each iteration of the 'hidden' loop, each element of the array is paseed to the Action delegate Sub, ByVal. ... Therefore the string that is passed to the delegate is returned unaltered and the parent array is not modified. ... And you may be right about the ForEach not fitting my conditions, I really can't say for sure since I have never had the opportunity to use ForEach, but either way, it never hurts to have extra knowledge so that if a situation were to come up where it was fit I would know it. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: foreach pretty useless for composite classes, dont ya thunk?
    ... Change the code to look like this in your foreach within main: ... I modified your original code to include an array of ints (in addition ... public string[] FirstNames; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need a simple database for name and email only
    ... function getMultipleSegments($segment, $xml) ... foreach ... $string = strtr; ... $supportedExtensions = array ( ...
    (alt.php)
  • Re: foreach statement output
    ... know about foreach loop workings. ... loop as though it was a C-style for loop. ... the number of characters in a string. ... elements in an array, we refer to the array's *size*. ...
    (comp.infosystems.www.authoring.cgi)