#!/usr/bin/perl -w use strict; use Data::Dumper; for(grep {/\w/}){ # g repeats the regex, e executes the Perl in the substitution, # returns the number of matches into the condition. # $1 and $2 are the bracketed matches in the regex in order. unless( s/^(^[a-z ]+) *: *([\w\d]+)/$_{$1}=$2/ige ){ chomp; if (/:/){ # key part $_{$_}=join(',',@_); # make new key item @_=() } else { # list part push @_,$_ } } } print Dumper(\%_); __DATA__ Graq: Agnostic: Number: 634321 age: 27 hair colour: black height: 73 weight: 123 legs: 2 arms: 2 jameson bells guinness favourite: detests: likes: