perlquestion
jeah
<p>
Thanks very much for the feedback, everyone and sorry about any confusion, I hope my questions aren't trivial. I've 4 further questions on Revised Code Below, thanks very much in advance.</P><p>
1. If I use $in on Lines 6 - 8 and print any elements, the entire lines of the file are returned without being processed (with pipes included). I thought I could substitute $_ with
any variable. Why won't it work here?</p><p>
2. If I use $_ on these lines instead, then I get good results (the specified element on Line 12.</p><p>
3. If I UnComment Lines 9 - 11, a '1' is returned between the last 2 elements in each line. Why is that?</p><p>
4. To clarify myself (on moritz's question), even-numbered elements return nothing, eg. if I type print $fields[0], "\r"; on line 12 in my revised script, I get nothing. I've
found that any even number gives me nothing, it's odd, I thought the 0th element would be 'Baw', the 1st element would be 'Vao' and so on. The pipe is discarded by perl,
so I don't understand why the even-numbered elements are 'skipped', so to print 'Baw', I type print $fields[1], "\r" and to print 'Vao', I type print $fields[3], "\r"</p>
<code>
1 #!/usr/bin/perl -w
2 use strict;
3
4 open my $datfh, '<', 'C:\begperl\my scripts\cdata.txt' or die "Couldn't open file: $!\n";
5
6 foreach my $_ (<$datfh>) {
7 my @fields = (map { s/^\s+//; s/\s+$//, $_ } # eliminates irrelevant spaces
8 split /\|/, $_);
9 # for my $i (0 .. $#fields) {
10 # print $fields[$i], "\n" unless $fields[$i] =~ /\r|\n/;
11 # }
12 print $fields[1], "\r";
13 }
14 close $datfh;
</code>
<p>
My File's data (for testing):
</p><p>
Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789</p><p>
Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782</p><p>
Uaa|Kvbr|805 Test Rd|Zero|This St|MN|17205|3018757203|3012986736</p><p>
Caa|Lvbr|905 Test Rd|Bero|That St|MD|12705|3028887203|3028886736</p><p>
Eaa|Pvbr|311 Zest Rd|Tero|My St|MI|12505|3018757203|3012986736</p><p>
</p>