in reply to Why are my hash keys undefined?

The * quantifier means "zero or more times". It seems your data contain commas at line ends, so the non-comma is matched zero times. $1 is then the empty string:
#!/usr/bin/perl use warnings; use strict; my %denovo; while (my $line = <DATA>){ chomp $line; if ($line =~ /([^,]*)$/){ my $name = "$1"; print "name is $name\n"; $denovo{$name} = $line; # Quotes not needed. } } print "hash denovo is:\n"; while( my( $key, $value ) = each %denovo ){ print "$key: $value\n"; } __DATA__ no comma comma, in the middle comma at the end, previous line was empty
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Why are my hash keys undefined?
by Anonymous Monk on Feb 08, 2016 at 17:43 UTC
    Thanks for the response. The thing is my data doesn't have commas at the ends of the line, and I know that the regex matchs the right thing because the print in the if statement always gives me the names I expect. Despite this, the keys are empty!
      Windows line endings, then? The keys aren't empty, but contain "\r" at the end. When printed, the character moves the cursor to the beginning of the line, and the value then overwrites the key. Convert your files to the *nix format using dos2unix or fromdos, or apply
      s/\r//
      to your input lines.
      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Your quite right - the data wasn't in unix format and the function suggested solves the issue. Many thanks indeed!