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

Hi, I need print just $name, $lastName, $address, $phone and $email from %contact, and it's has to be in this format and with dumper.
================================================ Name LastName Email Address Phone ================================================ name1 lasname email address phone This is the code until now.
#!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; my %contact; my $input; sub entrada{ my ($var) = shift; print "Ingrese $var: "; my $in = <STDIN>; return $in; } sub contact_entry { my @contact =(); my $name = entrada('Name'); my $lastName = entrada('Last Name'); my $address = entrada('Address'); my $email = entrada('Email'); my $phone = entrada('Phone Number'); my $image = entrada('Image Profile'); my $detailProfile = entrada('Detail Profile'); my %contact_entry = ( name => $name, lastName => $lastName, address => $address, email => $email, phone => $phone, image => $image, detailProfile => $detailProfile ); return %contact_entry; } sub contact_list { my (%contact) = shift; } %contact = contact_entry; print "Desea ingresar mas Contactos?[S/n]:"; $input = <STDIN>; chomp($input); if ($input eq 's') { print "Cargar mas contactos\n"; } else { print <<EOF; ============================================== Nombre Apellido E-mail Direccion Telefono ============================================== EOF print Dumper(\%contact); exit 1; }

Replies are listed 'Best First'.
Re: Print Data::Dumper with format
by Athanasius (Archbishop) on Oct 20, 2014 at 02:53 UTC

    Hello Matiasinf, and welcome to the Monastery!

    Here are two suggestions which you will find useful:

    1. Add a chomp statement to sub entrada:

      sub entrada { my ($var) = shift; print "Ingrese $var: "; my $in = <STDIN>; chomp $in; # <== Add this return $in; }

      This removes the trailing newline character from the datum input by the user.

    2. Pick out the hash entries you want using a hash slice:

      print join("\t", @contact{qw(name lastName email address phone)});
    3. As others have already pointed out, Data::Dumper is not the appropriate tool to use here.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thanks a lot! I just try it and it's works fine. I just have to do some adjustments to do it all. Again, thanks, and now i understand that Data::Dump is not appropriate...
Re: Print Data::Dumper with format
by GotToBTru (Prior) on Oct 20, 2014 at 02:35 UTC

    The primary purpose of Data:Dumper is to provide you with a clear picture of the structure of the data you're dumping. Once you have that, it should be straightforward to pull out the individual parts you're interested in. Then you can format them however you like.

    1 Peter 4:10
Re: Print Data::Dumper with format
by Loops (Curate) on Oct 20, 2014 at 02:30 UTC

    Hi there and welcome to the monastery! Am struggling to understand what your question is though. Do you want one line per entry as the header suggests? If so, why do you want to use Data::Dumper?

      Hi, I want to print all contacts that i add to the hash. But it has to be with that header. For example, if i have 3 contacts:
      ================================================ Name LastName Email Address Phone ================================================ John Matt J_m@gmail Park av. 465512 Math Perry ..... .... .... Julie .... ..... .... .....
      I hope you understand now... I use Data::Dumper to print all... Thanks for everything!

        Okay, that makes sense. So there are few things that need to change in your code. As you've already figured out, Data::Dumper isn't the tool you want to use to create tables. A good choice for that instead is Text::Table. This module will automatically adjust column sizes for you to accommodate whatever text is entered for each field.

        Also, you want your code to accept multiple contact entries, looping once for each person entered. After each entry you create a hash for that person(as your code already does), and accumulate the hashes in an array. This array will hold multiple hashes, one for each person entered

        Once the last person has been entered you can use Text::Table to print out this array of hashes.

        #!/usr/bin/perl use strict; use warnings; use diagnostics; use Data::Dumper; use Text::Table; # Chomp each input to remove trailing linefeed immediately sub entrada{ my ($var) = shift; print "Ingrese $var: "; my $in = <STDIN>; chomp $in; return $in; } # Return a reference to the hash that holds all of the contact informa +tion sub contact_entry { my @contact =(); my $name = entrada('Name'); my $lastName = entrada('Last Name'); my $address = entrada('Address'); my $email = entrada('Email'); my $phone = entrada('Phone Number'); my $image = entrada('Image Profile'); my $detailProfile = entrada('Detail Profile'); my %contact_entry = ( name => $name, lastName => $lastName, address => $address, email => $email, phone => $phone, image => $image, detailProfile => $detailProfile ); return \%contact_entry; } # Create a hash for each contact entered, and add it to the end of our + array my @contacts; while (1) { my $contact = contact_entry; push @contacts, $contact; print "Desea ingresar mas Contactos?[S/n]:"; my $input = <STDIN>; chomp($input); last if $input ne 's'; } # Set the header my $table = Text::Table->new( "Nombre\n======", "Apellido\n========", "E-mail\n======", "Direccion\n=========", "Telefono\n========" ); # load the data into the table, converting each hash to an array with +only the proper columns $table->load( map { [ @{$_}{qw(name lastName email address phone)} ] } @contacts ); # All done, print it out print $table;
Re: Print Data::Dumper with format
by Anonymous Monk on Oct 20, 2014 at 19:42 UTC

    Am sure others have pointed out relevant solution, which I believe worked. But if I may suggest, what improvement for your "contact_entry" subroutine.
    Instead of the way it is presently implemented, why not either do this

    sub contact_entry { return ( name => entrada('Name'), lastName => entrada('Last Name'), address => entrada('Address'), email => entrada('Email'), phone => entrada('Phone Number'), image => entrada('Image Profile'), detailProfile => entrada('Detail Profile'), ); }
    OR this
    sub contact_entry { my %contact_entry; for (qw(name last_name address email phone_number image detail_pro +file)) { my $data = /_/ ? join ' ' => map { ucfirst $_ } split /_/, $_ : ucfirst $_; $contact_entry{ $_ } = entrada($data); } return %contact_entry; }
    Make perl and your computer work for you, not the other way round! :)