Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble passing an array to a module 1

Status
Not open for further replies.

Nutthick

MIS
Jan 14, 2004
126
0
0
GB
I'm trying to pass a couple of vars and an array to a perl module, but I can't seem to get the syntax correct. The module has the following code
Code:
my ($new_name) = $_[0];
my ($new_desc) = $_[1];
my (@new_time) = $_[2];

print FILES "$new_name\n";
print FILES "$new_desc\n";

foreach my($tempTime) (@{$new_time})
{
	print FILES "$tempTime\n";
}
and my code to call it
Code:
update_time_log($name, $desc, \@time);
I keep getting an error saying that I'm missing a '$' from the foreach line. I'm sure this is something simple, but it's winding me up. Can anyone help?

Thanks
 
Since you are passsing reference to an array shouldnt it be :
Code:
my ($new_name) = $_[0];
my ($new_desc) = $_[1];
my ([b]$[/b]new_time) = $_[2];

print FILES "$new_name\n";
print FILES "$new_desc\n";

foreach my($tempTime) (@$new_time)
{
    print FILES "$tempTime\n";
}


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Remove the parenthesis from around $tempTime in the line that begins with `foreach'. That'll do it.

@spookie, @$new_time and @{$new_time} are the same thing.
 
Thanks for the replies, but I'm still not there.

My declaration is now
Code:
my ($new_cron) = $_[2];

Here is what the loop looks like now
Code:
my($tempTime);
foreach $tempTime (@$new_time)
{
	print FILES "$tempTime\n";
}
However, on the file line that receives $tempTime I don't get an array value, I get this
Code:
ARRAY(0x8234d10)
Any ideas?
 
then you seem to have an array of array references. What are you expecting $tempTime to be?

This might work but I have no idea what you are really trying to do with your data:

Code:
foreach [b]my[/b] $tempTime (@$new_time)
{
    print FILES "@{$tempTime}\n";
}

or if that's all the code there is you can write it shorter:

Code:
print FILES "@$_\n" for @$new_time;
 
Hi KevinADC

The array is full of strings that I need to write to file. I'm getting closer, but I'm just changing things to see what effect they have. I can get rid of the
Code:
ARRAY(0x8234d10)
in the file, by changing my call to the module to
Code:
update_time_log($name, $desc, @time);
so removing the '\' before '@time'. The file gets written the correct data, but only the first element of the array. I think that makes sense, based on the code, but it obviously isn't what I want.
 
Code:
update_time_log($name, $desc, \@time);
This is the correct way of doing it. What does your update_time_log() function look like?

M. Brooks
 
you can pass an array like this:

update_time_log($name, $desc, @time);

as long as you understand when update_time() gets the data it will be one long list, so you have to do something like this to turn it back into two scalars and an array:

Code:
sub update_list {
   my $variable1 = shift;
   my $variable2 = shift;
   my @array = @_;
   [rest of code here]
}


 
Code:
sub update_list {
	my ( $variable1, $variable2, $array ) = @_;

	for my $i ( 0 .. @$array ) {	
		print @$array[$i] . "\n";
	}
}

M. Brooks
 
All I'm trying to do is pass two string variable and one array of strings to a perl module. That module then appends those values to a file. The most success I've had so far is the file only getting the first value out of the array, but that's by fudging the call. This is where I'm at.

If I do the following call
Code:
$name="my_test";
$desc="my_desc";
push @time, "Item 1";
push @time, "Item 2";

update_job_log($name, $desc, \@time);
then the array gets printed to file as
Code:
ARRAY(0x8234cb0)
removing the leading '\' on the call means I only get the first element of the array printed in the file. If I print the value of @$new_time to the file, both time it is '1'.

The update_log file currently looks like this
Code:
sub update_log
{
	my ( $new_name, $new_desc, $new_time ) = @_;

	open (FILES, ">>" . $filelog) || die "Could not open file for append";
	print FILES "$new_name\n";
	print FILES "$new_desc\n";
	print FILES "1\n";

	for my $i ( 0 .. @$new_time )
	{    
        	print FILES @$new_time[$i] . "\n";
    	}
	print FILES "||\n";
	close(FILES);

	return 1;
}
but again placing a '\' when I pass the array leads to the 'ARRAY(0x8234cb0)' being printed in the file.

What I would expect is the following appended to the file
Code:
my_test
my_desc
Item 1
Item 2
 
Same result. If I include the leading '\' in the call I get
Code:
ARRAY(0x8234d04)
written to the file. If I omit it I get the first item in the array only.
 
Wait a minute..

What's this in the example you posted above?
Code:
update_job[b]_log[/b]($name, $desc, \@time);
and then
Code:
sub [b]update_log[/b]
{
    my ( $new_name, $new_desc, $new_time ) = @_;

    open (FILES, ">>" . $filelog) || die "Could not open file for append";
    print FILES "$new_name\n";
    print FILES "$new_desc\n";
    print FILES "1\n";

    for my $i ( 0 .. @$new_time )
    {    
            print FILES @$new_time[$i] . "\n";
        }
    print FILES "||\n";
    close(FILES);

    return 1;
}

M. Brooks
 
That's it, I quit. If I can't notice stuff like that I need to be shot. I've got stuff passing over now, thanks for the help.

I going to bed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top