Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Unpack and Print NEW

by johngg (Canon)
on Aug 23, 2017 at 22:08 UTC ( [id://1197894]=note: print w/replies, xml ) Need Help??


in reply to Unpack and Print NEW

Further to the comments from hippo and afoken, I'm a little puzzled by your input (when downloaded) and your expected output as the alignment seems inconsistent. Have you perhaps pasted some TAB characters in there?

Regarding your code:-

  • Use of lexical rather than package file handles is now recommended and I would suggest that the error messages on open failure be differentiated with the file name so that you know immediately which failed rather than having to do some detective work.

  • Your

    while (<FILE>) { $line = $_;

    could more simply be written

    while ( my $line = <FILE> ) {

  • You pad the line to a length of 280 characters but you have forgotten to chomp the line terminator off before padding so you have a line terminator left in the middle.

  • A simpler way to pad lines with spaces is to use pack with the A template, for example:-

    johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $text = q{abcd}; say qq{>$text<}; $text = pack q{A10}, $text; say qq{>$text<};' >abcd< >abcd < johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my $text = q{abcd}; say qq{>$text<}; $text = pack q{A2}, $text; say qq{>$text<};' >abcd< >ab<

  • Rather than unpacking into several scalars do so into an array and then apply double quotes to the relevant items using an array slice, something like this:-

    johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my @items = q{a} .. q{g}; s{(.*)}{"$1"} for @items[ 0, 1, 3, 5, 6]; say for @items;' "a" "b" c "d" e "f" "g"

  • Rather than multiple print statements, perhaps use a single say (if running perl 5.10 or later) and join all the items together with commas in one fell swoop, something like

    say join q{,}, @items;

I hope these pointers are helpful and you are able to move towards a solution. Please ask further if difficulties persist.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found