Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Use 2 files and print each line of each one side-by-side (One-liner)

by jellisii2 (Hermit)
on Feb 18, 2014 at 20:15 UTC ( [id://1075377]=note: print w/replies, xml ) Need Help??


in reply to Re: Use 2 files and print each line of each one side-by-side (One-liner)
in thread Use 2 files and print each line of each one side-by-side

For relative values of simple...
  • Comment on Re^2: Use 2 files and print each line of each one side-by-side (One-liner)

Replies are listed 'Best First'.
Re^3: Use 2 files and print each line of each one side-by-side (One-liner)
by BrowserUk (Patriarch) on Feb 18, 2014 at 20:36 UTC
    For relative values of simple...

    Huh? Relative to what? A plank.

    It's two well-known switches and a print statement.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I seldom do oneliners myself, and never anything this clever, but let me attempt to noodle out what's going on here as an exercise for myself. None of this should come as a surprise to those who have serious depth in perl, particularly those who do lots of CLI work, but it will be enlightening to me.

      -n forces the program to loop while there's still input from a.txt. I assume this also takes into account input from <STDIN> as well in the event b.txt is longer than a.txt, but I'm unclear on that point.

      -l tells perl that a carriage return is the record separator, and chomps each line from a.txt, and sets the output record separator to the same, so each line printed should have a carriage return. This may also chomp input from b.txt, but I'm unclear on that.

      The formatting of the print statement is pretty obvious to me, but for completion's sake, the format is 10 characters (with right padding of spaces if the string is short), a tab, and another 10 characters.

      $_ is the current line from a.txt, and will be used left of the tab in the formatted print.

      I don't understand why scalar is in place for the <STDIN> that is ingesting b.txt from the shell's redirect. Assuming this works as advertised (not that I have any real doubt), it will be used in right of the tab in the formatted print.

      To those of us (it may just be me, who knows) who don't often use the CLI, no, it's not quite as simple as it would seem to you. I get that "knowing the tool" is important, and strive to learn more as I go, but the reality is that there's a LOT baked into perl to learn. Between this fact, the habit to "stick with what works", and that my job nor hobby is %100 perl, I freely admit my education/depth/knowledge may be a bit lopsided. I think this would apply to a majority of the monks in the monastery to some degree.

      Thank you for the enlightenment.

        -n forces the program to loop while there's still input from a.txt.

        Yes but ...

        I assume this also takes into account input from <STDIN> as well in the event b.txt is longer than a.txt, but I'm unclear on that point.

        No. -n doesn't consider STDIN (thus b.txt) at all.

        My assumption was that the two files are the same length as the OP made no mention of what should happen otherwise. But ...

        If B is short than A, the last lines of A will be output with nothing from B.

        If B is longer than A, the last lines of B will just be ignored (never read).

        -l tells perl that a carriage return is the record separator, and chomps each line from a.txt, and sets the output record separator to the same, so each line printed should have a carriage return.

        Yes, but I use printf rather than print which si not affected by the setting of the output record separator.

        This may also chomp input from b.txt, but I'm unclear on that.

        No. The record separator from b.txt is left in place when <STDIN> is read, so it provides the newlines for the output.

        I don't understand why scalar is in place for the <STDIN> that is ingesting b.txt

        Because printf provides a list context which would cause <STDIN> to slurp all of b.txt and append it to the first line of a.txt. By providing a scalar context it ensure that only one line is read from B for each line from A.

        To those of us ... who don't often use the CLI, no, it's not quite as simple as it would seem to you.

        I process lots of unstructured data; hence I use these facilities a lot and so I've become very familiar with this aspect of Perl. I guess it took your analysis of the one-liner to cause me to realise just how much implicit behaviour my "simple" one-liner made use of to do what it does.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

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

    No recent polls found