unable to return multilple values in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unable to return multilple values in perl
# 1  
Old 03-04-2012
Lightbulb unable to return multilple values in perl

hello friends,

i have written one perl script.Which opens a file and search for some parameter's value and gets the status of these parameters. but while i am trying to return these value always i am getting false. Can any one please help me..
here is that function:
=======================
Code:
sub getParameterStatus {
open(FILE,"<$File");
if (grep{/parameter1=false/} <FILE>){
print " parameter1 is false Do you want to make it true \n";
$var1="true";
}
else 
{
print "parameter is already true \n";
}

if (grep{/parameter2=false/} <FILE>){
print " parameter2 is false Do you want to make it true \n";
$var2="true";
}

else 
{
print "parameter is already true \n";
}

if (grep{/parameter3=false/} <FILE>){
print " parameter3 is false Do you want to make it true \n";
$var3="true";
}
else 
{
print "parameter is already true \n";
}
close FILE;

return ($var1,$var2,$var3);

}


Last edited by radoulov; 03-04-2012 at 06:08 AM.. Reason: Code tags!
# 2  
Old 03-04-2012
Code:
sub getParameterStatus {
    open(FILE,"< file.txt");
    for (<FILE>) {
        if (/parameter1=false/) {
            print "param1 is false. setting variable var1 to true\n";
            $var1 = "true";
        }
        if (/parameter2=false/) {
            print "param2 is false. setting variable var2 to true\n";
            $var2 = "true";
        }
        if (/parameter3=false/) {
            print "param3 is false. setting variable var3 to true\n";
            $var3 = "true";
        }
    }
    close FILE;
    return ($var1,$var2,$var3);
}

# 3  
Old 03-04-2012
Why not just something like :

Code:
$ cat my_parameters
param1=toto
param2=titu
param3=tita
param4=titb
param5=titc
param6=titd
$ . ./my_parameters
$ echo ${param3} ${param5}
tita titc

?
# 4  
Old 03-04-2012
Thanks a lot balajesuri...it worked. Smilie..but can you please tell me whats happening in my case...
# 5  
Old 03-05-2012
When you grep on an open filehandle, you read the entire contents at once. After the first if-block, there isn't anything left in the filehandle to be read and grepped. So the 2nd and 3rd if-blocks always go to the else part.

One way is to store the entire contents of the file in an array @array = <FILE> and then grep from the array like this - grep {/parameter1=false} @array. This is a crude and inefficient way. What if your file is large? You wouldn't want to store the entire file in an array and cram the memory, right?

Another way would be to open the file before each if-block and close it before opening it again before 2nd if-block. But that would be an over-kill, wouldn't it?

What I did was to open it once and read it line by line in a for-loop. This way you have better control over the filehandle.
# 6  
Old 03-05-2012
Thanks again to explain in detail.
Yes i tried this also @array = <FILE> and you are absolutely right,mine file is large having so many configurable parameters.
I also tried the second method open the file before each if-block and close it before opening it again before 2nd if-block.it worked but once again you are right Smilie it would be over kill.
anyway mine script is working fine now..Once again thanks a million.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

2. Shell Programming and Scripting

Return multiple values using for loop in perl

I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message. @path=("$DIR1","$DIR2"); foreach (@path) { $rc=system("cp $_/*xml $DIR3"); if ($rc == 0) { print "Files were copied... (1 Reply)
Discussion started by: liyakathali
1 Replies

3. Shell Programming and Scripting

Need Multiple Return Values

Hi, I need to retrun multiple values function errorFileCreation { echo "Before" return -1 "Siva"; echo "Aftyer" } echo ${?} - This can be used to getting first value. how can i get second one. Advance Thanks... Shiv (3 Replies)
Discussion started by: rsivasan
3 Replies

4. Shell Programming and Scripting

Can $? return multiple values?

Hi, I have a script which does something like the below: execute_some_script.sh $arg1 $arg2 `exec-some-cmd` if then; do something else do something else fi However, during some cases, there is an error saying: line xxx: [: too many arguments at the line number which has... (5 Replies)
Discussion started by: laloo
5 Replies

5. Shell Programming and Scripting

unable to return a decimal value from a function

Hi Guys, I am unable to return a decimal value from a function to the main script. I am getting the correct value in the function but when it is returning to the main script using "return" it is coming as 0. Below is my code. read VALUE_V fun_FATHERID fun_SONID fun_TOPID fun_RTYPE <... (2 Replies)
Discussion started by: mac4rfree
2 Replies

6. Shell Programming and Scripting

Help: return values from awk

Hi. I have a script like this: nawk 'BEGIN {FS=","; TOT1=0; REJ1=0;} { if($7=="TOTAL") { TOT1=TOT1 +$8} if($7=="REJS") { REJ1=REJ1 +$8} }' FILE_123.dat and... (1 Reply)
Discussion started by: mrodrig
1 Replies

7. UNIX for Dummies Questions & Answers

Need to write messages to Multilple users

hi, I need to send a message to multiple users to all there logged on terminals i tried write but its send to only one user and only one terminal i cant use wall command because all the people in group should not receive the message (3 Replies)
Discussion started by: pbsrinivas
3 Replies

8. Shell Programming and Scripting

Possible return values for $?

I think the $? returns 0 if the last issued command was successful and otherwise if not. But does anyone knows the value list that may be returned ? (or it is only zero/one ? ) Thanks in advance, Abrahao. (3 Replies)
Discussion started by: 435 Gavea
3 Replies

9. Shell Programming and Scripting

perl "system" cmd return values..

perl 5.6.1: when i try a "system" command(with if loops for $?), i get this: child exited with value 1 what is meant by this $? values and what does it meant if it returns 1?.. (0 Replies)
Discussion started by: sekar sundaram
0 Replies

10. UNIX for Dummies Questions & Answers

exit/return values

Sys: HP-UX 9000 In the calling script how do I 'read' the return/exit value of a called script?:confused: THX in advance for any assistence.:) (1 Reply)
Discussion started by: vslewis
1 Replies
Login or Register to Ask a Question