Solved

Perl code to write values to a file

Posted on 2009-07-06
4
171 Views
Last Modified: 2012-05-07
Hello

Could you help me with some code ? I have to do some stats to some data then print it back to a new file (to be created at the time)

what code would I use to create a file and print out the contents of variables to it ?
0
Comment
Question by:MichaelGlancy
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 24789564

#The variables, and their values

my $v1=1;

my $v2=2;
 

open(my $fh, ">filename.txt") or die "filename.txt: $!\n";

print $fh "v1=$v1\n";

print $fh "v2=$v2\n";

close($fh);

Open in new window

0
 

Author Closing Comment

by:MichaelGlancy
ID: 31600378
thankyou
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24789588
Using your other question as a sample.
#!/usr/bin/perl
 

open(IN, "<in.txt") or die;

open(OUT, ">out.txt") or die;

my $s;
 

while($s = <IN>) {

   if($s =~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {

      my ($n1, $n2, $n3, $n4, $n5, $n6, $n7) = ($1, $2, $3, $4, $5, $6, $7);

      # Do some random calculations

      $n1 += 1;

      $n2 += 2;

      print OUT "$n1,$n2,$n3,$n4,$n5,$n6,$n7\n";

   }

}

Open in new window

0
 

Author Comment

by:MichaelGlancy
ID: 24789609
thank you    mrjoltcola
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 109
Perl script to parse log and delete the file 17 172
SIMPLE Perl Regex 1 153
Perl passing in variables to do substitution 6 66
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now