Work with multiple capture groups in multiple records

Data format:

attribname: data 

Sample data:

 cheese: good pizza: good bagel: good fire: bad 

The code:

 my $subFilter='(.+?): (.+)'; my @attrib = ($dataSet=~/$subFilter/g); for (@attrib) { print "$_\n"; } 

Code spits out:

 cheese good pizza good [etc...] 

I was wondering what is the easiest way for Perly to do this? I am parsing the data from the log, the data above is garbage for simplicity. I'm new to Perl, I suspect I can do this with fanatical indexes, but I was wondering if there is a short way to implement this? Is there a way for capture groups to fit into two different variables instead of sequentially adding to the list along with all matches?

Edit: I want the attribute and its associated value to match so that I can do what I need. For example, if inside the for for loop I could access both the attribute name and the attribute value.

Edit:

I tried

 my %attribs; while (my $line = <$data>) { my ($attrib, $value) = ($line=~m/$subFilter/); print $attribs{$attrib}," : ", $value,"\n"; } 

and no luck :( I do not get any output with this. My data is in a variable, not in a file, because it is parsed from the set of parent data that is in the file. It would be convenient if my variable worked so that my (@attrib, @value) = ($line=~/$subFilter/g); populated the lists accordingly with a few matches.

Decision:

 my @line = ($7 =~/(.+?)\n/g); for (@line) { my ($attrib, $value) = ($_=~m/$subFilter/); if ($attrib ne "") { print $attrib," : ", $value,"\n"; } } 
+1
perl
Jul 01 '10 at 17:02
source share
2 answers

I don’t quite understand what you really want to save, but here is how you can store data in a hash table, and β€œ1” indicates β€œgood” and β€œ0” indicates β€œbad”:

 use strict; use warnings; use Data::Dumper; my %foods; while (my $line = <DATA>) { chomp $line; my ($food, $good) = ($line =~ m/^(.+?): (.+)$/); $foods{$food} = ($good eq 'good' ? 1 : 0); } print Dumper(\%foods); __DATA__ cheese: good pizza: good bagel: good fire: bad 

Fingerprints:

 $VAR1 = { 'bagel' => 1, 'cheese' => 1, 'fire' => 0, 'pizza' => 1 }; 
+2
Jul 01 '10 at 17:18
source share

A reasonable approach would be to use the split function:

 my %attrib; open my $data, '<', 'fileName' or die "Unable to open file: $!"; while ( my $line = <$data> ) { my ( $attrib, $value ) = split /:\s*/, $line, 2; $attrib{$attrib} = $value; } close $data; foreach my $attrib ( keys %attrib ) { print "$attrib: $attrib{$attrib}\n"; } 

If you are in a single line space, the following will be achieved:

 $ perl -F/:\s*/ -ane '$attrib{$F[0]} = $F[1]; } END { print $_,"\t",$attrib{$_},"\n" foreach keys %attrib;" fileName 
0
Jul 02 2018-10-02T00:
source share



All Articles