Solved

Perl range operator and printing of array

Posted on 2013-10-24
5
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
Part One of the two-part Q&A series with MalwareTech.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

632 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