Solved

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

10 Experts available now in Live!

Get 1:1 Help Now