Hello,
I have the following perl array:
@longname = (Fasthernet0/0 Fasthernet0/1 Serial0/1/0 Serial0/2/1 Tunnel55 Tunnel77)
with the followinh array:
@shortname = (Fa0/0 Fa0/1 Se0/1/0 Se0/2/1 Tu55 Tu77)
in other words, I need to remove the following from each element in the array (from 3rd char. to first number).
Any one help me to do this??
Thanks in advance.
Here you go:
@longname = qw(Fasthernet0/0 Fasthernet0/1 Serial0/1/0 Serial0/2/1 Tunnel55 Tunnel77);
@shortname = map { s/(..)\D*(.*$)/\1\2/; $_ } @longname;
$\ = "\n";
$, = "\n";
print @shortname;
---------- Post updated at 11:59 AM ---------- Previous update was at 11:58 AM ----------
And the results:
Fa0/0
Fa0/1
Se0/1/0
Se0/2/1
Tu55
Tu77
my @shortname = map { s/(..)\D*(.*$)/$1$2/; $_ } @longname;
@leo gutierrez -- you are correct, I should have used $1$2
, not \1\2
. But I think we both should have used
s/^(..)\D*(.*)$/