in reply to Extract a small part of a long sentence using regular expressions

You can use a bit more sophisticated regex to capture the numbers and commas in the parentheses. Then split them on commas, grep for non-zeroes, and use a hash slice to populate the key-value pairs in one step:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; while (<DATA>) { next unless /action \( ([0-9,]+ ) \) /x; my @args = grep $_, split /,/, $1; my %hash; @hash{ ('a' .. 'z')[0 .. $#args] } = @args; print Dumper \%hash; } __DATA__ blah blah [AHB_REPORTER][INFO]: action(62,1,0,0,0,0,5,53,9,0,190)D:/XYZ/reg/Test +s/Mcu/A_test.cCALL: (null)

Output:

$VAR1 = { 'e' => '9', 'c' => '5', 'a' => '62', 'b' => '1', 'd' => '53', 'f' => '190' };
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Extract a small part of a long sentence using regular expressions
by Anonymous Monk on Dec 02, 2014 at 16:06 UTC
    Thank you! I tried your method but somehow 2 things go wrong.

    1. I am unable to sort the keys in the right order.

    2. I keep getting 2 hash dumpers, both containing the same key-value pairs only in different orders!

      1. Hash keys are not sorted. Check $Data::Dumper::Sortkeys in Data::Dumper.
      2. Your data probably contain two matching lines.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Sorry, my bad. Point 1: duly noted and Point 2: I take it back. I overlooked the fact that i have multiple match cases. Too much of Perl for today i suppose!
Re^2: Extract a small part of a long sentence using regular expressions
by fionbarr (Friar) on Dec 02, 2014 at 21:01 UTC
    hi....elegant solution but please: where does the array hash figure....I see the %hash declaration but how is the array sigil used?
      It's called a hash slice. See Slices in perldata. The @ sigil just means plural, as -s in English, it doesn't necessarily mean "array".
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      { 'hashes', 'are', 'curly', 'ones' }

      [ 'arrays', 'are', 'square' ]

      ( 'lists are round' )

      my %foo; @foo{ 'hash', 'slices' } = ( 'use', 'curly braces' );

      my @bar; @bar[ 1,2,3,4 ] = ( 'array', 'slices', 'use', 'square brackets' );

      See also References quick reference