Greetings monks. I am having difficulties with the following code, in which I am trying to conditionally assign a variable based on matching the first character of a string contained in 2 other variables.
if ($B1=~ m/^*/ | $B2=~ m/^*/) { my $type; $type eq "S"; } else { $type eq "I"; }
Basically, I am searching two other variables, if either of them have "*" as the first character, then variable $type should be set to the character variable S, otherwise it should be set to I. The error message I am getting is: Global symbol "$type" requires explicit package name at /home/radon00/amr/sandbox/alpha/SNPtagger.pl line 48. Global symbol "$type" requires explicit package name at /home/radon00/amr/sandbox/alpha/SNPtagger.pl line 51. So I think the problem has to do with the fact that I am using the $type variable which is declared in the if block, and the $B1 and $B2 variables which were declared outside of the if block. I'm just not sure what the solution is. Here is the entire code just in case some context is needed:
my $SNP_FILE="SNP.test.out"; # Will be $out in future, just hardcoded +to SNP.report for testing now. open (LINES,"<$SNP_FILE") || die ("Could not open file! \n"); # Read S +NP File <LINES>; while ($SNP_FILE=<LINES>) { my ($contig, $PP, $UPP, $Cons, $Cov, $Conflicts, $B1, $B2)=split( +'\t', $SNP_FILE); #Split line of SNP File my $ratio=sprintf("%.2f",($Conflicts/$Cov)); #Creates new variabl +e of ratio of Conflict Count/Total Coverage $PP=$PP+1; # Bump padded position coordinate by 1, as gap4 coordi +nates are off by 1 base compared to bank file $Cons=~ s/-/*/; # Replace - with gap4 pad notation in consensus v +ariable $B1=~ s/-/*/; # Replace - with gap4 pad notation in Base 1 vari +able $B2=~ s/-/*/; # Replace - with gap4 pad notation in Base 2 vari +able chomp $B2; $contig=~ s/Contig/#/; # Replace contig with number sign so gap4 +can recognize contig identifier if ($B1=~ m/^"*"/ or $B2=~ m/^"*"/) { my $type eq "S"; } else { my $type eq "I"; } my $type; print "$contig $PP $Cons $Cov $Conflicts $B1 $B2 $ratio $type \n" +; } close (LINES);
Thanks for the help in advance!

In reply to Setting Conditional Variable Based on Partial Match by Osiris1975

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.