olivier.scalb...@algosyn.com wrote:
> $ cat test.txt
> keyword1 word1, word2
>   word3;
>   blabla
> 
>   blabla
> 
> 
> keyword2
>   word4, word5,
>   word6, word7, word8,
>   word9;
> 
>   bla bla
>   bla bla
> 
> keyword1
>   word10, word11;

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

my $file = shift @ARGV;

my $source;
open my $source_fh, '<', $file or die "could not open $file: $!\n";
{
  local $/;
  $source = <$source_fh>;
}
close $source_fh;

my %keywords;
my @captured = $source =~ m{ ( keyword\d+ ) ( [^;]+ ) \; }gmsx;
while( @captured ){
  my $keyword = shift @captured;
  my $words = shift @captured;
  $words =~ s{ \A \s+ }{}msx;
  $words =~ s{ \s+ \z }{}msx;
  my @words = split m{ \s* \, \s* }msx, $words;
  push @{ $keywords{$keyword} }, @words;
}

print 'keywords: ', Dumper \%keywords;

__END__

-- 
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to