Perl Array Variables to be returned to main


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Array Variables to be returned to main
# 1  
Old 07-01-2009
Perl Array Variables to be returned to main

Hi All,

I can;t seem to print out the array in sequence using the below subroutine.
My first element in the array @lotsuffix is suppose to be $lotsuffix[1] as defined in the subroutine, but when the array variable is being pass on the main program, my first element actually becomes $lotsuffix[0] !
Can any expert give some advice on this ?
How can i overcome this problem ?


Code:
#!/usr/bin/perl
use strict;

my @lotsuffix;
my @date;

capture_LOTSUFFIX_DATE("test");
        print "LOTSUFFIX1= $lotsuffix[1]\n";    
        print "LOTSUFFIX2= $lotsuffix[2]\n";
        print "LOTSUFFIX3= $lotsuffix[3]\n";
        print "LOTSUFFIX4= $lotsuffix[4]\n";
        print "LOTSUFFIX5= $lotsuffix[5]\n";
        print "LOTSUFFIX6= $lotsuffix[6]\n";
        print "LOTSUFFIX7= $lotsuffix[7]\n";
        print "LOTSUFFIX8= $lotsuffix[8]\n";
        print "LOTSUFFIX9= $lotsuffix[9]\n";
        print "LOTSUFFIX10= $lotsuffix[10]\n";


sub capture_LOTSUFFIX_DATE {

        my $counting = 1;
        my $line;


        for ($counting = 1; $counting <= 10 ; $counting++ ) {
                print "reports_${counting}_${_[0]}.txt\n";
                     open(FH, "< $HISTORY/reports_${counting}_${_[0]}.txt" ) or die $!;
                             while( $line = <FH> ) {
                                     my @Fld = split(' ', $line );
                                     if ( $Fld[1] eq 'LOTSUFFIX=' ) {
                                             $lotsuffix[$counting] = $Fld[2];
                                                print "$lotsuffix[$counting]\n";
                                     }
                                    if ( $Fld[1] eq 'DATE=' ) {
                                         $DATE[$counting] = $Fld[2];
                                         my @fields = split(/-/, $DATE[$counting]);
                                         $date[$counting] = sprintf ("%s-%s",$fields[1],$fields[2]);
                                        print "$date[$counting]\n";
                                     }
                             }
                             close FH;
             }

                return (@lotsuffix , @date); 
 
}

# 2  
Old 07-01-2009
Even though you start adding to the array at index 1, perl inserts index 0 before index 1.
# 3  
Old 07-01-2009
If you still want to keep the base index as 1,use this command,

$[=1;

sample code:
@Marks = (10,11,2007);
print @Marks[0],@Marks[1],@Marks[2];
$[=1;
print @Marks[1],@Marks[2],@Marks[3];


# 4  
Old 07-01-2009
Hi prasperl,

May i know if the below code is added , will it set all the rest of the base array index to 1 or does it just apply to the particular array @Marks only ?

Code:
$[ = 1;

Another question is that my "reports_${counting}_${_[0]}.txt" file has the below contents, however if i put the index to be the below in red, i would NOT be able to capture the correct $lotsuffix variable ? Why is this so ?

Code:
LOTSUFFIX1= AAAAA
LOTSUFFIX2= BBBBBB
LOTSUFFIX3= NIL
LOTSUFFIX4= NIL
LOTSUFFIX5= NIL
LOTSUFFIX6= NIL
LOTSUFFIX7= NIL
LOTSUFFIX8= NIL
LOTSUFFIX9= NIL
LOTSUFFIX10= NIL

DATE1= 29-Jun
DATE2= 29-Jun
DATE3=
DATE4=
DATE5=
DATE6=
DATE7=
DATE8=
DATE9=
DATE10=

Code:
my @Fld = split(' ', $line );
if ( $Fld[0] eq 'LOTSUFFIX=' ) {
          $lotsuffix[$counting] = $Fld[1];
          print "$lotsuffix[$counting]\n";
}


Last edited by Raynon; 07-01-2009 at 05:41 AM..
# 5  
Old 07-01-2009
Setting $[ is not something you should do, it affects other functions and may still not work properly. You should just accept the fact that the first index is zero and start there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Perl: How to avoid warnings for superfluous values returned?

Hi all, a question for the Perl knowledgeable: I have use warnings; enabled. I use something like: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); In the further code I only work with some of the returned variables, as the others I have no need for. Though the... (1 Reply)
Discussion started by: zaxxon
1 Replies

3. UNIX for Dummies Questions & Answers

[ksh93+] Array fed by function is empty when used in main.

I feel that i am missing something obvious but i can't find what is wrong. I have a script that is launching some functions with "&" and each call is feeding the array with a value. When all calls are finished I just want to retrieve the values of that array. It is looking like that : ... (5 Replies)
Discussion started by: bibou25
5 Replies

4. 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

5. Shell Programming and Scripting

Help in separating variables declared in the main function

Hi! I've a C program as shown below.. The line numbers and the statements of the program are separated by a space.. 1 #include<stdio.h> 2 char a,b,c; 3 float x,y,z; 4 int main() 5 { 6 int d,e,f; 7 // further declarations 8 // further declarations 9 /* body*/ 10 } 11 void fun1() 12... (1 Reply)
Discussion started by: abk07
1 Replies

6. Shell Programming and Scripting

How to get the main node name in xml using perl?

Hi, I am having an conf file like this: <Main> <NODE> <NODENAME>FRUITS</NODENAME> <NAME>APPLE</NAME> <COLOUR>RED</COLOUR> <NODE> <IsWrapper/> <NODENAME>SEASONAL</NODENAME> <NODE> <NAME>MANGO</NAME> <COLOUR>GREEN</COLOUR> </NODE> </NODE>... (4 Replies)
Discussion started by: vanitham
4 Replies

7. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

8. 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

9. Shell Programming and Scripting

'who' command -- getting data returned into variables (in bash)

Anyone know how to get the data out of the "who" command? I'm still in lukewarm pursuit of creating an OS X-like welcome message at the start of any Terminal session, sans having to use either bash login or calling up ssh. My trials & errors and surfing around have narrowed it down for me... (1 Reply)
Discussion started by: SilversleevesX
1 Replies

10. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies
Login or Register to Ask a Question