Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a subroutine that passes a value to another subroutine. In the second subroutine I try to operate on the argument that was passed to it by using $_ and this is not working.

If I try to print out $_ in the second subroutine, I get the argument string that was passed to the first subroutine. What is the proper way to get at the argument passed in???

The code I'm using is below:

sub splitFn{
	my @splitArray = split(/;/,$_);
	my $retList = ();
	my %hashOut = ();
	my $i = 0;
	my $j = 0;

	for ($i = 0; $i < scalar(@splitArray); $i++){
		$splitArray[$i] =~ s/^\s+//;	#remove leading white spaces ;
		$splitArray[$i] =~ s/\s+$//;	#remove trailing white space s/s+$//;
	}
	if (scalar(@splitArray) == 2){
		$hashOut{"name"} = $splitArray[0];
		$hashOut{"FirstRange"} = $splitArray[1];
	}
	elsif (scalar(@splitArray) == 3){
		$hashOut{"name"} = $splitArray[0];
		$hashOut{"FirstRange"} = arrayMkr($splitArray[1]);
		$hashOut{"SecondRange"} = arrayMkr($splitArray[2]);
	}
	else{
		die "\n ************** Input file is not formatted correctly! At line $splitArray[$i] $!\n";
	} 
			
	
	$retList =(\%hashOut);		#	Creating a reference to the hash that will be passed back.
	return($retList);

}

sub arrayMkr{
	my @splitUpIn = split(" ",$_);
	my $size = scalar(@splitUpIn);
Posted

A Perl function gets an array of argumnents.
That array is referenced by @_.

I usually do the following:

sub FunctionName {
  my ($arg1, $arg2, ... ) = @_;
  ...
}


In your case, you want to split the first argument of the argument array (@_).
You have several options:

sub arrayMkr {
  my $arg = shift;
  my @splitUpIn = split(" ", $arg);
  ...
}


or

sub arrayMkr {
  my ($arg) = @_;
  my @splitUpIn = split(" ", $arg);
  ...
}


or

sub arrayMkr {
  my $arg = $_[0];
  my @splitUpIn = split(" ", $arg);
  ...
}


1st Hint: make sure you know when to use $ and when to use @ (or %). You might want to consult some Perl tutorial on variables and familiarize yourself with the terms scalar/array/list/hash.

2nd Hint: try to avoid $_ an make it explicit. $_ has different meaning depending on the context it is used in. You have less problems if you avoid its usage, especially if you are not an expert on Perl.

You might also consider to re-write the whole function again, e.g.:

sub splitFn {
	my ($line) = @_;
	my $rHash = {}; # reference to a hash
	if ($line =~ m/^\s*(.*?)\s*;\s*(.*?)\s*$/) {
		$rHash = {name=>$1, FirstRange=>$2};
	}
	elsif ($line =~ m/^\s*(.*?)\s*;\s*(.*?)\s*;\s*(.*?)\s*$/) {
		my ($nm, $a, $b) = ($1, $2, $3);
		$rHash = {name=>$nm,
                          FirstRange=>arrayMkr($a),
                          SecondRange=>arrayMkr($b)};
	}
	else {
		die("Failure in '$line' ($!)\n");
	}
	return $rHash;
}
sub arrayMkr {
	my ($range) = @_;
	my @range = split("\s+", $range);
	#...
}


Cheers

Andi
 
Share this answer
 
v3
Awesome!. That provided me some juch needed help.

One thing I notided is that if I assign the @_ to a variable in the subroutine like the following
sub arrayMkr{
    my ($sectionIn) = @_;

}


the code executes properly but if I use this...

sub arrayMkr{
    my $sectionIn = @_;

}

it does not work properly. $sectionIn has a value of 1 assigned to it. Why the difference? What is the importance of adding () when surrounding the first variable defined in the subroutine?
 
Share this answer
 
See http://perldoc.perl.org/perldata.html[^] for a description on the meaning of $scalar, %hash, @array, and (list). Especially, search for @foo in that man page for an explanation on "mixing" arrays and lists with scalars.

You may also check out an (old bust still valid) OO-Perl Demystify Session[^] I held several years back (it includes some short intro into Perl variables and OO-Perl).

Your assignment
my $arg = @_;

assigns the length of the array to the variable, which is "evaluating the array in a scalar context", equivalent to
my $arg = scalar(@_);


To confuse you completely ;-) , there is a difference between assigning an array to a scalar versus assigning a list to a scalar:

$var = @arr; # stores the number of array elements to $var
$var = (3,2,1); # stores the last elemet of the list to $var: 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900