Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Formatting and write help

by negzero7 (Sexton)
on Apr 15, 2008 at 01:19 UTC ( #680408=perlquestion: print w/replies, xml ) Need Help??

negzero7 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get perl to read in a bunch of info from the user and format it, to print it out on screen like this:

+--------------------------------------------------------------+ | Check No. $chkno | | $first $last | | $addr[0] | | $addr[1] | | $phone $date | | | | Pay to the | | Order of: $payee $amount | | | | For: $memo Stamped Signature | | | +--------------------------------------------------------------+

I keep running into errors and I am not sure what's wrong. The address is supposed to be stored in an @array[0] and @array1 (first for house number and street, second for city, state, zip).

Take a look at my code and see if you can help me out.

#!/usr/bin/env perl use warnings; use strict; print "Please enter the check number: "; chomp (my $chkno = <STDIN>); print "Please enter your first and last name: "; chomp (my $first = <STDIN>); chomp (my $last = <STDIN>); print "Please enter your house number (or PO box) and street name: "; chomp (my $one =<STDIN>); print "Please enter your city, state, and zipcode: "; chomp (my $two = <STDIN>); my @addr = {$one, $two}; print "Please enter your phone number: "; chomp (my $phone = (<STDIN>)); print "Please enter the date: "; chomp (my $date = <STDIN>); print "Please enter the payee's name: "; chomp (my $payee = <STDIN>); print "Please enter the amount to be paid: "; chomp (my $amount = <STDIN>); print "Please enter a memo for the check: "; chomp (my $memo = <STDIN>); format CHECK = +--------------------------------------------------------------+ | Check No. @##### | $chkno, | @<<<<<<<<< @||||||||||||||| | $first, $last, | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | $addr[0], | @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< | $addr[1], | @<<<<<<<<<< @|||||||||||||||||| | $phone, $date, | | | Pay to the | | Order of: @|||||||||||||||||||||||||||| @#####.## | $payee, $amount, | | | For: @<<<<<<<<<<<<<<< Stamped Signature | $memo | | +--------------------------------------------------------------+ . write (CHECK);

Replies are listed 'Best First'.
Re: Formatting and write help
by ikegami (Pope) on Apr 15, 2008 at 01:43 UTC

    Curly brackets create a hash, initializes it with the contents of the curlies, and returns a reference to the hash. So

    my @addr = {$one, $two};

    means

    my %anon_hash; $anon_hash{$one} = $two; my @addr = \%anon_hash;

    Obviously, that's not what you want. To build a list, use parentheses.

    my @addr = ($one, $two);
Re: Formatting and write help
by ysth (Canon) on Apr 15, 2008 at 02:26 UTC

      Thanks for the tip, I wasn't catching that part from the examples I was using, it now works perfectly!

Re: Formatting and write help
by moklevat (Priest) on Apr 15, 2008 at 01:47 UTC
    It looks like you'll have a working solution with ikegami's help, but if you're in the mood to explore a different way of accomplishing your task, you might look at Perl6::Form.
Re: Formatting and write help
by amarquis (Curate) on Apr 15, 2008 at 01:45 UTC

    Curly braces define an anonymous hash reference, you want parens to make a list:

    my @addr = ($one, $two);

    Edit: Beaten by Ikegami, which seems to be a running theme :).

      Whoops, silly mistake there. Ok, that is fixed, but I am getting this error:

      write() on unopened filehandle CHECK at project3.pl line 63, <STDIN> line 10.

      Now I'm assuming that means that my filehandle CHECK is somehow not initialized or it's not formatted properly to be read. Any help with that one?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://680408]
Approved by grep
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-11-27 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?