in reply to extracting strings from text file
Another way, similar to the OPed and LanX's solutions:
Please note that:c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use autodie; ;; my $file = qq{(Special)\n} . qq{TG: VIN:1000000\n} . qq{type: a very special type of plane\n} . qq{\n} . qq{(Special)\n} . qq{TG: VIN:1000001\n} . qq{type: a very special type of car\n} . qq{\n} . qq{ \t \n} . qq{(Special)\n} . qq{TG: VIN:1000002\n} . qq{type: a very special type of boat\n} ; ;; open my $fh, '<', \$file; ;; my $rx_vin = qr{ \d+ }xms; my $rx_text = qr{ \S (?: \s* \S+)* }xms; ;; my %hash; ;; local $/ = ''; while (my $block = <$fh>) { my $extracted = my ($vin, $text) = $block =~ m{ \A \s* \Q(Special)\E \s+ TG: \s+ VIN: \s* ($rx_vin) \s+ type: \s+ ($rx_text) \s* \Z }xms; die qq{bad block '$block'} unless $extracted; die qq{duplicate vin '$vin'} if exists $hash{$vin}; ;; $hash{$vin} = $text; } ;; close $fh; ;; dd \%hash; " { 1000000 => "a very special type of plane", 1000001 => "a very special type of car", 1000002 => "a very special type of boat", }
Give a man a fish: <%-{-{-{-<
|
|---|