Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

For one thing, your code doesn't run:

Can't use global $_ in "my" at /tmp/tst line 4, near "my $_ " Execution of /tmp/tst aborted due to compilation errors.

For another, all you're doing with that "map" statement is returning (via $_) the return of the substitution statement ('1') rather than the modified value of $_ - printing "@fields" would be instructive here.

Lastly, you're explicitly splitting $_ inside your loop; changing the iterator variable is thus going to leave $_ empty, which means that your code isn't going to work. Here's a slightly simpler version of your code, both with an explicit iterator and without:

#!/usr/bin/perl -w use strict; for my $foo (<DATA>) { my @fields = grep { s/^\s+|\s+$//g; 1; } split /\|/, $foo; print "$fields[1]\n"; } __END__ 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
#!/usr/bin/perl -w use strict; for (<DATA>) { my @fields = grep { s/^\s+|\s+$//g; 1; } split /\|/; print "$fields[1]\n"; } __END__ 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

The output in both cases is:

Vao Wxr Kvbr Lvbr Pvbr

Update: I just noticed one more thing: you're using "\r" when you're printing - which means that you're going to overwrite every line that you print. This means that you'll only see the last line, plus anything left over (i.e., anything that was past the current last character) from the previous lines. The correct character to use, at leat in the Unix world, is "\n"; for DOS-based systems, "\n\r" is appropriate.


--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

In reply to Re: variable mystery by oko1
in thread 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 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? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-05-23 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (81 votes). Check out past polls.

    Notices?