469,307 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Perl Script Problem

5
Hi,

I want to write a perl script that will take value from an external file
the file will be in name=value format and write those values to another file
My problem is I am not able to select variables from the file randomly.
My program is always reading the value line by line.
The program so far I tried

Expand|Select|Wrap|Line Numbers
  1. $ENV{'PATH'} = 'C:\shared\IQA\appli.env';
  2. open(ENV,"$ENV{PATH}") || die "cannot open File:";
  3. chomp($a =<ENV>);
  4. chomp($b =<ENV>);
  5. chomp($c =<ENV>);
  6. ($variable, $value) = split /=/, $a;
  7. ($variable1, $value1) = split /=/, $b;
  8. ($variable2, $value2) = split /=/, $c;
  9. if ($variable eq "$APPLI_LOG_SHL_DIR")
  10.      {$APPLI_LOG_SHL_DIR = $value;}
  11. elsif ($variable1 eq "$JOB_ID")
  12.       {$JOB_ID = $value1;}
  13. elsif ($variable2 eq "$LOG_MSG")
  14.       {$LOG_MSG = $value2;}
  15.  my $LOG_FILE  = $value . "/" . $value1 . ".log";
  16.  my $date_as_a_string = localtime;
  17.  open (LOGFILE,">>$LOG_FILE") || die "cannot open logfile $LOG_FILE for append: $!";
  18.  print LOGFILE $value2,"\n",$date_as_a_string, "\n";
  19.  close (LOGFILE);
  20.  
This progarm is always taking the first 3 variable values from the file.But I want the program to take variable values randomly.
Please help me I am complete stuck
Oct 8 '07 #1
2 1266
numberwhun
3,503 Expert Mod 2GB
Hi,

I want to write a perl script that will take value from an external file
the file will be in name=value format and write those values to another file
My problem is I am not able to select variables from the file randomly.
My program is always reading the value line by line.
The program so far I tried

Expand|Select|Wrap|Line Numbers
  1. $ENV{'PATH'} = 'C:\shared\IQA\appli.env';
  2. open(ENV,"$ENV{PATH}") || die "cannot open File:";
  3. chomp($a =<ENV>);
  4. chomp($b =<ENV>);
  5. chomp($c =<ENV>);
  6. ($variable, $value) = split /=/, $a;
  7. ($variable1, $value1) = split /=/, $b;
  8. ($variable2, $value2) = split /=/, $c;
  9. if ($variable eq "$APPLI_LOG_SHL_DIR")
  10.      {$APPLI_LOG_SHL_DIR = $value;}
  11. elsif ($variable1 eq "$JOB_ID")
  12.       {$JOB_ID = $value1;}
  13. elsif ($variable2 eq "$LOG_MSG")
  14.       {$LOG_MSG = $value2;}
  15.  my $LOG_FILE  = $value . "/" . $value1 . ".log";
  16.  my $date_as_a_string = localtime;
  17.  open (LOGFILE,">>$LOG_FILE") || die "cannot open logfile $LOG_FILE for append: $!";
  18.  print LOGFILE $value2,"\n",$date_as_a_string, "\n";
  19.  close (LOGFILE);
  20.  
This progarm is always taking the first 3 variable values from the file.But I want the program to take variable values randomly.
Please help me I am complete stuck
That is because all your script is doing is reading the first 3 lines of the file and reporting them. That is why its always the first 3 lines.

My suggestion would be to take the test file and split on the = sign and assign the two options to a hash (key and value). Once you have them all in a hash, you can then search the keys to get the value of each (randomly without having to be in order.

Regards,

Jeff
Oct 8 '07 #2
itzaps
5
Hi,
Thank you very much for your reply..
I am very new to perl scripting so didn't having much idea about the hash.
Could you please give a detail about the code.
I have tried the program in another way this time it is working fine

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $APPLI_LOG_SHL_DIR ='none';
  4. $JOB_ID = 'none';
  5. $LOG_MSG = 'none';
  6. $tmp ='none';
  7. $ENV{'PATH'} = 'C:\shared\IQA\appli.env';
  8. open(ENV,"$ENV{PATH}") || die "cannot open File:";
  9.  
  10. while($line =<ENV>)
  11. {
  12. chomp $line;
  13. ($variable, $value) = split /=/, $line;
  14. $variable =~ s/^\s+|\s+$//g ;
  15. $value =~ s/^\s+|\s+$//g ;
  16. if ($variable eq "APPLI_LOG_SHL_DIR")
  17.      {
  18.       $APPLI_LOG_SHL_DIR = $value  ;
  19.      }
  20. if ($variable eq "JOB_ID")
  21.       {
  22.       $JOB_ID = $value ;
  23.       }
  24. if ($variable eq "LOG_MSG")
  25.       {
  26.       $LOG_MSG = $value;
  27.       }
  28. }
  29. my $LOG_FILE  = $APPLI_LOG_SHL_DIR . "/" . $JOB_ID . ".log";
  30. ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);$year += 1900;
  31. $mon++;
  32. $sdttm   = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year, $mon, $mday, $hour, $min, $sec);
  33. open (LOGFILE,">>$LOG_FILE") || die "cannot open logfile $LOG_FILE for append: $!";
  34. print LOGFILE $LOG_MSG,"\n",$sdttm, "\n";
  35. close (LOGFILE);
  36.  
Oct 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Christian Eriksson | last post: by
9 posts views Thread by Martin Foster | last post: by
reply views Thread by Marc de Winter | last post: by
6 posts views Thread by Richard Trahan | last post: by
3 posts views Thread by Daniel Moree | last post: by
6 posts views Thread by WipeOut | last post: by
4 posts views Thread by itzaps | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.