Perl String Parsing to Hash -



Perl String Parsing to Hash -

so lets had string.

$my str = "hello how today. oh thats i'm glad happy. thats wonderful; thats fantastic."

i want create hash table each key unique word , value number of times appears in string i.e., want automated process.

my %words { "hello" => 1, "are" => 2, "thats" => 2, "thats" => 1 };

i brand new perl , have no clue how this, how handle punctuation etc.

update:

also, possible use

split('.!?;',$mystring)

not syntax, split @ . or ! or ? etc.. oh , ' ' (whitespace)

one simple way split string on character not valid word-character in view. note no means exhaustive solution is. have taken limited set of characters.

you can add together valid word-characters within brackets [ ... ] find border cases. might search http://search.cpan.org modules designed purpose.

the regex [^ ... ] means match character not within brackets. \pl larger subset of letters, , others literal. dash - must escaped because meta character within character class bracket.

use strict; utilize warnings; utilize data::dumper; $str = "hello how today. oh thats i'm glad happy. thats wonderful; thats fantastic."; %hash; $hash{$_}++ # increment count each field # in loop split /[^\pl'\-!?]+/, $str; # on list splitting string print dumper \%hash;

output:

$var1 = { 'wonderful' => 1, 'glad' => 1, 'i\'m' => 1, 'you' => 2, 'how' => 1, 'are' => 2, 'fantastic' => 1, 'good' => 1, 'today' => 1, 'hello' => 1, 'happy' => 1, 'oh' => 1, 'thats' => 1, 'thats' => 2 };

string perl parsing hash

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -