Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

simple printing question

by kevind0718 (Scribe)
on Jan 31, 2012 at 23:18 UTC ( [id://951091]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Wise Monks:

I have a very simple print question: The following code does what I need:
@array = 5..9; print " @array \n"; push @array, 10; print "@array \n" ; push @array, qw( fred barney wilma) ; print "@array \n" ;
produces the following, which is what I expected:
C:\dev\learningPerl>perl ex3a.pl 5 6 7 8 9 5 6 7 8 9 10 5 6 7 8 9 10 fred barney wilma C:\dev\learningPerl>
My question is: what is going on in this code?
@array = 5..9; print @array . "\n"; push @array, 10; print @array . "\n" ; push @array, qw( fred barney wilma) ; print @array . "\n" ;
produces the following, which seems strange to me. Certainly not what I expected.
C:\dev\learningPerl>perl ex3a.pl 5 6 9 C:\dev\learningPerl>

Just try to learn Perl and the above seeems strange to me.

Many Thanks for your kind assistance.

Best

KD

Replies are listed 'Best First'.
Re: simple printing question
by jwkrahn (Abbot) on Jan 31, 2012 at 23:33 UTC

    The concatenation operator (.) forces scalar context on its operands and an array in scalar context returns the number of elements in the array.

      crystal clear now

      many thanks

      KD
Re: simple printing question
by InfiniteSilence (Curate) on Jan 31, 2012 at 23:32 UTC

    Your usage is forcing @array into scalar context which returns the number of elements in the array.

    ~linux> perl -e '@array = 5..9; push @array,10; print scalar @array;'
    Same thing. There are some excellent books you should read in order to learn Perl. Trial and error will only get you so far and will contribute to both confusion and bad habits. Read Effective Perl Programming to eradicate these problems sooner than later.

    Celebrate Intellectual Diversity

Log In?
Username:
Password:

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

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

    No recent polls found