in reply to Re: Variable blasphemy
in thread Variable blasphemy

Updated the code in the original post and added the "use warnings". Same output problems though. No warnings given. Sigh.

Replies are listed 'Best First'.
Re^3: Variable blasphemy
by poj (Abbot) on Jul 24, 2015 at 15:38 UTC
    Are you reading a windows file on a unix machine ?
    Try changing chomp to s/\s+$//g;
    Update:

    Assuming each RSno has only one StarName and vice versa, it would be simpler to use a hash for the conversion

    #!perl use strict; my @Stars = qw(CYP2C19_10 CYP2C19_12); return if (@Stars == 0); # name,no my $ref_file = "test.csv"; open my $star_FH,'<',$ref_file or die "Could not find reference file $ref_file : $!"; my %RSno=(); my $count=0; print "Reading from ref_file $ref_file .. "; while (<$star_FH>){ s/\s+$//; my ($no,$name) = split ",",$_; $RSno{$name} = $no; ++$count; } close $star_FH; print "$count records read\n"; for my $StarIndex (@Stars){ if (exists $RSno{$StarIndex}){ print "$StarIndex is $RSno{$StarIndex}\n"; } else { print "$StarIndex NO CONVERSION\n"; } }
    poj
      That worked! Thanks! Can you explain what the difference was (and what the above code means?) =P Thank you so much!
        s/\s+$//

        demonstrates the use of the s command, which takes this form: s/replace stuff matching this pattern/with this/. In this case the "with this" is blank, which means it will just remove stuff matching the pattern. \s is an abbreviation for white space characters - space, tab, line feed. + means match one or more such characters in a row. $ means match only if the pattern is found at the very end of a string.

        This is an inadequate explanation; see the Tutorials section, especially here.

        Dum Spiro Spero
      The csv file was saved under windows, yes. Do I need to open it a different way?

        Ah. That explains it. Windows Linebreak consists of CarriageReturn and LineFeed while Linux uses a LineFeed. Your chomp only strips the LineFeed, but leaves the CarriageReturn in place.

        You can either pre-process the CSV file with dos2unix / flip or some other tool or manually strip the CarriageReturn in perl.