in reply to Re^2: Variable blasphemy
in thread Variable blasphemy

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

Replies are listed 'Best First'.
Re^4: Variable blasphemy
by SixTheCat (Novice) on Jul 24, 2015 at 15:51 UTC
    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
Re^4: Variable blasphemy
by SixTheCat (Novice) on Jul 24, 2015 at 15:49 UTC
    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.