473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1353
numberwhun
3,509 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

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

Similar topics

1
by: Christian Eriksson | last post by:
Hi! I have a problem using the environment variable LD_LIBRARY_PATH in my perl script. Everything works fine when I set it at the command prompt and then runs my perl script like this: ...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
0
by: Marc de Winter | last post by:
Hello all, I hope someone on this group can help me, even though this is kind of a specific question about a (widely used) script. It's the NMS-WWWBOARD script. Formerly from "Matt's Script...
3
by: PzYon | last post by:
Hey 2gether! I'm trying to execute a PERL script on the web server when the user presses a button in an ASP.NET Web Application using Visual Basic. The most obvious solution for me seemed to be...
6
by: Richard Trahan | last post by:
I want a js function to call a Perl script residing on a server. The Perl script will return a string, to be used by the js. Pseudo code: <script> stringvar = perlfunc_on_server(stringarg)...
3
by: Daniel Moree | last post by:
I'm attempting to use a perl script to interface with my Visual Basic 6 program using Winsock. I've got my program setup to connect and works great if i connect to another winsock program, but it...
6
by: WipeOut | last post by:
I am having a problem in a perl script that I can't seem to find an answer for.. The $cost and $retail vars come from another part of the script and would be something like 000134.345 and...
4
by: itzaps | last post by:
I am writing one perl script which will take value from an external file and then append the value to another file. The external file will be in the format like $name=value; $name1=value1; and so...
2
by: IvanIV | last post by:
Hi All, I have trouble with embedding a Perl interpreter into a C program. I would like to replace some subroutine in a Perl script without changing this script from my C program. I need...
3
by: vijayarl | last post by:
Hi all, i have perl script, which is used to send mail. its a command line utility. if we run this perl script in command line by passing all it's required arguments, it works very well.there no...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.