Solved

Perl range operator and printing of array

Posted on 2013-10-24
5
363 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

895 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

11 Experts available now in Live!

Get 1:1 Help Now