Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

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

While working on an algorithm for finding polynomials to fit a bunch of points (which you can see on my scratchpad), I tripped across this little syntax quirk and wondered this behavior is expected, or possibly a bug in my perl (version 5.8.7, the stock cygwin build).

Namely, I found that this code:

use strict; my $a = [ qw(mary had a little lamb with mint jelly) ]; my $b = [ $a->[1..4] ]; my $c = [ @{$a}[1..4] ]; print "a is [ @$a ]\n"; print "b is [ @$b ]\n"; print "c is [ @$c ]\n";
produces this output:
a is [ mary had a little lamb with mint jelly ] b is [ mary ] c is [ had a little lamb ]

That is, $b is equal to [ $a->[0] ]. I cannot imagine how perl's syntax rules got there. Apparently, 1..4 is being evaluated in scalar context (despite the expression $a->[1..4] clearly being evaluated in a list context), and the result of scalar(1..4) is numerically equal to 0. This struck me as bizarre; I would have thought scalar(1..4) would be 4, not ''.

Is anyone else startled by this behavior?

--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Is this a bug, or expected behavior? by fizbin

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 chilling in the Monastery: (3)
As of 2024-04-16 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found