Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

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

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

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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2022-07-07 13:27 GMT
Find Nodes?
    Voting Booth?

    No recent polls found