Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

arrays and dot operator

by rkappler (Initiate)
on Apr 05, 2017 at 13:56 UTC ( [id://1187118]=perlquestion: print w/replies, xml ) Need Help??

rkappler has asked for the wisdom of the Perl Monks concerning the following question:

Perl noob, coming in from Python and Bash. Reading the llama book, in the bit about arrays, and noted something...unexpected.
@array = 5..9; # this section works as expected print @array; # 56789 print "\n"; $fred = pop(@array); print $fred; # 9 print "\n"; print @array; # 5678 print "\n"; @new_array = 1..5; # this section does not print @new_array . "\n"; # 5 $barney = pop(@new_array); print $barney . "\n"; # 5 print @new_array . "\n"; # 4 print "\n";
In the first section I'm getting the contents of the array and in the second the size of the array, seemingly based on how I'm using the newlines. Is this some odd anomaly or intended functionality? Am I missing something? regards, Richard

Replies are listed 'Best First'.
Re: arrays and dot operator
by vrk (Chaplain) on Apr 05, 2017 at 14:06 UTC

    It's not a newline problem. In your second section, the line print @new_array . "\n" has @new_array in scalar context, because the string concatenation operator (.) puts its arguments in scalar context. The line is actually executed as print scalar(@new_array) . "\n". In scalar context, you get the array length, which is 5 in this case.

    If you want to print a newline after the array, just use a comma:

    print @new_array, "\n";
      Or use:
      #print "@array,\n"; print "@array\n";

      This will print the array elements separated by the contents of $" (space by default), followed by the newline.

      Update: Removed comma as indicated by vrk

      Bill

        You have an extra comma there.

Re: arrays and dot operator
by haukex (Archbishop) on Apr 05, 2017 at 14:20 UTC

    vrk has already answered your question, I just wanted to give a tip: don't use arrays of numbers to test things like this, otherwise it's hard to tell when the array in scalar context is returning the size of the array.

    my @new_array = 'a'..'e'; print @new_array . "\n"; # "5" print pop(@new_array) . "\n"; # "e" print @new_array . "\n"; # "4" print @new_array , "\n"; # "abcd"
Re: arrays and dot operator
by thanos1983 (Parson) on Apr 05, 2017 at 14:22 UTC

    Hello rkappler,

    Welcome to the community, regarding the non expecting behavior the monks already provided you with wisdom.

    Just another idea on printing an array and complex data, I use Data::Dumper:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @new_array = 1 .. 5; print Dumper \@new_array; __END__ $ perl test.pl $VAR1 = [ 1, 2, 3, 4, 5 ];

    Take a look also here (How do I output each Perl array element sourrounded in quotes?).

    Hope this helps.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: arrays and dot operator
by huck (Prior) on Apr 05, 2017 at 14:02 UTC

    print @new_array . "\n"; uses @new_array in a scalar reference

    print @new_array , "\n"; uses @new_array in an "wantarray" reference

Re: arrays and dot operator
by rkappler (Initiate) on Apr 05, 2017 at 14:11 UTC
    I understand (or at least think I do, off to run some test scripts to be sure). Either I missed that bit in the book or it wasn't (yet?) addressed. Thanks for the help!!!
Re: arrays and dot operator
by rkappler (Initiate) on Apr 05, 2017 at 14:01 UTC
    Forgot to add... using perl 5.14 on Win10 - work box, not by choice :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found