Re: Split and concatenation

2011-12-19 Thread Brandon McCaig
On Sat, Dec 17, 2011 at 08:22:31AM +, vishnu.kuma...@wipro.com wrote:
> Hi,

Hello:

> 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");
> my $e = @d;
> for (my $i=0; $i < $e; $i++) {
> 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");
> 
> 
> Output I am getting is Value after concatenation is "amm"

Make sure you always use the 'strict' and 'warnings' pragmas.
They should usually be the first couple of lines in every Perl
script or module file you write (i.e., right after your shebang).

use strict;
use warnings;

It looks like $abc in the file scope is not declared, and $abc
within the for-loop is only visible within that loop. That
explains your erroneous results. The 'strict' pragma would have
made it an error to refer to $abc without declaring it.

For example, executing your program with the strict and warnings
pragmas yields the following output:

Global symbol "$abc" requires explicit package name at - line 13.
Execution of - aborted due to compilation errors.

(Line 13 being your last line with the print statement on it)

See how Perl detects the problem for you and refuses to execute
until you fix it? Never write Perl code without these pragmas. :)
They will save you a lot of trouble.

That said, as somebody else pointed out, $abc would be undef in
your print statement (if allowed to execute i.e., without the
strict 'vars' pragma enabled) so your alleged output doesn't make
sense given what you have shown us.

The others have already shown you better ways to do this so I
guess I won't repeat them.

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

/me prints this E-mail unnecessarily. >:)

Regards,


-- 
Brandon McCaig  
Castopulence Software 
Blog 
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


RE: Split and concatenation

2011-12-19 Thread T D, Vishnu
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 +
 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


Re: Split and concatenation

2011-12-18 Thread Dr.Ruud

On 2011-12-17 09:20, T D, Vishnu wrote:


I am trying to convert the string abc.def.ghi.amm to abcdefghiamm


$string =~ s/\.+//g



using split and concatenation.


join "", split /\.+/, $string

--
Ruud


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




Re: Split and concatenation

2011-12-17 Thread John W. Krahn

vishnu.kuma...@wipro.com wrote:

Hi,


Hello,



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");
my $e = @d;
for (my $i=0; $i<  $e; $i++) {
 print("Value of array element $i is $d[$i]\n"); }

#concatenation
for (my $i=0; $i<  $e; $i++) {
   my $abc .= "$d[$i]";


my() creates the $abc variable inside the loop so it is only visible 
inside the loop.




}

print("Value after concatenation is $abc\n");


Output I am getting is Value after concatenation is "amm"


That is not possible because the variable $abc is not visible at file scope.




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

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




Re: Split and concatenation

2011-12-17 Thread Shlomi Fish
Hi Vishnu,

On Sat, 17 Dec 2011 08:22:31 +
 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: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/