Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: using function output ananymous array vs. named array in foreach

by Marshall (Canon)
on Dec 26, 2012 at 15:48 UTC ( [id://1010398]=note: print w/replies, xml ) Need Help??


in reply to Re: using function output ananymous array vs. named array in foreach
in thread using function output ananymous array vs. named array in foreach

chomp returns whatever is chomped off, not the line, so $a, will end up being a bunch of delimiters.

Well, actually chomp() returns the number of "characters" that were chomped - not the delimiters. So the OP's code is not going to work or I will admit that I am having a hard time understanding what this thing it supposed to do.

A short demo of chomp() on my Windows machine:

#!usr/bin/perl -w use strict; #chomp demo #running on Windows XP 32 bit... # chomp... "It removes any line ending that corresponds # to the current value of $/ (also known as # $INPUT_RECORD_SEPARATOR in the English module)." # # chomp() returns the total number of "characters" removed. # The "number of characters" depends.... my $count; print "Count\t Text\n"; my $x ="blah...with CRLF (Windows)\n"; $count = chomp ($x); print "$count \t\t $x\n"; print "setting \$/ to \\r\n"; $/ = "\r"; my $y ="blah...with CR (like Old Mac)\r"; $count = chomp ($y); print "$count \t $y\n"; print "This won't work...An \"extra new line\" remains...\n"; my $z ="blah again with CRLF (Windows)\n"; $count = chomp ($z); print "$count \t\t $z\n"; print "set \$/ back to \\n \n"; $/ = "\n"; $z ="blah again with CRLF (Windows) but 2 CRLF's\n\n"; $count = chomp ($z); print "only one of the \\n's is chomped\n"; print "$count \t\t $z\n"; print "end of demo\n"; __END__ Count Text 1 blah...with CRLF (Windows) setting $/ to \r 1 blah...with CR (like Old Mac) This won't work...An "extra new line" remains... 0 blah again with CRLF (Windows) set $/ back to \n only one of the \n's is chomped 1 blah again with CRLF (Windows) but 2 CRLF's end of demo

Replies are listed 'Best First'.
Re^3: using function output ananymous array vs. named array in foreach
by BrowserUk (Patriarch) on Dec 26, 2012 at 17:18 UTC
    actually chomp() returns the number of "characters" that were chomped - not the delimiters.

    You're right of course. (But your over elaborate demonstration added nothing to the simple statement of fact.)

    I remember discovering early on that the return from chomp wasn't useful and I've never concerned myself with what non-useful thing it actually returned ever since.

    So the OP's code is not going to work...

    That was the point I was making when I said: "Neither snippet will do what you think it will do.".


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes, I was overly elaborate.
      Yes, I have never used the return value from chomp().
      That's it and I think we agree about that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1010398]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-19 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found