?
Solved

'require' file returns only address of array, not array contents

Posted on 2002-06-21
5
Medium Priority
?
192 Views
Last Modified: 2010-03-05
I have two perl files.  I want the second one to include the code of the first.  Example:

*perl1.pl*
@array = ([1, 2, 3],
          [4, 5, 6]);

*perl2.pl*
require 'perl1.pl';
print @array;


When perl2.pl is executed, the print statement returns ARRAY(0x1abf0cc) instead of the values in the array.  Is there any way to use arrays in this fashion?  $scalars work as expected.
0
Comment
Question by:macleod999
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 7100063
print "@$_\n" for @array;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 7101458
The 'require' makes no difference.
Just print as you would have in perl1.pl.
For example
    print "One value: $array[0][1]\n";
    print "Second array: ";
    print join ", ", @{$array[1]};
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7103046
# variation of prvious suggestion:
grep((print "@$_\n"),@array);
0
 
LVL 5

Accepted Solution

by:
Sapa earned 1500 total points
ID: 7113963
macleod999,

at first, using this constructor you have got the
array of arrays, not plain array.

@array = ([1, 2, 3], [4, 5, 6]);

now @array contain two elements, each of them is the
reference to 3-element array. Such arrays of arrays
cannot be used as argument for print. You should write
something like:

foreach my $aref (@array) {
  # now $aref contains reference to inner 3-element array.
  print join(', ', @$aref), "\n";
  # print all elements of inner array separated by comma.
  # @$aref means 'dereference' of array reference.
}

and at second, you shoud no use 'require' for include
plain perl code from another file. Use:

do "perl1.pl";

there are several differences between 'require' and 'do'.
For example, 'require' cannot be called twice at the
same file, and 'require' will raise error if the 'included' script returns false.

Andrey
0
 

Author Comment

by:macleod999
ID: 7115269
Thanks everyone; yeah, there were a few things I was doing wrong...
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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…
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
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

593 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