Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl range operator and printing of array

Posted on 2013-10-24
5
Medium Priority
?
388 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 85

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 85

Accepted Solution

by:
ozo earned 2000 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 85

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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 …
Starting up a Project

972 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