Solved

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

Posted on 2002-06-21
5
171 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
[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
5 Comments
 
LVL 84

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

756 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