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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing array from one perl script to another perl script 1

Status
Not open for further replies.

dmazzini

Programmer
Jan 20, 2004
480
US
Hi expert

I want to pass one array from one perl script to another perl script.

I don't have problems passing variables. My problem is with the array.

What I am doing:

In the ScriptName 1 :

I am processing some things, but at the end I need to invoke the scriptname 2 and pass some values...

system(scriptname2.pl @array $var1 $var2 $var3 $var4 $var5);


In the ScriptName 2 :
Getting the values from the ScriptName 1

@array = $ARGV[0];
$var1 =$ARGV[1];
$var2 =$ARGV[2];
$var3 = $ARGV[3];
$$var4 =$ARGV[4];
$var5 =$ARGV[5];


Error Found:
Scalar found where operator expected

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

Somebody can help me with that! I was checking references, but to be honest I don't know how to apply it to that particular case...It applies easily parsing arrays to another function, like the nex example:


@array1 = qw/1 2 3/;
@array2 = qw/4 5 6/;

puke( \@array1, \@array2 ); #outputs 123,456

sub puke {
my ($array_ref1, $array_ref2) = @_;
print "$_" foreach @{$array_ref1};
print ",";
print "$_" foreach @{$array_ref2};
}

Thanks!



 
IN this situation I would normally "squash the array into a scalar, and then unpack it in the new script

Code:
my $collapsed;
$i=0;
foreach (@my_array) {
  if ($i==0) {
    $collapsed="$_";
  } else {
    $collapsed.="|$_";
  }
  $i++;
}
Code:
system(scriptname2.pl $collapsed $var1 $var2 $var3 $var4 $var5);
or
Code:
@results=`perl scriptname2.pl $collapsed $var1 $var2 $var3 $var4 $var5)`;
Code:
@array=&unpack_array($ARGV[0]);
...
sub unpack_array {
  my ($collapsed)= @_;
  @array=split(/\|/,$collapsed);
  return @array;
}

HTH
--Paul
 
Have a look at the standard module Data::Dumper, it's ideal for what you want.

Save your data structure in a temporary file using Data::Dumper and then read it in your second script.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hi PaulTEG!

It was very good solution!

I 've never thought to pack and unpack it!

Thanks a lot!


PD: Hey MikeLacey thanks a lot for the advice about the DataDumper.

Cheers
 
You're welcome
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
I need to do the exact same thing. When I use your code sub unpack_array {
my ($collapsed)= @_;
@array=split(/\|/,$collapsed);
return @array;
}
I'm getting errors "Use of uninitialized value in split at email1checker.pl line 114"

 
my @array=split(/\|/,$collapsed);

????
Either that, or @_ does not exist(As in, theres nothing in the array).

___________________________________
[morse]--... ...--[/morse], Eric.
 
my @array=split(/\|/,$collapsed); is still giving error Use of uninitialized value in split at email1checker.pl line 115.

I'm going nuts trying to figure it out.
 
Can you post the calling line? and another few for context
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Calling line: my @array=&unpack_array($ARGV[0]);

The sub:
sub unpack_array
{
my ($collapsed)=@_;
my @array=split(/\|/,$collapsed);
return @array;
}

Here's the code from script1 that collapses it:
foreach $line(@array)
{
if($line=~/$subject1/)
{
my $collapsed;
my $i=0;
foreach (@array)
{
if ($i==0)
{
$collapsed="$_";
}
else
{
$collapsed.="|$_";
}
$i++;
}
system "email1checker.pl",$collapsed;

It is collapsing it but I don't know why I'm getting uninitialized.
 
why not
system(scriptname2.pl $var1 $var2 $var3 $var4 $var5 @array );

in scriptname2.pl
my ($var1,$var2 .... ,$var5,@array)=@ARGV;
 
Hi guys!

Look what I did:

Script 1:

sub pack_array {
my $collapsed;
$i=0;
foreach $element(@array) {
if ($i==0) {
$collapsed="$element";
} else {
$collapsed.="_"."$element";
}
$i++;
}

chomp $collapsed;
$collapsed =~ s/^\s+//;
$collapsed =~ s/\s+$//;

system ("$SCRIPTPATH/$SCRIPT $collapsed $flag_bcf_bts_hoc_poc $flag_malbal $flag_trx $flag_dap $flag_adce $flag_lcse ");
}


(Script 2)
# ##################################################################
# Receiving Arguments
# ##################################################################

@array=&unpack_array($ARGV[0]);
$flag_bcf_bts_hoc_poc=$ARGV[1];
$flag_malbal=$ARGV[2];
$flag_trx= $ARGV[3];
$flag_dap=$ARGV[4];
$flag_adce=$ARGV[5];
$flag_lcse= $ARGV[6];


sub unpack_array {
my ($collapsed)= @_;
@array=split(/\_/,$collapsed);
return @array;
}



 
The other thing can be that you are using in the first line of the perl script the option -w.

#!/usr/bin/perl -w

Check it too!

Cheers!
 
and i did this:
trypass.pl:
#!/usr/bin/perl
my ($a,$b,$c,@arr)=qw (a b c 11 22 33 44);
system ("./printThis.pl $a $b $c @arr");

printThis.pl:
#!/usr/bin/perl
my ($a,$b,$c,@arr)=@ARGV;
print "$a,$b,$c,@arr\n";

./trypass.pl
a,b,c,11 22 33 44
 
Well I've still got issues. What I'm doing is forwarding an email to script1 which determines the subject line and then forwards it, after packing the email, to script2. Right now script1 is packing the email and forwarding the collapsed array.

But script2 is having issues unpacking it because the only thing unpacked is the first word. That's it. I need the entire email unpacked to an array! Here's the packing and unpacking code. Can anyone see anything wrong?

Packing:

foreach $line(@array)
{
if($line=~/$subject1/)
{
my $collapsed;
my $i=0;
#foreach(@array)
foreach $element(@array)
{
if($i==0)
{
#$collapsed="$_";
$collapsed="$element";
}
else
{
#$collapsed.="|$_";
$collapsed.="|"."$element";
}
$i++;
}
chomp $collapsed;
$collapsed=~s/^\s+//;
$collapsed=~s/\s+$//;
#system(email1checker.pl $collapsed);
system ("./email1checker.pl $collapsed");
}

That works fine and the entire email is in collapsed.

Here's the unpacking that doesn't work:

my @array=split(/\|/,$collapsed=($ARGV[0]));



 
what problem did you encounter with ar0ld's simple solution?
 
The simple solution won't work because I'm putting an entire email into the array. So I can't do ($a,$b,$c,@arr)=qw (a b c 11 22 33 44);


 
you are attempting to pass an entire,email; headings, free form, variable length body text?
 
a little more fun. I tried my own test:
system "./mmmm.pl 11 22 \' @array\'";
my @array was created as @array=<SomrOpenedFileofMine>

with mmmm.pl:

print shift @ARGV,"\n";
print shift @ARGV,"\n";
@inp= split /\n/, $ARGV[0];

each line in @inp has a leading space, but otherwise OK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top