I have some data as such :
TYPE: Travel
ADDRESS
Barcelona
Paris
So, address can be 1 or many (I need to discard ADDRESS and get only those cities). For some reason my parsing fails (only "ADDRESS" is printed) to produce the correct result.Am i missing something ?
elsif (/^ADDRESS/) {
my @address_t = split /[no matter what i put,only ADDRESS is printed]+/, $_;
shift @address_t; #is this how i will discard ADDRESS ?
foreach my $address (@address_t) {
@address_names = ($address);
}
I think the regex is suppose to be split a newline, space ?
This is how i processed TYPE:
elsif (/^TYPE/) {
my @type_t = split '\s', $_;
$type = $type_tmp[1];
print "$type" ; #to test, but i have a hashmap which i load them in and print at the end of the file.
Thanks
Try something like this:
It will print lines if the previous line matches /^ADDRESS/
. Let me know if there's a point at which you want to stop, and I can adjust...
use warnings;
use strict;
my $current_line = "";
my $line_count = 0;
while (<IN>){
chomp;
my $previous_line = $current_line;
$current_line = $_;
if ($previous_line =~ /^ADDRESS/ or $line_count > 0 ){
$line_count++;
print "$current_line\n"
}
}
use warnings;
use strict;
while(<DATA>) {
if (/^ADDRESS/) { # if line contains ADDRESS then read addresses
while (<DATA>) { # ... in a loop
last if !/^ +/; # until we find a non-indented line
print $_; # here you can push $_ to a list
}
}
if ($_ && /^TYPE/) { # a TYPE after address can be processed now
# stuff
}
}
__DATA__
TYPE: Travel
ADDRESS
Barcelona
Paris
TYPE: Travel
ADDRESS
Barcelona
Paris
Produces:
Barcelona
Paris
Barcelona
Paris
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.