Hi Shawn,

thanks for your answer. See below for my response.

On Wed, 5 Sep 2012 09:54:11 -0400
Shawn H Corey <shawnhco...@gmail.com> wrote:

> On Wed, 5 Sep 2012 14:33:13 +0100
> jet speed <speedj...@googlemail.com> wrote:
> 
> > Hi All,
> > 
> > i have an regx question. i have the array contents, now i want to
> > remove the first 2 characters (fc) of each element in the array and
> > store it in a second array ex: @array2
> > 
> > @array ="fc20/1, fc30/22, fc40/3, fc20/1";
> > 
> > output
> > 
> > @array2 ="20/1, 30/22, 40/3, 20/1";
> > 
> > please advice.
> 
> It would be helpful if you posted actual Perl code.
> 
> Try:
> 
>     # remove the first 2 characters from every element of the array
>     my @array2 = map { s/^..//msx } @array1;
> 

This code is wrong in two respects:

1. the map clause will return the return value of the s/// subtitution and will 
modify
the original array in place:

[SHELL]
shlomif@telaviv1:~$ cat s.pl
#!/usr/bin/perl

use strict;
use warnings;

my @array1 = ("fc11", "fc12", "fc13");
my @array2 = map { s/^..//msx } @array1;

print "Array 1 is " . join(",", @array1) . "\n";
print "Array 2 is " . join(",", @array2) . "\n";
shlomif@telaviv1:~$ perl s.pl
Array 1 is 11,12,13
Array 2 is 1,1,1

2. A minor problem of semantics is that under /m "^" matches any start of line, 
so \A is
preferable.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

Chuck Norris is a real programmer. He writes programs by implementing the most
optimised machines for them using real atoms.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to