×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

I need to output values to a new file....

I need to output values to a new file....

I need to output values to a new file....

(OP)
I have two variables in memory and I want to output them to a new file called type.opt . One is a string and the other is a value. They are in a loop and change with each transition. They have to be output in the style

string1=value1   #loop 1
string2=value2   #loop 2
string3=value3   #loop 3
string4=value4   #loop 4 etc.

Thanks for any suggestions

Gordon Bell
gordon.bell@xilinx.com

RE: I need to output values to a new file....

If you are doing CGI stuff and are using CGI.pm, then open an output file (type.opt) and use the 'save' method....like.


# with vars in memory.
open(OPF,">type.opt") ¦¦ &complain("Failed to open output file, $!");
$query->save(OPF);
close OPF;


If you are not doing CGI with CGI.pm.

%hash_of_pairs = ('string1','value1','string2','value2','string3','value3');
open (OPF,">type.opt") ¦¦ die "Failed to open output file, $!\n";
foreach $key (keys(%hash_of_pairs))
    {
    # print key and value to output file.
    print OPF "$key = $hash_of_pairs{$key}\n";
    }
close OPF;


beware the odd typo

RE: I need to output values to a new file....

Another helpful hint is that you do not have to use the letters OPF, you can use anything like FILE, or FILEHANDLE, anything works like that.

Hope I could help.

RE: I need to output values to a new file....

(OP)
goBoating - The 2nd piece of script you gave me seemed to be what I needed. The only problem was with the line
%hash_of_pairs = ('string1','value1','string2','value2','string3','value3');

because the number of strings and values is variable and of no maximum length.

Thanks for your help anyway - and if you haven't gone boating today you might be able to help me  on this one - cheers!!

Gordon Bell
gordon.bell@xilinx.com

RE: I need to output values to a new file....

(OP)
That is still not that clear.

I have two variables $string and $value. Both change on each loop and there is a variable number of loops. Each time there is a new loop the variables change and I want to make a list of these variables in a .opt file.

Sorry if I'm repeating myself but my first description was not very clear.

Thanks again

Gordon Bell
gordon.bell@xilinx.com

RE: I need to output values to a new file....

I couched the previous post in hash terms to emulate the series of paired values you listed.  The hash could be any iterative (looping) data source.  A hash or an array or a looping file read/split behavior could supply a similar series of values to feed the output logic.
.....as vikter points out, OPF is my own convention
    OPF for OutPutFile
similarly - IPF for InPutFile - you can call the handle what ever makes sense to you

# get a handle on your OutPutFile (OPF)
open (OPF,">type.opt") ¦¦ die "Failed to open output file, $!\n";

# If you are reading some input stream (e.g. reading a file line by line)......
open (IPF,"<someInputFile") ¦¦ die "Failed to open input file, $!\n";
while ($line = <IPF>)
    {
    
#split or pattern match the elements you want to store.
    # If your file looks like lastName¦phoneExtension - pipe delimited
    ($name,$extension) = split(/\¦/,$line); split the line on a pipe, '¦'
    
# print name and extension to OPF
    print OPF "$name = $extension\n";
    }
close IPF;
close OPF;


beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close