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

Re^2: not able to get last index of array

by Lotus1 (Vicar)
on Jun 04, 2016 at 15:36 UTC ( [id://1164917]=note: print w/replies, xml ) Need Help??


in reply to Re: not able to get last index of array
in thread not able to get last index of array

Hi ww,

When I run the OP's script with use warnings; added I don't see a warning for printing "@a" but I do get one for print $a[-2] since there is only one element in the OP's array. Here is the warning I get:

Use of uninitialized value in print at C:\usr\pm\sort\1164677.pl line 7.

When you print an array inside of double quotes it adds a space by default between the elements or whatever the output field list separator, "$," '$"', is set to. How do I print an array with commas separating each element?

Update: I forgot to mention also at the line (my @newstring) = sort {$a <=> $b} @numbers; you don't need the parentheses since defining an array is already in array context. These are nitpicky comments since your program works and answered the OPs questions, but it's my 2 cents worth.

Update: Fixed the separator as pointed out by AnomalousMonk

Replies are listed 'Best First'.
Re^3: not able to get last index of array
by AnomalousMonk (Archbishop) on Jun 04, 2016 at 15:50 UTC
    When you [interpolate] an array inside of double quotes it adds ... the output field separator, "$,", ...

    Array interpolation adds the list separator  $" between elements. See perlvar.


    Give a man a fish:  <%-{-{-{-<

      Thanks AnomalousMonk

      If I had read more closely the node I linked to it explains the difference between $, and $" . I just knew there was a variable involved and grabbed the first one I saw.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found