Re: formline and right-justification
by freakingwildchild (Scribe) on Jun 18, 2006 at 15:04 UTC
|
Ok, quick and dirty script, does this do what you want it to do ?
#!/usr/bin/perl
use strict;
use warnings;
my ($string) = 'abcdefghijklmnopqrstuvwxyz';
my ($fragment) = substr($string, length($string) - 7);
print "...$fragment\n";
| [reply] [d/l] |
|
|
No need for the length call -- you can just do: substr($string, -7)
| [reply] [d/l] [select] |
|
|
hehe, good approach, freakingwildchild.
of course this would do what i need, but it assumes one has
the width of the output terminal, which i do not have.
since there are various ways to get this info, i'll use your snippet, if no solution is given including a formline-format string.
thank you :)
| [reply] |
|
|
my mistake. of course i know the width. sorry, you were right here :)
| [reply] |
Re: formline and right-justification
by sh1tn (Priest) on Jun 18, 2006 at 14:56 UTC
|
You may want to take a look at perldoc -f printf
perl -e 'printf "%10s", "abc"' # ' abc'
| [reply] [d/l] [select] |
|
|
thx sh1tn for your reply, but i think you got me wrong, as
my description of the problem was not complete i guess.
the string can either be longer than the format specification,
or shorter. if it is shorter, it should be left-justified.
if it is longer, it should get cut as usual, but the string's
end should be the output, rather than the the string's beginning. so just what the "..."-format specifier does.
your printf-example does not do that :(
| [reply] |
Re: formline and right-justification
by davidrw (Prior) on Jun 18, 2006 at 15:12 UTC
|
i'm limited to formline (and format), so no additional modules can be included.
Why are you limited & can't use modules?
If it's because this is homework, you should have told us is was.. If it's because you don't have root, be sure to Super Search for how to locally install modules (this comes up a lot).
Also, if you want the output string to be truncated to 10 chars, then use substr ... this demonstrates the difference:
while(<DATA>){
chomp;
print "== $_ ==\n";
printf "printf: [%10s]\n", $_;
printf "substr: [%10s]\n", substr($_, -10);
}
__DATA__
abc
abcdefghjiklmnopqrstuvwxyz
| [reply] [d/l] [select] |
|
|
it seems the substr-approach is the best, as already suggested by freakingwildchild. so i will take this one.
thanks a lot, davidrw :)
PS: no homework, no user+permission-problem.
it should be "portable", this means the users should not be
forced to install any other modules.
the program should run with the modules that
come with perl itself.
| [reply] |
|
|
Y, that's the 3rd common reason people say "i can't use modules" ..
have a look at PAR and its pp script for packaging up all module dependencies for distribution..
| [reply] [d/l] |
|
|