Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Title of Column and Split Function

by eepvesity (Initiate)
on Mar 28, 2013 at 15:49 UTC ( #1025988=perlquestion: print w/replies, xml ) Need Help??
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 (Prior) 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 $!;


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
      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 ++


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025988]
Front-paged by Corion
[Corion]: Discipulus: Yes, but the chart gives some limits on whether it's really worth spending time for saving your time. If you gain enjoyment, automating is still great, but it doesn't save time ;)
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2017-07-27 09:28 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (408 votes). Check out past polls.