Loading ...
Sorry, an error occurred while loading the content.
 

Re: [PBML] Cutting columns fm a txt file w/PERL

Expand Messages
  • Charles K. Clarkson
    ... I think this could be written shorter, but I was going for clarity. I used a while loop instead of arrays, to avoid a lot of memory use for long files. sub
    Message 1 of 3 , Jun 2, 2001
      <ericvia@...> asked:

      : Please bear with a beginner -
      :
      : I am FORCING myself to learn PERL, so although I
      : know I can do what I want to do using AWK, I want
      : to be able to do such things in PERL scripts
      : - but cannot easily understand it as well.
      :
      : If I have a text file like so, called -
      : "story.txt", that reads like:
      :
      : 48 dogs barking at house
      : 27 mules living in trees
      : 19 elephants eating the grass
      :
      : And I want to split each into it's own separate
      : txt file, so I end up with:
      :
      : one txt file with the first word called perhaps
      : "howmany.txt"
      : 48
      : 27
      : 19
      :
      : and the second, perhaps "animal.txt"
      : dogs
      : mules
      : elephants
      :
      : and so on - you get the picture I think - I could
      : do easily what come to mind easily in AWK:
      :
      : awk '{print $1} story.txt > howmany.txt
      : awk '{print $2} story.txt > animal.txt
      : and so on down the line.
      :
      : I am going to GUESS with my limited knowledge
      : of PERL and say that I would instead read in
      : "story.txt" as an array? Yes? And I could name
      : each part of the array? - and once that's done
      : it's a matter of doing anything I want with each
      : part of the array correct? So - (thinking out
      : loud here) - I can "open" the story.txt,
      : and...and....and then....why
      : I'll.......I'll.......Okay - this is where my
      : ideas run out I'm afraid - I'm just hoping that
      : perhaps PERL can do this as easily as AWK can?
      :
      : But then, - dare I saw that if PERL (or awk
      : for that matter) can be able to split up the
      : words like I want to write them to text files,
      : why can't he just work with right where they're
      : at!?? (But then perhaps that would be too much
      : for me right now and I can do that later)

      I think this could be written shorter, but I
      was going for clarity. I used a while loop instead
      of arrays, to avoid a lot of memory use for long
      files.

      sub columns_to_files ($$);

      columns_to_files 'in.txt', ['col1.txt', 'col2.txt', 'col3.txt'];

      sub columns_to_files ($$) {
      my $source_file_name = shift;

      # get a hash of file handles:
      my $file_names = shift;
      my %file_handle;
      foreach (@$file_names) {
      open my $fh, ">", $_ or die "Uh-Oh! $_: $!";
      $file_handle{$_} = $fh;
      }

      open my $fh, $source_file_name
      or die "Cannot open $source_file_name: $!";
      while (<$fh>) {
      chomp;
      my %columns;
      @columns{@$file_names} = split;
      print { $file_handle{$_} } "$columns{$_}\n"
      foreach @$file_names;
      }
      }

      I didn't do any error checking or file locking.
      If you want a line by line explanation - just ask.


      HTH,
      Charles K. Clarkson


      Under capitalism man exploits man;
      Under socialism the reverse is true.
      - Polish Proverb
    • Eric Via
      On 6/2/01, with the clock reading 7:43 PM -0500, Charles K. Clarkson took a sip of coffee, stared into the email screen, and with the greatest of confidence
      Message 2 of 3 , Jun 2, 2001
        On 6/2/01, with the clock reading 7:43 PM -0500, Charles K.
        Clarkson took a sip of coffee, stared into the email screen, and
        with the greatest of confidence and grace, and at around 160 words a
        minute, began to type: :

        [snip]

        >
        > I didn't do any error checking or file locking.
        >If you want a line by line explanation - just ask.

        YOU bet I will - THANK YOU! I most definately will go over what
        you've written and try to understand each line - I love this list
        because there 's such great knowledge and such friendly people - and
        I don't want to just - "ask, take their script, and run" - 'cause I
        GOTS TA LEARN!

        THANK you Charles!!! I'll get back to you asap!

        Eric
        PS - what's "HTH" in your signoff?
        --
        ---------------------------
        Eric Via
        ericvia@...
        My home page: http://web2.airmail.net/ericvia/ericpage.html
        (Buddhism, VW New Beetle, Mercedes, Spam, D.B. Cooper, Trains, & more!)
        -----------------------------
        It is not our preferences that cause problems but our attachment to
        them. - Buddha
      • Charles K. Clarkson
        Eric Via ... Well, here in Dallas it might mean Hotter Than Heck , but I mean Hope That Helps . HTH, Charles K. Clarkson The Earth is
        Message 3 of 3 , Jun 2, 2001
          Eric Via <ericvia@...>

          : PS - what's "HTH" in your signoff?

          Well, here in Dallas it might mean 'Hotter Than
          Heck', but I mean 'Hope That Helps'.

          HTH,
          Charles K. Clarkson


          The Earth is degenerating today.
          Bribery and corruption abound.
          Children no longer obey their parents,
          every man wants to write a book (?),
          and it is evident that the end of the
          world is fast approaching.
          - Assyrian tablet, c. 2800 BC
        Your message has been successfully submitted and would be delivered to recipients shortly.