Hi Prasanth,

OK now that the message arrived.

On Tue, 10 Feb 2015 09:21:33 +0200
Shlomi Fish <shlo...@shlomifish.org> wrote:

> Dear Prasanth,
> 
> next time please reply to the list. I'm going to comment on your code after
> this forwarded message arrives at the list.
> 
> Regards,
> 
>       Shlomi Fish
> 
> Begin forwarded message:
> 
> Date: Tue, 10 Feb 2015 11:01:56 +0530
> From: prasanth <prasanthne...@gmail.com>
> To: Shlomi Fish <shlo...@shlomifish.org>
> Subject: Re: use perl format data
> 
> 
> Hi,
> 
> Lets see if the  below code helps:
> 

First of all your code is missing strict and warnings:

http://perl-begin.org/tutorials/bad-elements/#no-strict-and-warnings

(Note : perl-begin.org is a site which I originated and am maintaining.)

>  sub read_array{
>  my @file;

You should not call a variable file. @lines would be more appropriate here:

http://perl-begin.org/tutorials/bad-elements/#calling-variables-file


>   ($filename) =@_;

This is missing a "my" annotation. Strict would have caught it.

> open($fh,'<:encoding(UTF-8)', $filename)  or die "Could not open file
> '$filename' $!";

Missing "my" for $fh, but the open is otherwise good.

> 
>  while (my $row = <$fh>) {
>    chomp $row;
>    push(@file,$row);
> }

This can be done more efficiently using:

my @lines = <$fh>;
chomp(@lines);


> close($fh);
> 
> 
> @file;
> }

1. Always have an explicit return:

http://perl-begin.org/tutorials/bad-elements/#explicit_return .

2. It would be a better idea to return a reference to the array instead of
returning it as a list in the function call.

3. You can use a module such as
https://metacpan.org/pod/Path::Tiny#lines-lines_raw-lines_utf8 to do all this
for you.

> 
> @fa= &read_array('fa.txt');
> 
> @fb= &read_array('fb.txt');
> 
> @fc= &read_array('fc.txt');

Don't call subroutines with ampersands:

http://perl-begin.org/tutorials/bad-elements/#ampersand-in-subroutine-calls

> 
> $i=0;
> 
> while($i<=3){
> print "$fa[$i]\t$fb[$i]\t$fc[$i]\n";
> $i++;
> }

This is better written as:

for my $i (0 .. 3) {
...
}

And furthermore the 3 is a magic number:

http://perl-begin.org/tutorials/bad-elements/#magic_numbers

------

Perhaps you'd like to go over the rest of the
http://perl-begin.org/tutorials/bad-elements/ page so you'll know how to avoid
writing code exhibiting other such bad elements in the future. This code is
certainly unacceptable for giving as a source of enlightenment in a beginners’
forum 

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Becky: My mother’s Jewish and I’m kind of a spoiled Jewish American Princess.
    — http://www.shlomifish.org/humour/Buffy/A-Few-Good-Slayers/

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