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

Re: [PBML] merging multiple files

Expand Messages
  • RSS
    The problem seems to be in file read read(INPUTFILE, $data, $size, $offset); The offset here i guess is the offset to place the read data at some other
    Message 1 of 2 , Aug 1, 2002
    • 0 Attachment
      The problem seems to be in file read "read(INPUTFILE, $data, $size,
      $offset);"

      The offset here i guess is the offset to place the read data at some other
      place other than the beginning of the $data and not the file offset.

      You can use "seek" to move to that file position where offset can be
      specified.

      - RSS




      "mustang97_gt"
      <mustang97_gt@ To: perl-beginner@yahoogroups.com
      yahoo.com> cc:
      Subject: [PBML] merging multiple files
      2002/08/02
      04:11
      Please respond
      to
      perl-beginner






      Hi all,

      I'm having trouble with merging multiple files together.
      Here is a small example of what I'm trying to do:

      file1.dat file2.dat file3.dat
      12345 abcde ABCDE
      67890 fghij FGHIJ
      12345 klmno KLMNO
      67890 pqrst PQRST
      12345 uvwxy UVWXY

      Given an order list (2, 3, 1) and a size number like 1,
      I'd like to get an output file that looks like

      dataout.dat
      aA1bB2cC3dD4eE5
      fF6gG7hH8iI9jJ0
      ...
      ..


      The code I have now is as follows:
      #!/usr/local/bin/perl5

      @order = (2,3,1);
      $size = 1;

      #place the file in the proper order
      for($i=0; $i<@order ;$i++) {
      $number = $order[$i];
      $filename[$i] = "file".$number.".dat";
      # printf("file $i = $filename[$i]\n");
      }

      open(OUTPUTFILE, ">dataout.dat");
      #I'd like to loop until EOF but don't know how
      for ($j=0;$j<10; $j++) {
      for($i=0; $i<@order ;$i++) {
      $data = "";
      open(INPUTFILE, $filename[$i]);
      $offset = $size*$j;
      read(INPUTFILE, $data, $size, $offset);
      $data =~ s/\n//g;
      print(STDOUT "data = $data \n");
      print(OUTPUTFILE "$data");
      close(INPUTFILE);
      }
      }
      close(OUTPUTFILE);


      The output that I get for this case though is
      dataout.dat
      aA1aA1aA1aA1aA1aA1aA1aA1aA1aA1

      Can anyone tell me what errors I've made in the code?
      Am I not reading the file properly? It looks like
      the offset is not working to me. If anyone has any suggestions
      on how to accomplish this task, perhaps in an easier way,
      I'd greatly appreciate it.

      Thanks in advance,
      another perl newbie


      Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    • Sudarshan Raghavan
      ... This can actually be written as for (@order) { # The special variable $_ contains the value of order # currently being looped over. You can also say # for
      Message 2 of 2 , Aug 3, 2002
      • 0 Attachment
        On Thu, 1 Aug 2002, mustang97_gt wrote:

        > #!/usr/local/bin/perl5
        >
        > @order = (2,3,1);
        > $size = 1;
        >
        > #place the file in the proper order
        > for($i=0; $i<@order ;$i++) {

        This can actually be written as
        for (@order) {
        # The special variable $_ contains the value of order
        # currently being looped over. You can also say
        # for my $ind (@order), now $ind will contain the value instead of $_
        # perldoc perlsyn,

        >     $number = $order[$i];
        >     $filename[$i] = "file".$number.".dat";
        > #   printf("file $i = $filename[$i]\n");
        > }
        >
        > open(OUTPUTFILE, ">dataout.dat");
        > #I'd like to loop until EOF but don't know how

        open (FILE1, "file1.dat") or
        die "Cannot open file1.dat for reading: $!\n";
        while (<FILE1>) { print "$_\n"; }
        close (FILE1);

        When you have reached EOF the read attempt <FILE1> on that filehandle will
        return a undef, undef evaluates to false.

        > for ($j=0;$j<10; $j++) {
        >     for($i=0; $i<@order ;$i++) {
        >       $data = "";
        >       open(INPUTFILE, $filename[$i]);
        >       $offset = $size*$j;
        >       read(INPUTFILE, $data, $size, $offset);

        The offset arg of read does not do what you are expecting of it. Let me
        explain it with a e.g.

        my $data;
        open (FILE1, "file1.dat") or
        die "Failed to open file1.dat for reading : $!\n";
        # Tip 1: You are not checking if open failed, always remember to do that

        read (FILE1, $data, 1, 0); # This will store 1 into $data, expected
        close (FILE1);

        open (FILE2, "file2.dat") or
        die "Failed to open file2.dat for reading : $!\n";

        read (FILE2, $data, 1, 1);
        # You are expecting this to start the read from offset 1 inside file2.dat,
        # but this actually is the offset within $data. Run this example and you
        # will see that $data now contains '1a'. If your statement had been
        # read (FILE2, $data, 1, 0) the '1' in $data will be overwritten with 'a'
        # There, an explanation on how the OFFSET arg applies to $data and not
        # FILE2. (perldoc -f read)
        close (FILE2);

        >       $data =~ s/\n//g;

        There is a perl function that does this for you, chomp (perldoc -f chomp)
        chomp removes any trailing newline, actually the contents of the special
        perl variable $/ (perldoc perlvar)

        What you want is a combination of seek (perldoc -f seek) and read. Note:
        you are also opening and closing the file to read $size characters, if
        it is a small enough file you can read the entire file into a buffer and
        do the manipulations there.
      Your message has been successfully submitted and would be delivered to recipients shortly.