Hello,
I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite work out the syntax. Here's what I have so far. As you can see, my understanding of Perl falls apart when I get to the subroutine. Any help would make my day. Thanks. -
my @fruitFiles =("apple.html", "orange.html", "kiwi.html");
-
my @vegetableFiles =("pea.html", "carrot.html", "limabean.html");
-
my @breadFiles =("rye.html", "wheat.html", "sevenGrain.html");
-
-
-
#Make references to arrays.
-
my $fruitArray = \@fruitFiles;
-
my $vegArray = \@vegetableFiles;
-
my $breadArray = \@breadFiles;
-
-
-
# Put references into an array to make a two-dimensional array.
-
@categoryArrays = ($fruitArray, $vegArray, $breadArray);
-
-
#Proof of concept, debug.
-
for my $i (0..$#categoryArrays) {
-
for my $j (0..$#{$categoryArrays[$i]}) {
-
print "\$i = $i, \$j = $j, $categoryArrays[$i][$j]\n";
-
}
-
}
-
-
-
for my $q (0..$#categoryArrays) {
-
#pass each array to subroutine.
-
&buildList ($categoryArrays[$q]);
-
}
-
-
-
sub buildList($catArray)
-
{
-
@locArray = $catArray;
-
foreach $element (@locArray)
-
{
-
print $element;
-
print "\n";
-
# I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
-
# I think I can manage that part if I can just reference them correctly down here.
-
}
-
-
}
-
-
6 3675
Hello,
I've made a two dimensional array using references, which I gather is the only way to do it in Perl. I now need to pass each interior array to a subroutine for processing and can't quite work out the syntax. Here's what I have so far. As you can see, my understanding of Perl falls apart when I get to the subroutine. Any help would make my day. Thanks. - #Make references to arrays.
-
my $fruitArray = \@fruitFiles;
-
my $vegArray = \@vegetableFiles;
-
my $breadArray = \@breadFiles;
-
-
-
# Put references into an array to make a two-dimensional array.
-
@categoryArrays = ($fruitArray, $vegArray, $breadArray);
-
-
for my $q (0..$#categoryArrays) {
-
#pass each array to subroutine.
-
&buildList ($categoryArrays[$q]);
-
}
-
-
-
sub buildList($catArray)
-
{
-
@locArray = $catArray;
-
foreach $element (@locArray)
-
{
-
print $element;
-
print "\n";
-
# I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
-
# I think I can manage that part if I can just reference them correctly down here.
-
}
-
-
}
-
-
All right, so inside buildList, we assume that you were passed in a reference to an array. (I'm not sure what the parentheses syntax is you've used - I haven't come across that in my studies). So @_ (the array with all of the arguments passed to the current subroutine) should have 1 element - the reference. You want locArray to be the array that is referenced - I can see that, because you use a foreach loop to access all members. But what you have now:
means @locArray will be an array with 1 element - the reference you were passed in. If you want to get the array, you need to dereference the reference: - @locArray = @{$catArray};
Now @locArray is an array with the same contents as the array referenced by $catArray.
As an aside, why aren't you using my to declare these variables as lexicals? i.e. shouldn't it be: - sub buildList($catArray)
-
{
-
# I think this should actually be:
-
# sub buildList
-
# {
-
# my @locArray = @{$_[0]};
-
my @locArray = @{$catArray};
-
foreach my $element (@locArray)
-
{
-
print $element;
-
print "\n";
-
}
-
-
}
You might have perfectly good reasons for doing so that I don't understand (after all, I'm taking my first class in Perl right now, so I don't have a lot of experience).
I did not go over all the code, but change the buildList function to this: - sub buildList
-
{
-
my $locArray = shift; # imports the data into the function
-
foreach $element (@{$locArray})
-
{
-
print $element;
-
print "\n";
-
# I actually want to open all of the files in the passed array, manipulate and concatenate bits of them into an output file.
-
# I think I can manage that part if I can just reference them correctly down here.
-
}
-
-
}
Ask questions if you have any.
Thank you both! Kevin, yours worked well; Gannon, thanks for the tips and thoughtful walk-through. I never took coursework in Perl so I have gaping holes in my knowledge and, I forget e.g., about "my".
Can either of you tell me how in the subroutine I can retrieve the name of the array variable I passed in? I need to be able to refer to the literal, "fruitArray".
Thanks.
John
You could add its name to the front of the actual array, or pass it into the subroutine as an additional variable. Otherwise, there is no way - remember that a variable's name is only for us programmers to comprehend and has nothing to do with the actual value that gets passed around.
Thank you both! Kevin, yours worked well; Gannon, thanks for the tips and thoughtful walk-through. I never took coursework in Perl so I have gaping holes in my knowledge and, I forget e.g., about "my".
Can either of you tell me how in the subroutine I can retrieve the name of the array variable I passed in? I need to be able to refer to the literal, "fruitArray".
Thanks.
John
You need to rethink your premise, there should never be a need to retrieve an array name in a perl program. You could pass it as a string to your function as Ganon11 suggests.
Gotcha. Thank you both again.
John
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dzieciou |
last post by:
I've used JENA and got the following result in result of query to RDF
file:
<j.0:ResultSet>
<j.0:solution rdf:parseType="Resource">
<j.0:binding rdf:parseType="Resource">
<j.0:value>John...
|
by: Catherine Lynn Wood |
last post by:
I need to know how to overlap DIV content within 'relative' associated
rendering. I am building div layers in the middle of a page and when I set
positioning to absolute in the CSS, it references...
|
by: Victor Bazarov |
last post by:
In the project I'm maintaining I've seen two distinct techniques used for
returning an object from a function. One is
AType function(AType const& arg)
{
AType retval(arg); // or default...
|
by: Dennis Myrén |
last post by:
I have these tiny classes, implementing an interface through which their
method
Render ( CosWriter writer ) ;
is called.
Given a specific context, there are potentially a lot of such objects,...
|
by: LP |
last post by:
Hello,
Here's the scenario:
Object A opens a Sql Db connection to execute number of SqlCommands. Then it
needs to pass this connection to a constructor of object B which in turn
executes more...
|
by: Arne |
last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart
from a pocket PC to a webservice.
What is the right datatype?
II have tried dataset as a datatype, but I can't get it to...
|
by: Stan |
last post by:
There are two ways to pass structured data to a web service:
xml
===
<Order OrderId="123" OrderAmount="234" />
or class
=====
|
by: quat |
last post by:
I have two unmanaged pointer in a managed form class:
IDirect3D9* d3dObject;
IDirect3DDevice9* d3dDevice;
In a member function of the form, I call:
d3dObject->CreateDevice(...
|
by: Andrew Bullock |
last post by:
Hi,
I have two classes, A and B,
B takes an A as an argument in its constructor:
A a1 = new A();
B b = new B(a1);
|
by: AMP |
last post by:
Hello,
I have this in form1:
namespace Pass
{
public partial class Form1 : Form
{
public Form2 form2;
public Form1()
{
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |