Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

print format

by Anonymous Monk
on Apr 22, 2003 at 01:12 UTC ( #252174=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: print format
by Zaxo (Archbishop) on Apr 22, 2003 at 01:48 UTC

    There is format and write, which are designed for that sort of output.

    After Compline,

      I am searching on GeneBank data, I got 480 Accession number, for each Accession number, I need two items: item1 and items, so I have the print format like:
      Accession item1 item2 12345678 abcd1234 als
      because I generate each accession number first, for each accession number, I use foreach loop to generate item1 and item2, maybe some of the accession number have a couple of groups item1 and item2, maybe some of them have none item1 and item2 at all. I tried to use:
      printf ("%08d",$accession);#which is before foreach loop. foreach (@accession){ .... $item1=....; $item2=....; printf("%16s%08s",$item1,$item2); }
      It printed out so messy. Please help! Thanks!

        Use a temporary variable. Set it to the value to print before entering the loop. After the first print in the loop, set it to an empty string. This assumes that the loop will produce at least one line of output.

        my ($print_accession) = sprintf("%08s",$accession); foreach (@accession) { ... $item1=...; $item2=...; printf("%8s%16s%8s\n",$print_accession,$item1,$item2); $print_accession = ""; }

        If you don't like your strings right aligned in their field, use '%-16s' - the minus indicates the field should be left aligned. See perldoc -f sprintf for details on the available formats.

        90% of every Perl application is already written.
•Re: print format
by merlyn (Sage) on Apr 22, 2003 at 02:43 UTC
    Since you haven't said how your original data is organized, it could be as simple as:
    print <<'END'; 1223455 abdkek akdjeg ksitjls jsoirl lsiet oliut 2874824 lsiugl potpsgj postis potltids 1375080 0875709 llskgjo ooiwuto 9876653 lsgjgdlk ldkgjldj END
    Really, any answer you can get is nonsense until you've described how your source data sits.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: print format
by ctilmes (Vicar) on Apr 22, 2003 at 16:03 UTC
    I stuck your test data into a structure purely for an example. Concentrate on the loops and the format/write. Fill in your own logic for setting $accession, $item1 and $item2.
    my %accession = ( '1223455' => [ { item1 => 'abdkek', item2 => 'akdjeg' }, { item1 => 'ksitjls', item2 => 'jsoirl' }, { item1 => 'lsiet', item2 => 'oliut' } ], '2874824' => [ { item1 => 'lsiugl', item2 => 'potpsgj' }, { item1 => 'postis', item2 => 'potltids' } ], '1375080' => [ ], '0875709' => [ { item1 => 'llskgjo', item2 => 'ooiwuto' } ], '9876653' => [ { item1 => 'lsgjgdlk', item2 => 'ldkgjldj' } ] ); my ($accession, $item1, $item2); foreach $accession (sort keys %accession) { #.... if (not scalar @{$accession{$accession}}) # no items { $item1 = $item2 = ''; write; next; } foreach my $itemset (@{$accession{$accession}}) { # Do whatever you want to set $item1, $item2 $item1 = $itemset->{item1}; $item2 = $itemset->{item2}; write; $accession = ''; # blank for subsequent prints } } format STDOUT_TOP = Accession item1 item2 . format STDOUT = @<<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<< $accession, $item1, $item2 .

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2021-04-16 17:07 GMT
Find Nodes?
    Voting Booth?

    No recent polls found