how to write it in one line..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fetaelin
    New Member
    • May 2007
    • 16

    how to write it in one line..

    Hi I have a litle script and want to write it in one line
    I dont know how to do that .. here is the script.
    Code:
    #!/usr/bin/perl
    open(FILE1, "<marenden");
    open(FILE3, ">>result");
    
    while($line = <FILE1>) {
      chop($line);
      $temp=`grep $line file2.txt`;
      print FILE3 $temp;
    }
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    One Line :)

    [CODE=perl]
    #!/usr/bin/perl
    open(FILE1, "<marenden" ); open(FILE3, ">>result") ; while($line = <FILE1>) {chop($line); $temp=`grep $line file2.txt`; print FILE3 $temp;}
    [/CODE]

    - Miller

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Hi Fetaelin,

      As you already know, it often is possible to create one-liners for a lot of the most typical parsing needs. However, I generally avoid this as anything more than a curiousity.

      In fact your currect script, if it's hoping to do what I think it is, is actually broken. I believe that the most important goal should be to make clear readable code, not ultra compact.

      To that end, here is how I personally would do the project that you have listed:

      [CODE=perl]
      #!/usr/bin/perl

      use File::Slurp qw(read_file);

      use strict;

      my $file1 = 'marenden';
      my $file2 = 'file2.txt';
      my $outfile = 'result';

      my %match = map {$_ => 1} grep {! /^\s*$/} read_file($file 1);

      open(IN, $file2) or die "Can't open $file2: $!";
      open(OUT, ">>$outfile ") or die "Can't open $outfile: $!";

      while (<IN>) {
      print OUT if $match{$_};
      }

      close(IN);
      close(OUT);
      [/CODE]

      As you can see, this is actually more lines of code because of the separation of the file name declarations. It also would be longer still if I hadn't used the File::Slurp module as a shortcut for reading in $file1. However, this code is more readable, and definitely easier to debug.

      I suggest that you first aim to write a script like this before you obsess about one-liners.

      - Miller

      Comment

      • fetaelin
        New Member
        • May 2007
        • 16

        #4
        What I ment was how to write it in one line in a command prompt and not have it as an script.
        ex.:
        bash$: perl the scrit as one line here...

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          I know. All my previous statements still apply.

          - Miller

          Comment

          • lokeshrajoria
            New Member
            • Jun 2007
            • 35

            #6
            hello
            if you want write in one line at the command prompt so write like this

            CODE
            $>perl -e 'open(FILE1, "<marenden" ); open(FILE3, ">>result") ; while($line = <FILE1>) {chop($line); $temp=`grep $line file2.txt`; print FILE3 $temp;}'

            regard'd
            Lokee.......

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              Here's the closest that I believe is possible:

              >perl -e"use File::Slurp ':all';%m=map{$ _=>1}read_file( 'a.txt');append _file('c.txt',g rep{$m{$_}}read _file('b.txt')) "

              Note that it does require File::Slurp, which is not a core module.

              - Miller

              Comment

              Working...