Hi Vishnu,
On Sat, 17 Dec 2011 08:22:31 +0000
<[email protected]> wrote:
> Hi,
>
> I am trying to convert the string abc.def.ghi.amm to abcdefghiamm using split
> and concatenation. I am missing something somewhere.. please help me to fix
> the code
>
> my $string = "abc.def.ghi.amm";
>
> my @d = split(/\./,"$string");
No need for double-quotes around the string:
my @d = split(/\./, $string);
> my $e = @d;
> for (my $i=0; $i < $e; $i++) {
This is better written as «for my $i (0 .. $#d)».
> print("Value of array element $i is $d[$i]\n"); }
>
> #concatenation
> for (my $i=0; $i < $e; $i++) {
> my $abc .= "$d[$i]";
> }
>
> print("Value after concatenation is $abc\n");
>
1. You should declare $abc outside the loop and concatenate to it.
2. No need for double quotes around $d[$i].
3. You can just use http://perldoc.perl.org/functions/join.html .
So your program becomes:
my $abc = join('', split(/\./, $string);
In this case it can also be written using:
my $abc = $string;
$abc =~ s/\.//g;
Or:
my $abc = $string;
$abc =~ tr/.//d;
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld
The number of items on an open source project’s to‐do list always grows or
remains constant.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/