Solved

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

Posted on 2002-06-21
5
156 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 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

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

20 Experts available now in Live!

Get 1:1 Help Now