Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: rectangularizing input to become array

by Athanasius (Archbishop)
on Feb 27, 2019 at 07:31 UTC ( [id://1230602]=note: print w/replies, xml ) Need Help??


in reply to rectangularizing input to become array

Hello Aldebaran,

Just a couple of points in haukex’s excellent answer that I would like to emphasise:

  1. The most important data missing from your SSCCE is your desired output. “A picture is worth a thousand words.”
  2. This line:
    my @lines = $path_to_file->slurp;
    almost certainly doesn’t do what you think it does. The documentation for Path::Tiny::slurp says that it “Reads file contents into a scalar.” So after that line of code is executed, the array @lines contains a single entry, identical to the string previously assigned to $guts. To get an array of lines, you need to split the string, either on newlines as haukex showed:
    my $guts = $path_to_file->slurp; my @lines = split /\n/, $guts;
    or using the special multiline pattern documented in split:
    my $guts = $path_to_file->slurp; my @lines = split /^/, $guts;
    The latter preserves newlines in the input data, including blank lines at the end of the input file; the former does not.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: rectangularizing input to become array
by Aldebaran (Curate) on Feb 28, 2019 at 19:58 UTC
    my @lines = $path_to_file->slurp; almost certainly doesn’t do what you think it does.

    It did not. I've been whittling this SSCCE down from a river of mojibake and woe, my soaked and freezing body wondering where my skills to deal with such environments have been. Well, getting the logic from Path::Tiny wrong was one thing that had me beat. I was reading this as:

    @lines = $file->lines;

    , which, I believe would produce different results. It's very difficult to diagnose path and file input problems from the net, but you and haukex have done exactly that. Thank you.

    What can the OP do about misapprehension? (Open question) I would like to introduce a little bit of code to test whether I have these data represented correctly. I frequently find that I'm off by a pair of square brackets or quotes and commas. I'll use readmore tags for output then new source for the caller.

    Thanks all for comments,

    2019-03-01 Athanasius changed one set of pre tags to code tags

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found