Solved

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

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

778 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