Thank you all so so much for your feedback and the help, you are my heros right now. The errors pointed out by poj, hippo and haukex solved the issue, so when I only make those quick changes (the one variable change still can't believe I overlooked that I used the wrong variable all along and the append mode) in my old script and it worked wonders. I get what I want and I could cry tears of happiness right now. You guys can not imagine the relief I am feeling right now.

However I do not just want a working script anymore but actually one that is also looking good. For that I would love to implement all the changes that have been suggested, however I am running in several problems while doing so.

1) when I try to declare the variables when I need them for some reasons I still get the following warning: Global symbol "VARIABLE" requires explicit package name (did you forget to declare "my VARIABLE"?)

2) While trying to use the newer suggested way of opening files I get the following warning: Scalar found where operator expected at new_reads.pl line 24, near """$folder" (Missing operator before $folder?)

I don't really understand why these errors are occurring, but I would love to get it fixed simply because I want my script not to be an "outdated" thing, using old ways of handling files and variables lol.

I copied the "newer" version of the script below and once again, thank all of you for your help.

#!/usr/bin/perl -w use strict; use warnings; #Initiate all variables, hashes and co #Open folders in working directy my @folders = glob("*"); #to get all folders in directory; extension ( +"*") as wildcard to get all names foreach my $folder(@folders) #to speak to each element in directory { next if ($folder!~/^UNITAS_/); #skip elements which do not start w +ith "UNITAS" opendir(DIR,$folder)||die print$!; #open folder, end script when o +pening is not possible (DIR is the "filehandle" for the directory) print"\n$folder"; while( my $file=readdir(DIR)) #returns content of folder { next if($file!~/\.mapped_sequences$/); #get the mapped_sequenc +es file we need to read out the reads print"\n$file"; #print out file names to make sure we get the +right files my $reads = 0; #set the number of reads to 0 for each run open my $fileone, '<', "$folder/$file" or die ""$folder/$file" +: $!"; while(my $tocount=<$fileone>)#read file { chomp $tocount; $tocount =~ s/>//g; #remove all ">" next if ($tocount =~ /[A-Za-z]/); #skip lines which contai +n the sequence if ($tocount =~ /[0-9]/) #get the read-number { print"\n$tocount"; $reads = ($reads + $tocount); # add up all reads } print"\n$reads"; } close $fileone; my $trftable = 'unitas.tRF-table.txt'; #save file name in vari +able open my $trf, '<', "$folder/$trftable" or die ""$folder/$trfta +ble": $!"; undef = <$trf> for 1 .. 4; my %hash = (); #initiate empty hash while( my $line=<$trf>) { chomp $line; my @line=split("\t",$line); if($line[0]=~s/tRNA-[^-]+-...//) # "tRNA-"(matched tRNA un +d -) "[^-]+" beginning bis Ende, egal was "-..."(weiterer Strich bis +Ende) { my $tRNAname=$line[0]; $tRNAname=$&; # "$&" = last pattern match print"\n$tRNAname"; } else { my $tRNAname=$line[0]; $tRNAname=~s/-ENS.+$//; # "-ENS.+$" ( matched allen di +e -ENS. bis Ende enthalten) print"\n$tRNAname"; } my $hash{$tRNAname}{"5p-tR-halves"}+=$line[1]/$reads*10000 +00; $hash{$tRNAname}{"5p-tRFs"}+=$line[3]/$reads*1000000; $hash{$tRNAname}{"3p-tR-halves"}+=$line[5]/$reads*1000000; $hash{$tRNAname}{"3p-CCA-tRFs"}+=$line[7]/$reads*1000000; $hash{$tRNAname}{"3p-tRFs"}+=$line[9]/$reads*1000000; $hash{$tRNAname}{"tRF-1"}+=$line[11]/$reads*1000000; $hash{$tRNAname}{"tRNA-leader"}+=$line[13]/$reads*1000000; $hash{$tRNAname}{"misc-tRFs"}+=$line[15]/$reads*1000000; } open my $merge,">>","$folder/$merge" or die "Could not open $f +older/$merge : $!"; my @tRF_types=("5p-tR-halves","5p-tRFs","3p-tR-halves","3p-CCA +-tRFs","3p-tRFs","tRF-1","tRNA-leader","misc-tRFs"); foreach $tRNAname(sort{$a cmp $b}keys%hash) #sortiert die alph +abetisch nach keys { print MERGE $tRNAname; # print tRNA name foreach my $tRF_type(@tRF_types) { print MERGE"\t$hash{$tRNAname}{$tRF_type}"; # print co +unts for each tRF type separated by tab } print MERGE"\n";# print newline } close TRF; close MERGE; close DIR; } }

In reply to Re: Skript help needed - RegEx & Hashes by PandaRaey
in thread Skript help needed - RegEx & Hashes by PandaRaey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.