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

My output repeat print part of the first line:
foreach (@record){ my $name; my $id; my $summary; my @data = split('', $_); $name = $data[0]; $id = $data[1]; $summary = $data[2]; format STDOUT = @<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $name, $id, $summary ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $summary . write; }
it printed out:
Sue Sue 098765598 Background alkgjal ksjhfk kajfhkas Background alkgjal ksjhfk kajfhkas alkdfj lskdfmh duuye ehfu sli leoi end. Jane Jane 098776556 Background lifjsl iejfls iwlfilsi Background lifjsl iejfls iwlfilsi lagkj lsifjlwei ksdjf lkci kjosie ksud last line.
it printed twice $name and $summary(only the first line) and the first time $name use the whole line. Please help!!! Thanks!!!

Replies are listed 'Best First'.
Re: Repeat print problem
by pzbagel (Chaplain) on Apr 28, 2003 at 16:36 UTC

    Your format just needs a slight change:

    format STDOUT = @<<<<<< @<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $name, $id, $summary ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $summary .

    The first summary field specification needs a caret instead of the @ to signify fill-mode for that field.

      Thanks. you are right. The summary does works great, but the $name part still print twice:
      abc abc 0987766 ................................... ............................
      Why? I checked I did not let it print more $name. Please help again and Thanks!!!

        What happens when you print just the $name variable outside of the format statement? Place this in the code before or instead of the write:

        print "Name is: $name that's all.\n";

        It could be that the split is placing the wrong value into the @data array which subsequently gets copied into $name.

        Let us know if that sheds any light.

Re: Repeat print problem
by Improv (Pilgrim) on Apr 28, 2003 at 18:08 UTC
    This isn't directly relevant, but your code can be made more concise.
    foreach (@record) { my ($name, $id, $summary) = split ''; format STDOUT = ... }
    Others have already given you what hopefully you'll need to solve the problem.
Re: Repeat print problem
by pzbagel (Chaplain) on Apr 28, 2003 at 20:10 UTC

    Pardon the duplicate post, but it seems that the default settings on the site are making my reply invisible to most users.

    What happens when you print just the $name variable outside of the format statement? Place this in the code before or instead of the write:

    print "Name is: $name that's all.\n";

    It could be that the split is placing the wrong value into the @data array which subsequently gets copied into $name.

    Let us know if that sheds any light.

Re: Repeat print problem
by allyc (Scribe) on Apr 28, 2003 at 22:26 UTC
    Hi,

    I know this is slightly off topic, but is it possible to direct the output of the format command to a scalar instead of STDOUT?

    I have looked at the documentation and I can’t see anything that will point me in the right direction? Is there another module that might be able to help me?

    Thanks,

    Alistair

      The formline function can be used to do this - The formline function is normally only used internally but allows for formats to be built and returned in the format output accumulator, $^A. Normally, the contents of this variable are outputted to the currently selected filehandle via the write command, but this variable, $^A, can be read and reset directly.

      An example from a previous node which I have written on the subject:

      my @list = (1, 2); # the output format can be stored in a variable prior to # output my $format = '@<<<< @<<<<'; # format the contents of @list as per the format structure # defined in the variable $format, the output to be # stored in the format output accumulator, $^A formline $format, @list; print $^A, "\n"; $^A = "";

      Thus in this manner, through use of the formline function, both the output format and the subsequent formatted output can be retrieved and stored - This method is advantageous in that it can be employed with older installations of Perl. For further information see formline and perlform.

       

      perl -le 'print+unpack("N",pack("B32","00000000000000000000001001010100"))'