Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Printing Fixed Width Strings and Spliting String into Multiple Lines

by mmartin (Monk)
on Feb 08, 2012 at 17:05 UTC ( #952532=note: print w/ replies, xml ) Need Help??


in reply to Re: Printing Fixed Width Strings and Spliting String into Multiple Lines
in thread Printing Fixed Width Strings and Spliting String into Multiple Lines

Hey GrandFather,

I think I may be going with your suggestion (Nice Work BTW!)... I just had one question about it.

For the line that starts with "$_ = wrapLine($_) for ..." what does the $_ stand for in this case. I tried just to do a print on the line before that statement just to see what the variable is and I get a "Use of Uninitialized value in concatenation (.) or string at..."

Also, the last line of code that starts "return join "\n## ", $line =~ ......", I'm assuming that the right side of the =~ is a regex? Would you be able to explain a little how that works? I would like to have it split to a new line just at the end of the $kMaxLine length instead of a blank space.
And also assuming your using parenthesis as separators instead of the usual "/", and that "m~" stands for multiline and the "~g" stands for gloabl... Is that right?

Sorry for all the extra questions but I'm just one of those people who has to know how it works lol... Thanks again!

Thanks in Advance,
Matt


Comment on Re^2: Printing Fixed Width Strings and Spliting String into Multiple Lines
Re^3: Printing Fixed Width Strings and Spliting String into Multiple Lines
by GrandFather (Cardinal) on Feb 08, 2012 at 21:25 UTC

    Don't apologise for asking questions to find out about stuff you don't know! After all, that is exactly why you are visiting.

    $_ is Perl's default variable. It is used as an alias to the current item for things like grep and map. It is also used in the same way when you use for as a statement modifier such as in $_ = wrapLine($_) for $module_cmd, $cfgmaker_cmd;. I used that trick by the way to save writing out:

    $_ = wrapLine($module_cmd); $_ = wrapLine($cfgmaker_cmd);

    and of course to expose you to some interesting Perl.

    $line =~ m~(.{1,$kMaxLine})(?:\s|(?<![-.\w=/]))~g is a regular expression match. m is the match operator and uses the following non-whitespace character as the quote character for the regular expression string. In this case it's using ~ which I chose to avoid having to quote the / used in the match string. The g on the end is the "global match" flag - match as many times as you can. There is a lot more in that regular expression and I strongly encourage you to read perlretut and perlre to at least get an overview of whet regexes do and look like. You can simplify the regex line to:

    return join "\n## ", $line =~ m~(.{1,$kMaxLine})~g;

    to have it break the line into fixed size pieces without regard to white space or punctuation. You would find it instructive however to read the documentation for regexen engough to be able to figure out how the original regex works. ;)

    True laziness is hard work
      Hey GrandFather,

      Once again thanks for the explanation!

      Thanks for dumbing it down a bit for me lol... Yea I will definitely check out those docs for the regex stuff. I've gotten somewhat decent with regular expressions lately, but like most things "code" unless you wrote it yourself or are that good with the language in question it can be pretty hard to comprehend. But I think I got it now!

      But thanks for your time, I really appreciate it.


      Thanks Again,
      Matt

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-07-28 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls