Hi Rajeev,

some comments on your code:

On Fri, 2 Sep 2011 13:28:49 -0700 (PDT)
Rajeev Prasad <rp.ne...@yahoo.com> wrote:

>  
> unfortunately it was not working as i intended.
>  
> so i have new code with some help now:
> using $column variable, i can control which column (from 1 to 4) i want to be
> printed with column 5. 

Add "strict" and "warnings".

> open(IN_FH,"<","infile") or die "Cannot open infile because: $!";
> open(OUT_FH,">","outfile") or die "Cannot open outfile because: $!";

Always use lexical filehandles: 

[CODE]
open(my $in_fh,"<","infile") or die "Cannot open infile because: $!";
[/CODE]

But it's good that you've used three-args open followed by "or die".

> foreach $line (<IN_FH>){

that should be:

while (my $line = <$in_fh>) {

With:

1. while instead of a foreach - consumes less memory.

2. Lexically scoped $line variable.

3. space before the "{".

>     @tmpAR=split(/ /,$line,5);                                   #/ +/ will
> split at any number of white spaces...

Please call the array with a more meaningful name than @tmpAR. Also declare it
using "my" (strict would have warned you about that.).
 
> next if $tmpAR[$column] == 0;

Where is $column defined? And it should be $column_idx or something.

I personally prefer to label all next/last/redo statements, but some people
here disagree.

>     push(@tmpAR2,"$tmpAR[$column] $tmpAR[4]");      # we can use -1 here as
> last element of array

@tmpAR and @tmpAR2? Oh god... Please use more meaningful variable names.

Also, you should use array references here:

[CODE]
push @all_records, [@columns[$column, 4]];
[/CODE]

> } 

> print OUT_FH sort { (split(/ /,$a))[0] <=>
> (split(/ /,$b))[0] } @tmpAR2; close IN_FH;
> close OUT_FH;

This could be easier done using the array of array refs and a map to stringify
them.

For more advice, see:

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

Sorry for mangling the reply.

Regards,

        Shlomi Fish


-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

Bill Gates, CEO of Microsoft decides to use Richard Stallman’s Emacs as the
basis of his company’s state‐of‐the‐art product Microsoft Editing Macros™
Enterprise Edition XP .NET Professional.

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