Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Help re: Formatted Output

Status
Not open for further replies.

Zdog

Technical User
Joined
Mar 6, 2003
Messages
4
Location
US
So far can't figure out how to output a formatted line of data out to a file using the write command. Should I be using printf instead or will write work?

Thanks
 
Are you trying to format output using printf, pack, or filehandle formats?

jaa
 
That's my question. I assumed I should be using a filehandle with write, but either I have the syntax wrong or using a filehandle is the wrong solution.

format Stuff = @<<<<<@<<<<<@
$stuff1, $stuff2, $stuff3
.

$_ = &quot;Stuff&quot;;
write; #Where does the output filehandle go??
 
The variables in your format need to have values (you need to assign something to them) before you write. So an example to format STDOUT:
Code:
format STDOUT =
@<<<<<<<<<@<<<<<<<<<@<<<<<<<<<<
$stuff1, $stuff2, $stuff3
.

for ('stuff1 stuff2 stuff3', 'item1 item2 item3', 'thing1 thing2 thing3') {

    # split string on whitespace and assign to variables
    ($stuff1, $stuff2, $stuff3) = split;
    write;
}
And to print formatted data to a file:
Code:
format OUT = 
@<<<<<<<<<@<<<<<<<<<@<<<<<<<<<<
$stuff1, $stuff2, $stuff3
.

open OUT, &quot;> format.out&quot; or die $!;

# Must select() the filehandle to use
my $oldfh = select (OUT);
for ('stuff1 stuff2 stuff3', 'item1 item2 item3', 'thing1 thing2 thing3') {

    ($stuff1, $stuff2, $stuff3) = split;
    write;
}
# Return viewers to their regularly scheduled filehandle
select ($oldfh);
Read the perlform perldoc for more info:

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top