In oko1's modification, grep is being used for side effects. grep returns the values of the list (in this case split result) for which the expression is true (in this case it is 1 for any and every input). Substitutions as a side effect just happen to be before forcing the test to be true.

Do modify your code if you are using perl 5.8.7. Either use an explicit loop variable other than $_ ($in should have worked in you had supplied it to split instead of $_; related code is missing)...

for my $in ( <$fh> ) { my @p = map { ... } split /\|/ , $in; ... }

... or just omit it ...

for ( <$fh> ) { my @p = map { ... } split /\|/ , $_; ... }

As oko1 said that 1 is the return value of s/// on success; false or empty string on failure. Using a comma, you were generating a 2-element list of s/// return value & $_ (as the last expression of map). Unlike oko1's statement about returning empty $_, returning $_ works just fine ...

print join ', ' , map { s/^\s+// ; s/\s+$// ; $_ ; } split /\|/ , 'p | q|r|s | t' ;

In any case, there is no need of a map or a grep with side effects since split can take care of the spaces itself as it takes a regular expression to split on ...

print join ', ' , split /\s*\|\s*/ , 'p | q|r|s | t' ;

There are also Text::CSV* modules for your parsing needs.

Back to your problem ...

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.
# input. Baw|Vao|111 Noa St||NewYork|NY|10012|2123456789|123456789
# code. my @fields = (map { s/^\s+//; s/\s+$//, $_ } split /\|/, $_);

So, after split, map produced ('', Baw) for Baw, ('', Vao) for Vao and so on. The array then would have an empty string at index 0, Baw at 1, empty string at 2, Vao at 3 and so on, so forth. Since you had used \r, you missed the empty strings.


In reply to Re^5: variable mystery by parv
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.