Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

how can i create a hash by reading an array

by s_gaurav1091 (Beadle)
on Jul 22, 2005 at 09:50 UTC ( [id://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

Replies are listed 'Best First'.
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 (Parson) 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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2024-04-16 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found