Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Text::Autoformat

by GrandFather (Saint)
on Apr 28, 2006 at 20:06 UTC ( [id://546374]=note: print w/replies, xml ) Need Help??


in reply to Text::Autoformat

Your immediate problem is that x doesn't do what you want. To achieve what you want ("doubling" the commands) you need something like:

push @array, ('@', '@') if scalar(@array) < 10; splice @array, -2, 2 if scalar(@array) >= 10;

However that is only a tiny part of the problem. Your bigest problem is that an array is not appropriate, you need to use a string. Something like this:

use strict; use warnings; use Text::Autoformat; my $str=qw/@/; for my $i(1..10){ $str = autoformat ($str, { justify => 'center' }); print "$str\n"; $str .= "@@" if length ($str) < 10; substr $str, -2, 2, '' if length ($str) > 10; }

However that still fails because you clobber $str each time through the loop. Changing the first two lines in the loop to :

my $newStr = autoformat ($str, { justify => 'center' }); print "$newStr\n";

gets you closer, but leaves another logic problem (bug) which I'll leave you to puzzle over. :)


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Text::Autoformat
by Andrew_Levenson (Hermit) on Apr 28, 2006 at 20:23 UTC
    Aha! That was interesting. I failed to notice that when figuring it out initially, but... huh. Thanks for your help!
    use strict; use warnings; use Text::Autoformat; my $str=qw/@/; for my $i(1..11){ my $newStr = autoformat ($str, { justify => 'center' }); print "$newStr\n"; $str .= '@@' if $i <= 5; substr $str, -2, 2, '' if $i > 5; }

      The trouble now is that the output is triple spaced. You can improve it to double spaced by removing the new line from the print. At the end of the day however Autoformat is not doing a good job for you. Nice idea, but no banana.

      I suspect in general that competitions don't expect you to have access to a full range of CPAN modules and that there is generally a reasonable way of solving the problem without resorting to a module such as Text::Autoformat.

      The single print version can be done without much modification of your current code and not using Text::Autoformat.


      DWIM is Perl's answer to Gödel
        In the competition, we can't use any modules that are not already on our system. (Unless we write our own, they have to come with the language initially.) So until I get this over with, i'm going to stick to what I know and what works. Then I figure out how to do it better. Thanks for all of your help and insight.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://546374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found