in reply to Re^4: Reading multiple lines from text file
in thread Reading multiple lines from text file
Since the key is always the last word on the line, you can get it by using split (which defaults to splitting on whitespace) and then index the resulting list with [-1] to get the last word (if any). You can then push the key onto an array for later use:
use strict; use warnings; use Data::Dump; my $first = 'dev'; my $last = 'manage'; my $in = 0; my @keys; while (<DATA>) { if (defined(my $key = (split)[-1])) { if ($key eq $first .. $key eq $last) { $in = 1; push @keys, $key; } elsif ($in && $key eq 'manage') { push @keys, $key; } else { $in = 0; } } } dd @keys; __DATA__ < as before >
Output:
17:42 >perl 1916_SoPW.pl ( "dev", "dev", "prod", "prod", "prod", "qa", "qa", "qa", "manage", "manage", "manage", "manage", ) 17:42 >
Note: the defined test is needed only if the input file might contain blank lines.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Reading multiple lines from text file
by newperlbie (Acolyte) on Aug 08, 2018 at 08:01 UTC | |
by Athanasius (Cardinal) on Aug 09, 2018 at 07:07 UTC | |
by AnomalousMonk (Archbishop) on Aug 09, 2018 at 13:40 UTC |