P
P
Hi,
I have an input file which looks like this:
aaaaaaa1
bb1
ccc1
ccc2
ccc3
bb2
ccc4
ccc5
ccc6
aaaaaaa2
bb3
ccc7
ccc8
ccc9
bb4
ccc10
ccc11
ccc12
bb5
ccc13
ccc14
ccc15
bb6
ccc16
ccc17
ccc18
aaaaaaa3
....
and so on. The letters are the same for the same indent
level. They don't mean that the data there is the same. I am
trying to do the following: as long as the indent level gets
deeper, I want to combine the lines into strings like this:
aaaaaaaa1/bb1/ccc1
aaaaaaaa1/bb1/ccc2
aaaaaaaa1/bb1/ccc3
aaaaaaaa1/bb2/ccc4
....
aaaaaaaa2/bb3/ccc7
and so on.
I have tried the following:
+start_of_code
#!/usr/bin/perl
use warnings;
use strict;
my $prev_indent = -1;
my $curr_wanted_string = '';
while (<DATA>) {
chomp;
my $curr_indent = length $1 if m/^(\s*)/;
s/^\s*|\s*$//;
if ( $curr_indent > $prev_indent ) {
$curr_wanted_string .= "$_/";
$prev_indent = $curr_indent;
}
else {
print "[$curr_wanted_string]\n";
$curr_wanted_string = '';
}
}
__DATA__
The above input here (didn't want to make post too long).
- end_of_code
The result, though, is that I get only the first string and
then many empty strings:
[aaaaaaa1/bb1/ccc1/]
[]
[]
[]
....
Can you please help me fix this?
Thank you,
Angie
I have an input file which looks like this:
aaaaaaa1
bb1
ccc1
ccc2
ccc3
bb2
ccc4
ccc5
ccc6
aaaaaaa2
bb3
ccc7
ccc8
ccc9
bb4
ccc10
ccc11
ccc12
bb5
ccc13
ccc14
ccc15
bb6
ccc16
ccc17
ccc18
aaaaaaa3
....
and so on. The letters are the same for the same indent
level. They don't mean that the data there is the same. I am
trying to do the following: as long as the indent level gets
deeper, I want to combine the lines into strings like this:
aaaaaaaa1/bb1/ccc1
aaaaaaaa1/bb1/ccc2
aaaaaaaa1/bb1/ccc3
aaaaaaaa1/bb2/ccc4
....
aaaaaaaa2/bb3/ccc7
and so on.
I have tried the following:
+start_of_code
#!/usr/bin/perl
use warnings;
use strict;
my $prev_indent = -1;
my $curr_wanted_string = '';
while (<DATA>) {
chomp;
my $curr_indent = length $1 if m/^(\s*)/;
s/^\s*|\s*$//;
if ( $curr_indent > $prev_indent ) {
$curr_wanted_string .= "$_/";
$prev_indent = $curr_indent;
}
else {
print "[$curr_wanted_string]\n";
$curr_wanted_string = '';
}
}
__DATA__
The above input here (didn't want to make post too long).
- end_of_code
The result, though, is that I get only the first string and
then many empty strings:
[aaaaaaa1/bb1/ccc1/]
[]
[]
[]
....
Can you please help me fix this?
Thank you,
Angie