in reply to An (almost) useful idiom; needs work.

In reference to your update the example posted is already generic and does exactly as you suggest with a minute change. Have a look at the Data::Dumper output. You have a hash of arrays keyed on the HEADER|TITLE|COMPND tokens. Each element in the arrays is the concatenated contiguous lines. You would reference one array like @{$hash{HEADER}} or a single element like $hash{HEADER}->[2]

my %hash; my $current_token = ''; $re_types = qr/(HEADER|TITLE|COMPND)/; while (<DATA>) { my ($token, $value ) = $_ =~ m/^$re_types\s+(.*)/; next unless $token; if ( $token eq $current_token ) { ${$hash{$token}}[-1] .= ' ' . $value; } else { $current_token = $token; push @{$hash{$token}}, $value; } } use Data::Dumper; print Dumper \%hash; __DATA__ blah

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: An (almost) useful idiom; needs work.
by BrowserUk (Patriarch) on Dec 22, 2002 at 06:02 UTC

    You still miss the point! Which was to use the inherent state memory of the bistable operator in one of its forms, '..' or '...' to replace the need for using a variable and an if statement to control the capture.

    Saying that you can do it "this other way", doesn't progress the idea.


    Examine what is said, not who speaks.

      Oh well if 12 lines of code that provide an efficient, elegant (if I do say so myself) and generic solution to the problem are worse than a couple of dozen lines that don't work, use cut and paste, parallel arrays and that would require sym refs to access in some sort or scalable fashion.....

      The single variable $token is a metastable operator (if you want to nit pick) and performs far more than a simple flag function as it is also the index key to the hash of arrays.

      In my world problem->solution == happiness

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      A reply falls below the community's threshold of quality. You may see it by logging in.