convert string in to hash in perl using split -


$hashdef = "mouse=>jerry, cat=>tom, dog=>spike";  %hash = split /,|=>/,$hashdef;  print "$_=>$hash{$_}" foreach(keys %hash);   mouse=>jerrydog=>spikecat=>tom 

i new perl can 1 explain regular expression inside split function able know | used choice of both still confused

%hash = split /|=>/,$hashdef; 

i ouput

s=>pe=>j=>et=>or=>rm=>,y=>,u=>sm=>og=>d=>oc=>ai=>kt   %hash = split /,/,$hashdef; mouse=>jerry=>cat=>tomdog=>spike=> 

please explain above condition

split's first argument defines separates elements want.

/,|=>/ matches comma (,) or equals sign followed greater-than sign (=>). they're literals here, there's nothing special them.

/|=>/ matches zero-length string or equals sign followed greater-than sign, , splitting on zero-length string splits string individual characters; therefore, in hash, m map o, u map s, etc. appear jumbled in output because hashes don't have definite ordering.

/,/ splits on comma. you're creating hash maps mouse=>jerry cat=>tom , dog=>spike nothing.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -