[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

passing a 2D array.

Posted on 2008-11-03
4
Medium Priority
?
667 Views
Last Modified: 2012-05-05
Hi experts,

I have main.pl and test.pl as follows. In test.pl, there are fun1 and fun2. My fun1 works fine, but my fun2 was not working. Basically, I want both fun1 and fun2 be able to get the array properly. Does anyone know what I missed in fun2? Thanks!

main.pl
----------------------------------------------
#!/usr/bin/perl -w

require "test.pl";

$data[0][0] = 1.0;
$data[0][1] = 11.0;
$data[0][2] = 111.0;

$data[1][0] = 2.0;
$data[1][1] = 22.0;
$data[1][2] = 222.0;

@newData1 = fun1(@data);

print "Test1 "
  . $newData1[1][0] . " "
  . $newData1[1][1] . " "
  . $newData1[1][2];
 
###############################

@newData2 = fun2(@data,2);

print "Test2 "
  . $newData2[1][0] . " "
  . $newData2[1][1] . " "
  . $newData2[1][2];

----------------------------------------

test.pl
---------------
#!/usr/bin/perl -w

sub fun1 {
      @data = @_;

      print "Row Number = " . @data . "\n";
      print "Column Number = " . @{ $data[0] } . "\n";
      print "Data Item [1][1] = " . $data[1][1] . "\n";

      $numRow = @data;
      $numCol = @{ $data[0] };

      for ( $i = 0 ; $i < $numRow ; $i++ ) {
            for ( $j = 0 ; $j < $numCol ; $j++ ) {
                  print $data[$i][$j] . "  ";
            }
            print "\n";
      }
      return @data;

}


sub fun2 {

      @data = @{$_[0]} ;

      print "Row Number = " . @data . "\n";
      print "Column Number = " . @{ $data[0] } . "\n";
      print "Data Item [1][1] = " . $data[1][1] . "\n";

      $numRow = @data;
      $numCol = @{ $data[0] };

      for ( $i = 0 ; $i < $numRow ; $i++ ) {
            for ( $j = 0 ; $j < $numCol ; $j++ ) {
                  print $data[$i][$j] . "  ";
            }
            print "\n";
      }
      return @data;

}
1;

0
Comment
Question by:meow00
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 1200 total points
ID: 22870209
@newData2 = fun2(\@data,2);
0
 
LVL 1

Author Comment

by:meow00
ID: 22870345
Thanks very much! It works. Now I am just wondering what does the back slash in front of @data mean? Thanks!
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 400 total points
ID: 22870428
It means pass a reference to the array instead of the array data (kind of like a pointer in C/C++).

Your functions assume that every row will have the same number of columns - although this is the case in the data you have, it's not necessarily true for all 2-D arrays in perl.
0
 
LVL 85

Expert Comment

by:ozo
ID: 22870638
perldoc perlref
perldoc perllol
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Six Sigma Control Plans
Suggested Courses

873 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