Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

hash_reference from file

by hmadhi (Acolyte)
on Sep 29, 2011 at 08:57 UTC ( #928496=perlquestion: print w/ replies, xml ) Need Help??
hmadhi has asked for the wisdom of the Perl Monks concerning the following question:

I have the following file that I would like to read into a array of hash references

CORDR KZN MTC-N AINT 2011-09-29 09:00 DTL PM05 83.79 4 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR20 85.39 3 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR21 86.06 1 - - CORDR Unk MTC-N AINT 2011-09-29 09:00 JSA BR22 84.55 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RN10 86.97 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS11 84.57 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS12 86.31 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SD10 86.79 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SN10 85.54 1 - -

eg

{APP=>'CORDR' , REG=>'SGC', KPI=>'MTC-N' INTERFACE=>'AINT', DATE=>'2011-09-29', TIME=>'09:00', NODE=> 'JSA', RAN => 'SN10', SUCC_RATE=85.54 ... }

How do I code this in perl

Comment on hash_reference from file
Select or Download Code
Re: hash_reference from file
by choroba (Canon) on Sep 29, 2011 at 09:11 UTC
    #!/usr/bin/perl use Data::Dumper; use constant KEYS => qw/APP REG KPI INTERFACE DATE TIME NODE RAN SUCC_RATE/; use warnings; use strict; my @array; while (<DATA>) { my @line = split; my %hash; @hash{+KEYS} = @line; push @array, {%hash}; } print Dumper \@array; __DATA__ CORDR KZN MTC-N AINT 2011-09-29 09:00 DTL PM05 83.79 4 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR20 85.39 3 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR21 86.06 1 - - CORDR Unk MTC-N AINT 2011-09-29 09:00 JSA BR22 84.55 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RN10 86.97 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS11 84.57 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS12 86.31 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SD10 86.79 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SN10 85.54 1 - -
    To understand, read the following:
    The strictures, according to Seuss
    Data::Dumper
    constant
    perldata

      Cool. Just what the Doctor Ordered.

      Next Question: How do I exclude certain lines. eg exclude lines that have 'Unk'

        Just don't push if $hash{REG} eq 'Unk'...
      while (<DATA>) { my @line = split; my %hash; @hash{+KEYS} = @line; push @array, {%hash}; }

      Why create a hash in the lexical scope of the while loop and then make a copy of that hash instead of just using a reference to that lexically scoped hash?

      while (<DATA>) { my %hash; @hash{ +KEYS } = split; push @array, \%hash; }
        You are absolutely right. I was typing too fast :)
Re: hash_reference from file
by Anonymous Monk on Sep 29, 2011 at 09:48 UTC
    #!/usr/bin/perl -- #~ 2011-09-29-02:50:12PDT by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use Data::Dumper(); Main( @ARGV ); exit( 0 ); sub Main { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=928 +496;part=1 my $raw = <<'__RAW__'; CORDR KZN MTC-N AINT 2011-09-29 09:00 DTL PM05 83.79 4 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR20 85.39 3 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA BR21 86.06 1 - - CORDR Unk MTC-N AINT 2011-09-29 09:00 JSA BR22 84.55 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RN10 86.97 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS11 84.57 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA RS12 86.31 2 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SD10 86.79 1 - - CORDR SGC MTC-N AINT 2011-09-29 09:00 JSA SN10 85.54 1 - - __RAW__ while( $raw =~ /^(.*)$/gm ){ my $line = $1; print DD( Gash( $line ) ); } } ## end sub Main sub DD { Data::Dumper->new([@_])->Useqq(1)->Dump; } BEGIN { my @fieldOrder = ( 'APP', 'REG', 'KPI', 'INTERFACE', 'DATE', 'TIME', 'NODE', 'RAN', 'SUCC_RATE' ); my %fieldWidths = ( APP => 'A5', DATE => 'A10', INTERFACE => 'A4', KPI => 'A5', NODE => 'A3', RAN => 'A4', REG => 'A3', SUCC_RATE => 'A5', TIME => 'A5' ); my $template = join ' x1 ', @fieldWidths{ @fieldOrder }; sub Gash { my %dat; @dat{ @fieldOrder } = unpack $template, $_[0]; return \%dat; } ## end sub Gash } ## end BEGIN
Re: hash_reference from file
by Anonymous Monk on Sep 29, 2011 at 14:02 UTC
    How do I code this in perl

    You don't, just post it on Perlmonks.com and someone will code it for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928496]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-07-03 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (48 votes), past polls