Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

how can i create a hash by reading an array

by s_gaurav1091 (Beadle)
on Jul 22, 2005 at 09:50 UTC ( #477155=perlquestion: print w/ replies, xml ) Need Help??
s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, M doubt is that how can i read an array each line and create a hash out of it fo ex my array contains a line EVENT_LOG_EMITTER rtbd:service=EventEmitterService,name=EventLogEmitter,protocol=XML now iwant my hash to contain a key of EVENT_LOG_EMITTER and value of rtbd:service=EventEmitterService,name=EventLogEmitter,protocol=XML

Comment on how can i create a hash by reading an array
Re: how can i create a hash by reading an array
by Tomtom (Scribe) on Jul 22, 2005 at 10:01 UTC
    use strict; my %hash = (); while (<DATA>) { chomp; $hash{$1} = $2 if m/^(.*?)\s+(.*)$/; } print "$_ = $hash{$_}\n" for keys %hash; __DATA__ EVENT_LOG_EMITTER rtbd:service=EventEmitterService,name=EventLogEmitte +r,protocol=XML EVENT_LOG_EMITTER2 rtbd:service=EventEmitterService,name=EventLogEmitt +er,protocol=XML2
Re: how can i create a hash by reading an array
by davorg (Chancellor) on Jul 22, 2005 at 10:05 UTC
    my %hash; while (<SOME_FILE>) { chomp; my ($k, $v) = split /\s+/, $_, 2; $hash{$k} = $v; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: how can i create a hash by reading an array
by Skeeve (Vicar) on Jul 22, 2005 at 11:39 UTC
    my %h= map { chomp; split /\s+/,$_,2 } <>;

    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-12-27 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls