Solved

Perl range operator and printing of array

Posted on 2013-10-24
5
361 Views
Last Modified: 2013-10-24
I was trying to populate an array with 100 elements having the values 1 through 100.  But I can not print out the contents of the array.  See the actual result I get below.  Am I using the correct sytax?  I am executing this with Strawberry Perl in Win 7.  Thank you for any insight.

@fred=[1..100];
print @fred;

The output:

C:\\perl_data>perl knit8.pl

ARRAY(0x39e6e8)
0
Comment
Question by:amigan_99
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39598852
@fred=(1..100);
print @fred;
#or
@fred=[1..100];
print @{$fred[0]};
0
 
LVL 1

Author Comment

by:amigan_99
ID: 39598921
Your method worked printing all the elements in a ray with no space in between the values.
I tried also print "@fred"; - but this did not work either.
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39599103
@fred=(1..100);
print "@fred";
#or
@fred=[1..100];
print "@{$fred[0]}";
#or
@fred=(1..100);
print "$_\n" for @fred;
0
 
LVL 1

Author Closing Comment

by:amigan_99
ID: 39599188
So that last one worked the best. And the central issue would appear to be using paren to contain the .. defined range as opposed to square brackets.  With the brackets I had the same weird ARRAY(hex) output.  When defined with parens even the simple print "@fred";
works and formats properly.  Thank you.


@fred=(1..100);
print "$_\n" for @fred;
0
 
LVL 84

Expert Comment

by:ozo
ID: 39599199
print "$_\n" for @fred;  # or    $"="\n"; print "@fred\n";
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now