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