?
Solved

printing contents of an array

Posted on 2004-11-06
1
Medium Priority
?
165 Views
Last Modified: 2010-03-05
i'm trying to print the contents of an array but i keep getting errors:
here is my code:
#!/usr/bin/perl
use strict;
use warnings;
my(@x,@y,@z);
while(<>)
{
      next unless /^ATOM/;
      my @flds = split;
      push @x, $flds[6];
      push @y, $flds[7];
      push @z,$flds[8];
}
for($i = 0; $i< @x;++$i)
{
      print $x[$i]"\n";
}

the error i'm getting is:
Global syhsmbol "$i" requires explcit package name
what do i need to do to solve this?
0
Comment
Question by:vonster
1 Comment
 
LVL 18

Accepted Solution

by:
kandura earned 100 total points
ID: 12514411
you need to localise the variable:

    for(my $i=0 ...

However, a more perlish way to write that would be

    foreach my $element(@x) {
        print $element, "\n";
    }

although I would shorten that to:

    print $_,$/ foreach @x;

or this is even cooler:

    {
        local $, = "\n";
        print @x;
    }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

840 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