[Perl] Strange problem with array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Perl] Strange problem with array
# 1  
Old 10-28-2010
[Perl] Strange problem with array

Hi,

I have a strange problem with arrays in Perl.
That is to say, for me it is strange and perhaps there is a special reason for it that I do not know of.
Not a real Perl Ace.

This is the program, as an example:

Code:
#!/usr/bin/perl -w #-d

use strict;

my $pu;
my $pu_list_cmd;

my @pu_list = (
"PU_0001",
"PU_0002",
"PU_0003",
"PU_0004",
"PU_0005");

printf "pu_list at the beginning: @pu_list\n";
foreach (@pu_list) {
  $pu = $_;
  printf "$pu\n";
  printf "pu_list in foreach loop: @pu_list\n";
} # End of foreach loop

printf "pu_list at the end: @pu_list\n";

printf "\n\n";

printf "pu_list at the beginning: @pu_list\n";
foreach (@pu_list) {
  $pu = $_;
  $pu_list_cmd = "echo \"$pu applied\"";
  printf "$pu";

  open (CMD, '-|', "$pu_list_cmd");
  while (<CMD>) {
    chomp;
    if ( /applied$/ ) {
      printf " ... OK.\n";
    } elsif ( /ERROR: There is no PU/ ) {
      printf " ... not installed.\n";
    }
  } # End of while loop
  close (CMD);
  printf "pu_list in foreach loop: @pu_list\n";
} # End of foreach loop

printf "pu_list at the end: @pu_list\n";

The output:

Code:
pu_list at the beginning: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0001
pu_list in foreach loop: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0002
pu_list in foreach loop: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0003
pu_list in foreach loop: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0004
pu_list in foreach loop: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0005
pu_list in foreach loop: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
pu_list at the end: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005


pu_list at the beginning: PU_0001 PU_0002 PU_0003 PU_0004 PU_0005
PU_0001 ... OK.
Use of uninitialized value in join or string at ./ej.pl line 42.
pu_list in foreach loop:  PU_0002 PU_0003 PU_0004 PU_0005
PU_0002 ... OK.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
pu_list in foreach loop:   PU_0003 PU_0004 PU_0005
PU_0003 ... OK.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
pu_list in foreach loop:    PU_0004 PU_0005
PU_0004 ... OK.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
pu_list in foreach loop:     PU_0005
PU_0005 ... OK.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
Use of uninitialized value in join or string at ./ej.pl line 42.
pu_list in foreach loop:
Use of uninitialized value in join or string at ./ej.pl line 45.
Use of uninitialized value in join or string at ./ej.pl line 45.
Use of uninitialized value in join or string at ./ej.pl line 45.
Use of uninitialized value in join or string at ./ej.pl line 45.
Use of uninitialized value in join or string at ./ej.pl line 45.
pu_list at the end:


Can someone explain why my array is being emptied in the second foreach loop ?

Thanks in advance,

E.J.
# 2  
Old 10-28-2010
Do it this way:
Code:
#!/usr/bin/perl -w #-d

use strict;

my $pu;
my $pu_list_cmd;

my @pu_list = (
"PU_0001",
"PU_0002",
"PU_0003",
"PU_0004",
"PU_0005");

printf "pu_list at the beginning: @pu_list\n";
foreach (@pu_list) {
  $pu = $_;
  printf "$pu\n";
  printf "pu_list in foreach loop: @pu_list\n";
} # End of foreach loop

#printf "pu_list at the end: @pu_list\n";


printf "pu_list at the beginning: @pu_list\n";
foreach $pu (@pu_list) {
  $pu_list_cmd = "echo \"$pu applied\"";
  printf "$pu";

  open (CMD, '-|', "$pu_list_cmd");
  while (<CMD>) {
    chomp;
    if ( /applied$/ ) {
      printf " ... OK.\n";
    } elsif ( /ERROR: There is no PU/ ) {
      printf " ... not installed.\n";
    }
  } # End of while loop
  close (CMD);
  printf "pu_list in foreach loop: @pu_list\n";
} # End of foreach loop

printf "pu_list at the end: @pu_list\n";

This User Gave Thanks to Klashxx For This Post:
# 3  
Old 10-28-2010
Thanks a lot !
Works now.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

2. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

3. Shell Programming and Scripting

Perl nested array problem

I have a array reference which has some number of array references inside it.The nested array references also contains the array references. my $Filename = "sample.xml"; my $Parser = new XML::Parser( Style => 'tree' ); my $Tree = $Parser->parsefile( $Filename ); Here the $Tree is the... (6 Replies)
Discussion started by: karthigayan
6 Replies

4. Shell Programming and Scripting

Perl and string array problem

#!/usr/bin/perl my @arr=("hello", "how", "are", "you"); $l=length(@arr); print $l; This print 1.Why? How can i print the array size = 4? I want to store these in an array. hello how are you And then i want to access these element through indexing. How can i do this? (4 Replies)
Discussion started by: cola
4 Replies

5. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

6. Shell Programming and Scripting

Strange behaviour with perl i/o?

Hi All, I got a strange problem here. I have a perl script which is fetching data from a database table and writing a file with that data. If i run that script from linux command line, the file it creates is a normal ascii text file without any binary character in it.But... (9 Replies)
Discussion started by: DILEEP410
9 Replies

7. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

8. Shell Programming and Scripting

Perl script strange problem

Hi All, I have one strange problem in perl script. Bellow is the contents of the script i am trying to write: #!/usr/local/bin/perl my $module_list = ''; my @running_module; my $count=0; my $flag=0; my $ccprojecthome=$ENV{CCPROJECTHOME}; @module_list = `cat... (2 Replies)
Discussion started by: vaibhav
2 Replies

9. Shell Programming and Scripting

Strange array handling in ksh93

I wrote a script in ksh93 (the OS is AIX 5.2, ML7), which failed to run. After some testing i found out why, but the answer is a bit dissatisfying. Look for yourself: #!/bin/ksh93 # --------- Step Names typeset achStepName="foo" typeset achStepName="bar" typeset achStepName="fubar"... (3 Replies)
Discussion started by: bakunin
3 Replies
Login or Register to Ask a Question