mikejones has asked for the wisdom of the Perl Monks concerning the following question:

My goal is to grab certain string of chars from the output of print on line 23,  which would be anything after = and before , Whether I use a regexp or a hash/value pair I don't care, as long as it works! thank you. :)
__DATA__ msgagt=ESM_WMB_AIX,sec_id=Sec _id,severity=Low,node=test,msgnode=qwmbap01.xxxxxxxxxxxxx.net,utc=2007 +-04-26 18:01:59.472+00:00,om=UID=3a7affd6-f420-11db-80b1-000000000000 +,AlertCod e=AEM001,AlertType=AEM-default,AppName=AEM-CommonService2,Message=5004 +:An error has been reported by the BIPXML4C component.:XML __CODE__ 1 use strict; 2 use warnings; 3 #use diagnostics; 4 use Data::Dumper; 5 6 my $ovo = qq(/home/mikej/out); 7 my $msgagt = qr/(?:\w+\=\w+)/; #|(?:\w+=\{.*?\}))/; 8 my ($key,$value); 9 my (%HoA,@HoA); 10 11 open (my $out, "+<", $ovo) or die "file '$ovo' was not opened $!"; 12 13 while (<$out>) { 14 s/^\s+|\s+$//g; ## rid of newlines at begin and end 15 chomp; 16 next unless length $_; ## skip blanks 17 s/^'|'\w+|'.*?$//; ## rid of ticks and ticks plus metachars +\w 18 s/\x7b|\x7d//g; ## rid of { and } 19 ($key,$value) = (split /=/, $_, 0); 20 #print "KEY:\t",$key,"\n"; 21 #print "VALUE:\t",$value,"\n"; 22 #push @{$HoA{$key}}, $value if (/$msgagt/g); 23 print "$_\n" if (/$msgagt/g); 24 } 25 #for my $i (sort keys %HoA) { 26 # print "$i -- @{ $HoA{$i} }\n"; 27 #}
As another idea, what about setting
local $/ = ',';

Replies are listed 'Best First'.
Re: key value pair or simply a regexp
by thezip (Vicar) on May 18, 2007 at 20:26 UTC

    You're making this more difficult for us because you don't really specify the output you're specifically seeking.

    Your code seems to indicate that you're looking for the associated value for the "msgagt" key (which has the value "ESM_WMB_AIX" in the data you provide). Is this correct?

    My assumption is that this is what you're looking for. Given that,

    my $data = qq(msgagt=ESM_WMB_AIX,sec_id=Sec_id,severity=Low,node=test, +msgnode=qwmbap01.xxxxxxxxxxxxx.net,utc=2007-04-26 18:01:59.472+00:00, +om=UID=3a7affd6-f420-11db-80b1-000000000000,AlertCode=AEM001,AlertTyp +e=AEM-default,AppName=AEM-CommonService2,Message=5004:An error has be +en reported by the BIPXML4C component.:XML); my ($msgagt) = $data =~ /msgagt=([^,]+)/; print $msgagt, "\n";

    Am I missing any other specs for this problem?


    Where do you want *them* to go today?
      No you are not missing anything, and thank you b/c your regexp worked. I asked what I needed in the beginning which was everything between the = and , But if a new string is entered in the front, middle or end such as node1=xxxx, thats why I started out using key/value pairs. thx agn :)
        Was able to get it working! Yea! Thanks to thezip for pointing me in the right direction after a little mod, it works.
        #!/usr/bin/perl use strict; use warnings; my $out = qq(/home/smith/out); open (my $file, "+<", $out) or die $!; local $/ = ','; while (<$file>) { s/^\s+|\s+$//g; my ($msgagt) = $_ =~ /\w+=([^,]+)/; print $msgagt, "\n"; }
Re: key value pair or simply a regexp
by jdporter (Paladin) on May 18, 2007 at 19:15 UTC

    I think you're going to have a problem with the om=UID=3a7affd6-f420-11db-80b1-000000000000 as it contains two equals signs.
    I'd do this:

    my %h = map { ( split /=/, $_, 2 ) } split /,/;

    Then you can stuff the data in %h into your HoA, or just

    print "$h{'msgagt'}\n" if exists $h{'msgagt'};
    or whatever.

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
      PLayed with it a little changing the split from
      my %h = map { ( split /=/, $_, 2 ) } split /,/; to my %h = map { ( split /=/, $_, 1 ) } split /,/; and my %h = map { ( split /=/, $_, 3 ) } split /,/; print "$h{'msgagt'}\n" if exists $h{'msgagt'};
      Output was all the same.
      ESM_WMB_AIX Odd number of elements in hash assignment at parse_4_ovo.plx line 20, +<$out> line 3. Odd number of elements in hash assignment at parse_4_ovo.plx line 20, +<$out> line 7. Odd number of elements in hash assignment at parse_4_ovo.plx line 20, +<$out> line 9. Odd number of elements in hash assignment at parse_4_ovo.plx line 20, +<$out> line 11.
Re: key value pair or simply a regexp
by FunkyMonk (Bishop) on May 18, 2007 at 21:57 UTC
    my (%HoA,@HoA);

    %HoA isn't a very good name for a variable, but it isn't nearly as bad as an array called HoA. Use variable names that describe the data they contain, or perhaps their use, not their type.

    This is the second time today I've seen similarly named variables. Perhaps it was the same monk, I don't know :(

Re: key value pair or simply a regexp
by duff (Parson) on May 18, 2007 at 18:40 UTC

    It sounds like you just want to change your split() to also split on commas like so:

    ($key,$value) = split /[=,]/;
      Close but no cigar. Output is
      KEY: msgagt VALUE: ESM_WMB_AIX KEY: _id VALUE: severity KEY: e VALUE: AEM001 KEY: SM_Integrator.InsertToSYSLOG Use of uninitialized value in print at parse_4_ovo.plx line 22, <$out> + line 7. VALUE: KEY: /DataFlowEngine/ImbRdl/ImbRdlThrowExceptionStatements.cpp: 158 +: SqlThrowExceptionStatement::execute: ComIbmComputeNode: MS_Alert_ES +M_Integrato Use of uninitialized value in print at parse_4_ovo.plx line 22, <$out> + line 9. VALUE: KEY: r#FCMComposite_1_2 Use of uninitialized value in print at parse_4_ovo.plx line 22, <$out> + line 11. VALUE:
      for loop on hash output ########################
      _id -- severity e -- AEM001 msgagt -- ESM_WMB_AIX
      maybe something like this, but even this is not exactly what I need:
      my $data = qq#'msgagt=ESM_WMB_AIX,sec_id=Sec_id,severity=Low,node=test,msgnode=qw +mbap01.cardinalhealth.net,utc={2007-04-26 18:01:59.472+00:00},om={UID=3a7affd6-f420-11db-80b1-000000000000,Alert +Code=AEM001,AlertType=AEM-default,AppName=AEM-CommonService2,Message= +5004:An error has been reported by the BIPXML4C component.:XML}' #; print "$_\n" for $data =~ /((?:\w+=\w+)|(?:\w+=\{.*?\}))/g;
      __OUTPUT__
      msgagt=ESM_WMB_AIX sec_id=Sec_id severity=Low node=test msgnode=qwmbap01 UID=3a7affd6 AlertCode=AEM001 AlertType=AEM AppName=AEM Message=5004