Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

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

What is your definition of "doesn't work" here?

The goal was a replacement for <FILE>, so anything that doesn't return what <FILE> is buggy. In hangon's favour, I didn't consider autochomping a bug.

my $data = "\n" # 1 . "abc\n" # 2 . "def\n" # 3 . "\n" # 4 . "\n" # 5 . "ghi\n" # 6 . "\n" # 7 . "\n"; # 8 { print("<FILE>\n"); my $lines = 0; open(my $fh, '<', \$data); while (<$fh>) { ++$lines; chomp; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/\n/, \$data)\n"); my $lines = 0; foreach (split(/\n/, $data)) { ++$lines; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/\n/, \$data, -1)\n"); my $lines = 0; foreach (split(/\n/, $data, -1)) { ++$lines; print("[$_]\n"); } print($lines, "\n"); } print("\n"); { print("split(/^/m, \$data)\n"); my $lines = 0; foreach (split(/^/m, $data, -1)) { ++$lines; chomp; print("[$_]\n"); } print($lines, "\n"); }

outputs

<FILE> [] [abc] [def] [] [] [ghi] [] [] 8 <-- This is what <FILE> gives. split(/\n/, $data) [] [abc] [def] [] [] [ghi] 6 <-- Not 8. Failed to emulate <FILE>. split(/\n/, $data, -1) [] [abc] [def] [] [] [ghi] [] [] [] <-- Extra line 9 <-- Not 8. Failed to emulate <FILE>. split(/^/m, $data) [] [abc] [def] [] [] [ghi] [] [] 8 <-- Bingo!

wc -l says 8 as well, by the way.

Perl doesn't do this on my system:

uh, yes it does... $VAR4 = '';. Lines aren't *seperated* by newlines, they are *terminated* by them.


In reply to Re^4: how to open a data file via http by ikegami
in thread how to open a data file via http by mwhiting

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: (1)
As of 2024-04-25 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found