P
Paul
Hello there. I'm new to Perl and am trying to maintain a large
complex script that generates some reports based on some text input
files. I'm having difficulty understanding a section of the code that
reads in from a text file with 3 columns of data to generate a new
output file with 5 columns of data. I was hoping that someone might
be able to help me understand a few lines that I find a bit complex
right now. Here are the details..
Sample Text input file:
---
"Filename" "Field" "Content"
"file0001.txt" "DESCRIPTION" "blah blah blah"
"file0001.txt" "NAME" "FOO"
"file0002.txt" "NAME" "BAR"
---
Need help understanding what this section of Perl code does:
1: foreach(@list) {
2: ($filenm,$fld_type,$content) = (split /\"/)[1,3,5];
3: $name =~ s/^\s+//;
4: $name =~ s/\s+$//;
5: push(@{$listcontent{"$filenm\{\{\{$fld_type"}},$content) if
($fld_type);
6: }
[snip]
7: foreach(sort keys %listcontent) {
8: ($newfilename, $fld_type) = split /\{\{\{/;
[...]
To start, @list is an array that holds the contents of the input file
(sans the header) like so:
["\"file0001.txt\"\t\"DESCRIPTION\"\t\"blah blah blah\"\n",
"\"file0001.txt\"\t\"NAME\"\t\"FOO\"\n",
"\"file0002.txt\"\t\"NAME\"\t\"BAR\"\n"]
What I know so far...
- line 1 - 6 iterates through each row in the @list array. ok.
- line 2 pulls out the content from each row into 3 variables. ok.
- lines 3 and 4 strip off the leading and trailing white space. ok.
- line 5. No idea. =(
--> Guess: it looks like "listcontent" is an array that now holds some
new arrangement of the data pulled out of the other array. I don't
get what the open curly braces are doing. I tried to "print" this
array after it ran but I didn't understand what I was seeing. It
looked like some kind of convoluted programming mess so I thought I
must have done something wrong.
-> Can anyone please tell me what this new array looks like, or help
me understand this line of code?
- line 7 starts a new loop iterating through the new array...
-> What does "sort keys" do? Well, okay, I know what 'sort' is, but I
don't get the 'keys' part. It's not a subroutine in this script, so
what's it sorting on/by here?
- line 8 pulls out the content from each row in "listcontent" into 2
new variables. I recognize the "\{\{\{" from line 5, but I don't get
how this is getting separated here. i.e. I don't know what
$newfilename looks like.
- I suppose if I understand line 5 I'll have a better chance of
understanding line 8.
Can anyone please help? Thanks in advance.
Paul.
complex script that generates some reports based on some text input
files. I'm having difficulty understanding a section of the code that
reads in from a text file with 3 columns of data to generate a new
output file with 5 columns of data. I was hoping that someone might
be able to help me understand a few lines that I find a bit complex
right now. Here are the details..
Sample Text input file:
---
"Filename" "Field" "Content"
"file0001.txt" "DESCRIPTION" "blah blah blah"
"file0001.txt" "NAME" "FOO"
"file0002.txt" "NAME" "BAR"
---
Need help understanding what this section of Perl code does:
1: foreach(@list) {
2: ($filenm,$fld_type,$content) = (split /\"/)[1,3,5];
3: $name =~ s/^\s+//;
4: $name =~ s/\s+$//;
5: push(@{$listcontent{"$filenm\{\{\{$fld_type"}},$content) if
($fld_type);
6: }
[snip]
7: foreach(sort keys %listcontent) {
8: ($newfilename, $fld_type) = split /\{\{\{/;
[...]
To start, @list is an array that holds the contents of the input file
(sans the header) like so:
["\"file0001.txt\"\t\"DESCRIPTION\"\t\"blah blah blah\"\n",
"\"file0001.txt\"\t\"NAME\"\t\"FOO\"\n",
"\"file0002.txt\"\t\"NAME\"\t\"BAR\"\n"]
What I know so far...
- line 1 - 6 iterates through each row in the @list array. ok.
- line 2 pulls out the content from each row into 3 variables. ok.
- lines 3 and 4 strip off the leading and trailing white space. ok.
- line 5. No idea. =(
--> Guess: it looks like "listcontent" is an array that now holds some
new arrangement of the data pulled out of the other array. I don't
get what the open curly braces are doing. I tried to "print" this
array after it ran but I didn't understand what I was seeing. It
looked like some kind of convoluted programming mess so I thought I
must have done something wrong.
-> Can anyone please tell me what this new array looks like, or help
me understand this line of code?
- line 7 starts a new loop iterating through the new array...
-> What does "sort keys" do? Well, okay, I know what 'sort' is, but I
don't get the 'keys' part. It's not a subroutine in this script, so
what's it sorting on/by here?
- line 8 pulls out the content from each row in "listcontent" into 2
new variables. I recognize the "\{\{\{" from line 5, but I don't get
how this is getting separated here. i.e. I don't know what
$newfilename looks like.
- I suppose if I understand line 5 I'll have a better chance of
understanding line 8.
Can anyone please help? Thanks in advance.
Paul.