Hi Yuma,

a few comments on your code.

On Mon, 2 Jul 2012 23:10:36 +1000
Yuma More <yuma...@gmail.com> wrote:

> Hi there:
> 
> I am very new in Perl and I am trying to write a script to search for
> similar text in two different files, If it founds a match, it should out
> put the whole line from the second file in a third file.
> 
> 
> 
> Let say that my first file (F1) has a list of some samples like
> 
> >Cortezaaerea23489284
> 
> >Cortezaterreste0714857
> 
> 
> 
> And the second file (F2) has information about all my universe of samples
> 
> >Cortezaaerea23489284
> QIUEFQIFNIQAONDAIFIQHFAIFAUIFNUAINAHDUFUDHJAHFUAHFUAFYAUFAUHADUHAUFHAUHFUAHUAHFUAUFAUFHAUFHAUFHUAHFUDAHFUAFUAI
> 
> >Cortezaterreste0918347
> QIUEFQIFNIQAONDAIFIQHFAIFAUIFNUAINAHDUFUDHJAHFUAHFUAFYAUFAUHADUHAUFHAUHFUAHUAHFUAUFAUFHAUFHAUFHUAHFUDAHFUAFUAI
> 
> >Cortezaterreste09899897
> IADUFIAJIOADFIOAMCIADFAIUFIAUFIOAUFDIOAUFAIUFAIIUFIAUFIDAUFIAIAUFIAUFIAUFIOAUFIOAUDOIFAUIODUAIOFUAIOFUAIUFOIAUFIAOUFDOIAU
> 
> >Cortezaaerea234789048
> IADUFIAJIOADFIOAMCIADFAIUFIAUFIOAUFDIOAUFAIUFAIIUFIAUFIDAUFIAIAUFIAUFIAUFIOAUFIOAUDOIFAUIODUAIOFUAIOFUAIUFOIAUFIAOUFDOIAU
> 
> >Cortezaterreste0714857
> 
> 
> IADUFIAJIOADFIOAMCIADFAIUFIAUFIOAUFDIOAUFAIUFAIIUFIAUFIDAUFIAIAUFIAUFIAUFIOAUFIOAUDOIFAUIODUAIOFUAIOFUAIUFOIAUFIAOUFDOIAU
> 
> 
> 
> The output for this example should be like this
> 
> >Cortezaaerea23489284
> QIUEFQIFNIQAONDAIFIQHFAIFAUIFNUAINAHDUFUDHJAHFUAHFUAFYAUFAUHADUHAUFHAUHFUAHUAHFUAUFAUFHAUFHAUFHUAHFUDAHFUAFUAI
> 
> >Cortezaterreste0714857
> 
> 
> IADUFIAJIOADFIOAMCIADFAIUFIAUFIOAUFDIOAUFAIUFAIIUFIAUFIDAUFIAIAUFIAUFIAUFIOAUFIOAUDOIFAUIODUAIOFUAIOFUAIUFOIAUFIAOUFDOIAU
> 
> 
> 
> My script is as follows, it work partially but always just with the last
> element in file 1. Could anyone help me to improve it?
> 
> 

1. Always start with "use strict;" and "use warnings;". See:

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

> 
> open (A,"C:/F1.txt");

1. Use lexical file handles.

2. Use three-args open.

3. Use "use autodie;" or alternative append or die ... to the open call:

http://perl-begin.org/tutorials/bad-elements/#open-function-style

> 
> print "openA\n";
> 
> open (B,"C:/F2.txt");
> 

Ditto.

> print "openB\n";
> 
> open (OUT,">>C:/R1.txt");
> 

Ditto.

> my @arr=<A>;
> 
> close (A);

Maybe you want to do chomp(@arr) - that or use something like
https://metacpan.org/release/IO-All or https://metacpan.org/release/File-Slurp .

> 
> my $Names;
> 

You shouldn't pre-declare your variables - declare them when you need them:

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

> while ($line = <B>){

It should be "my $line" (due to the strict) and you shold have a space before
the "{".

> 
>     print "$Names";@fields = split (/\t/,$line);

Don't put two statements on the same line. Use "my @fields".

> 
>     our $Seq = "$fields[0]\t$fields[1]";
> 

Why is it "our" instead of my?

>     our $Label = "$fields[0]";
> 

You've used $fields[0] more than once. You should do something like:

my ($label, $second_field) = (@fields);
my $seq = "$label\t$second_field";

>     foreach $Names (@arr){
> 

should be "foreach my $name (@arr)"

> # when I try to print “$Names” here; it print each element of the array.
> 

Yes, because it iterates on the loop.

> #but when I try the next comparison
> 
>      if ($Names eq $Label){
> 
>     print OUT "$Seq";

@arr still contains the newline.


> 
> #it just identify the last element of the array
> 
> }

The "}" should be properly indented.

----------------

Please reply to the entire list when replying (don't send the message only to 
me).

Which book or tutorial are you using to learn Perl? Your code is pretty bad
and non-modern. For learning better practices, see:

* http://perl-tutorial.org/

* http://perl-begin.org/ 

* http://onyxneon.com/books/modern_perl/

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

“I’m not straight — I’m Israeli.”

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