Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

how to remplace a names in a file by keys with hash

by zelman796 (Initiate)
on Apr 27, 2018 at 19:48 UTC ( [id://1213705]=perlquestion: print w/replies, xml ) Need Help??

zelman796 has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I need your help, I have a file like this:

>1 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >2 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG

And I would like to change all the numbers by a list of words, I made the next program with perl but I am new using the program and I dont know who is wrong with that.

%lista2=( 1 => "CAT00.3", 2 => "CAT43.1", 3 => "CAT40.3" ); open(OA,">file2.txt"); foreach $key (keys %lista2){ open(SAL,"file.txt"); while(<SAL>) { chomp; if(/>/) { @w=split("\t"); $r=0; s/\;//g; if (/%lista2[i]/) { print OA "$_ $lista2{$key}\n" ; $r=1; } } } } close(SAL); close(OA);

I would to get this:

>CAT00.3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >CAT43.1 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >CAT40.3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG

But Idont know how do that.

Replies are listed 'Best First'.
Re: how to remplace a names in a file by keys with hash
by Cristoforo (Curate) on Apr 27, 2018 at 22:21 UTC
    Hello

    Your program is full of errors, some of which would have been caught if you had at the top of your program:

    use strict; use warnings;
    Rather than going through your program to note the errors and some useless statements, I will post a correct program and you will see how it should look.
    #!/usr/bin/perl use strict; use warnings; my %lista2 = ( 1 => "CAT00.3", 2 => "CAT43.1", 3 => "CAT40.3" ); # open filehandle for reading (check that open was successful or die) open my $AL, '<', 'file.txt' or die $!; # open filehandle for writing (check that open was successful or die) open my $OA, '>', 'file2.txt' or die $!; while (my $line = <$AL>) { if ($line =~ /^>(\d+)$/) { my $key = $1; if (exists $lista2{$key}) { $line =~ s/$key/$lista2{$key}/; } } print $OA $line; } close $AL or die $!; close $OA or die $!;
    Note that it does not loop through the keys like you do. And it only opens the file for reading once (the main loop).

    As for what's inside your while loop, there doesn't seem to be any reason for the different actions you have taken. Most don't make sense.

    Instead of using the default variable '$_' to read in the input in the while loop, I assigned the line being read to '$line' and this was just for clarity for this example. If I were writing the program myself, I would've probably relied on the default variable '$_' instead.

    Since I am using 'strict', I have to declare all the variables (with my ...) when they are first mentioned.

Re: how to remplace a names in a file by keys with hash
by LanX (Saint) on Apr 27, 2018 at 22:14 UTC
    you can simply substitute a number into the hash-value s/^>(\d+)/$lista2{$1}/

    the following demonstration is safer because it is also catching missing hash entries

    use strict; use warnings; my %lista2= ( 1 => "CAT00.3", 2 => "CAT43.1", 3 => "CAT40.3" ); while (<DATA>) { s/^>(\d+)/$lista2{$1} || die "unknown number $1"/e; print } __DATA__ >1 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >2 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG >4

    unknown number 4 at d:/Users/lanx/pm/hash_regex.pl line 14, <DATA> lin +e 7. CAT00.3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG CAT43.1 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG CAT40.3 AACTCTGGGACAATGGCACACGGGAAACAGATAATGAACGATCAGCACAGGGAACTAGCG Compilation exited abnormally with code 255 at Sat Apr 28 00:16:17

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

    update

    just noticed, the s/^>(\d+)/$lista2{$1}/ variant will already warn you.

    Use of uninitialized value within %lista2 in substitution iterator at hash_regex.pl line 14, <DATA> line 7.

Re: how to replace a names in a file by keys with hash
by haukex (Archbishop) on Apr 28, 2018 at 08:52 UTC

    Crossposted to StackOverflow. Crossposting is ok, but it is considered polite to inform about it so that efforts are not duplicated.

    Also, in your StackOverflow post you show the numbers in the input data as ">;1;". Which is it?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1213705]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 02:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found