Thanks guys for your suggestion....

-----Original Message-----
From: Shlomi Fish [mailto:shlo...@shlomifish.org] 
Sent: Saturday, December 17, 2011 2:48 PM
To: vishnu.kuma...@wipro.com
Cc: beginners@perl.org
Subject: Re: Split and concatenation

Hi Vishnu,

On Sat, 17 Dec 2011 08:22:31 +0000
<vishnu.kuma...@wipro.com> 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 .

Please do not print this email unless it is absolutely necessary. 

The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments. 

WARNING: Computer viruses can be transmitted via email. The recipient should 
check this email and any attachments for the presence of viruses. The company 
accepts no liability for any damage caused by any virus transmitted by this 
email. 

www.wipro.com

Reply via email to