Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi, I hope my questions aren't trivial, I can't figure out why replacing the default $_ with an arbitrary variable gives different results, my questions follow, hope you can help, thanks :)

I'd also like to know why if I specify even-numbered elements, nothing returns, eg. if I type

print $fields[0], "\r";

on line 12 in my script, I get nothing. I've found that any even-numbered element specified on line 12 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"

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?

2. If I use $_ on these lines instead, then I get good results (eg. I get the element specified on Line 12.

3. If I UnComment Lines 9 - 11, a '1' is returned for each record/line between the last 2 elements from the file. Why is that?

1 #!/usr/bin/perl -w 2 use strict; 3 4 open my $datfh, '<', 'C:\begperl\my scripts\cdata.txt' or die "Could +n't open file: $!\n"; 5 6 foreach my $_ (<$datfh>) { 7 my @fields = (map { s/^\s+//; s/\s+$//, $_ } # eliminates irr +elevant 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;

My File's data (for testing):

Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789

Vca|Wxr|384 Mkl Ln|Xillo|Crrnt Stt|CT|05506|1015567781|1015567782

Uaa|Kvbr|805 Test Rd|Zero|This St|MN|17205|3018757203|3012986736

Caa|Lvbr|905 Test Rd|Bero|That St|MD|12705|3028887203|3028886736

Eaa|Pvbr|311 Zest Rd|Tero|My St|MI|12505|3018757203|3012986736


In reply to variable mystery by jeah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found