eepvesity has asked for the wisdom of the Perl Monks concerning the following question:

In using the split function for columns and push function for placing names and heart rates in hash from files with following format:

Patient Heart Rates Melly 70 Glenn 69
How would I ensure that the column headings "Patient" and "Heart Rates" do not end up in the hash or are excluded from appearing in any data structures? Thank you.

Replies are listed 'Best First'.
Re: Column Heading and Split Function
by hdb (Monsignor) on Mar 28, 2013 at 16:02 UTC

    use strict; use warnings; my %hrates; while( <DATA> ) { chomp; next if /^Patient/; my ($name, $heartrate) = split; $hrates{$name} = $heartrate; } print %hrates; __DATA__ Patient Heart Rates Melly 70 Glenn 69
Re: Column Heading and Split Function
by McA (Priest) on Mar 28, 2013 at 15:57 UTC
    open my $fh, "<", "filename" or die $!; my $headerline = <$fh>; unless(defined $headerline) { die "ERROR: Couldn't read header line: Bad file format?"; } while(my $line = <$fh>) { process($line); } close $fh or die $!;

    McA

Re: Column Heading and Split Function
by thirdm (Sexton) on Mar 28, 2013 at 16:24 UTC
    use File::Slurp; my %rates = (); $_ = read_file(\*DATA); /$/mg; # skip first line $rates{$1} = $2 while /^(\w+)\h+(\d+)/mg; $, = "\t"; print %rates; __DATA__ Patient Heart Rates Molly 70 Glenn 69
      hmmm.
      use File::Slurp; $_ = read_file(\*DATA); /$/mg; # skip first line my %rates = /^(\w+)\h+(\d+)/mg; $, = "\t"; print %rates; __DATA__ Patient Heart Rates Molly 70 Glenn 69

        One more reduction and we have to put this node into Obfuscation... ;-)

        But while thinking it's interesting it deserves a ++

        McA