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

Re: [PBML] Process binary files

Expand Messages
  • Hans Ginzel
    ... Did not benchmark it, but you can try: ($WhereIam, $CmdName) = $0 =~ m#^(.*[/ ]|[A-Z] :)?(.+)$#i; $opt{debug}++; $BASNAME = out_ ; $SPLIT_STRING =
    Message 1 of 6 , Nov 1, 2002
    • 0 Attachment
      On Thu, Oct 31, 2002 at 02:43:46PM -0000, matt_johnsson wrote:
      > I have a perl script that processes a binary file with
      > variable lenght records in it. I'm trying to split it
      > into several binary files, one per record found (using a part of
      > the data to build the filename).
      > Each record starts with the string "HEADER".

      Did not benchmark it, but you can try:

      ($WhereIam, $CmdName) = $0 =~ m#^(.*[/\\]|[A-Z]\:)?(.+)$#i;

      $opt{debug}++;
      $BASNAME = "out_";
      $SPLIT_STRING = "HEADER";

      # perldoc perlvar
      undef $/; # enable "slurp" mode
      @parts = split /$SPLIT_STRING/o, <>; # whole file now here
      shift @parts; # throw out the first epmty part

      foreach (@parts) {
      ($filename = substr, $_, 22, 20) =~ s/\s*$//; # check indexes yourself
      warn "filename = `$filename'" if $opt{debug};
      -e ($filename="$BASNAME.$filename") and next; # bild filename, skip writting file, if it exists;

      open OUT, ">$filename" or die "$CmdName: Cannot open output file `$filename': $!.\n";
      # binmode OUT; # depends on your OS, see perldoc -f binmode
      print "$SPLIT_STRING$part"; # do not forget the cut off "HEADER" string by splitting
      close OUT or die "$CmdName: Cannot close output file `$filename': $!.\n";
      }

      # Take care of endianity (perldoc -f pack).

      > Regards

      Hans
    • Triphuong Nguyen
      ... From: Hans Ginzel [mailto:hans@matfyz.cz] Sent: Friday, November 01, 2002 1:31 AM To: perl-beginner@yahoogroups.com Subject: Re: [PBML] Process binary
      Message 2 of 6 , Nov 1, 2002
      • 0 Attachment
        -----Original Message-----
        From: Hans Ginzel [mailto:hans@...]
        Sent: Friday, November 01, 2002 1:31 AM
        To: perl-beginner@yahoogroups.com
        Subject: Re: [PBML] Process binary files



        On Thu, Oct 31, 2002 at 02:43:46PM -0000, matt_johnsson wrote:
        > I have a perl script that processes a binary file with
        > variable lenght records in it. I'm trying to split it
        > into several binary files, one per record found (using a part of
        > the data to build the filename).
        > Each record starts with the string "HEADER".

        Did not benchmark it, but you can try:

        ($WhereIam, $CmdName) = $0 =~ m#^(.*[/\\]|[A-Z]\:)?(.+)$#i;

        $opt{debug}++;
        $BASNAME = "out_";
        $SPLIT_STRING = "HEADER";

        # perldoc perlvar
        undef $/; # enable "slurp" mode
        @parts = split /$SPLIT_STRING/o, <>; # whole file now here
        shift @parts; # throw out the first epmty part

        foreach (@parts) {
        ($filename = substr, $_, 22, 20) =~ s/\s*$//; # check indexes
        yourself
        warn "filename = `$filename'" if $opt{debug};
        -e ($filename="$BASNAME.$filename") and next; # bild filename,
        skip writting file, if it exists;

        open OUT, ">$filename" or die "$CmdName: Cannot open output file
        `$filename': $!.\n";
        # binmode OUT; # depends on your OS, see perldoc -f binmode
        print "$SPLIT_STRING$part"; # do not forget the cut off "HEADER"
        string by splitting
        close OUT or die "$CmdName: Cannot close output file `$filename':
        $!.\n";
        }

        # Take care of endianity (perldoc -f pack).

        > Regards

        Hans


        Yahoo! Groups Sponsor

        ADVERTISEMENT

        <http://rd.yahoo.com/M=237459.2482214.3917349.2146399/D=egroupweb/S=17050069
        51:HM/A=1267611/R=0/*http://ad.doubleclick.net/jump/N2524.Yahoo/B1071650;sz=
        300x250;ord=1036143089945311?>

        <http://us.adserver.yahoo.com/l?M=237459.2482214.3917349.2146399/D=egroupmai
        l/S=:HM/A=1267611/rand=878897956>

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

        Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
        <http://docs.yahoo.com/info/terms/> .




        [Non-text portions of this message have been removed]
      • prakash
        This code is working fine.. but I am interested in creating CLEAN and BEST code.. I never say my code is the best code.. always there is possibility for
        Message 3 of 6 , Nov 1, 2002
        • 0 Attachment
          This code is working fine.. but I am interested in creating CLEAN and BEST
          code..
          I never say my code is the best code.. always there is possibility for
          improvement
          Please pass your valuable suggestion to improve the code... thank you very
          much..


          #!/usr/bin/perl -w
          $|=1;

          use lib '/home/aebolts/cgi-bin/PM';
          use strict;
          use Pros;
          use CGI::Carp qw(fatalsToBrowser);
          use CGI qw(:standard);

          my $username = cookie('user');
          if($username){
          my $cgi = new CGI;
          my $aPros = Pros->new($username);
          if($cgi->param('add')) {
          if(!$cgi->param('view'))
          { if(!$aPros->addInfo($cgi)){$aPros->printAll();} }
          $aPros->printAddNew();
          }
          elsif($cgi->param('update')) {
          $aPros->update($cgi);
          }
          elsif($cgi->param('aprospect')) {
          if(!$cgi->param('view'))
          { if($aPros->uploadInfo($cgi)){ $aPros->printAll(); } }
          $aPros->printViewProspect();
          }
          elsif($cgi->param('upload')) {
          if($cgi->param('file'))
          { if($aPros->uploadProspects($cgi)){ $aPros->printAll(); } }
          $aPros->printUploadNew();
          }
          else{
          $aPros->printAll($cgi->param('sort'));
          }
          }
          ######## PROGRAM HAS NO COOKIE TO EAT ##############################
          else{
          print header();
          print qq~<meta http-equiv="refresh" target="new"
          content="0;URL=http://www.asdfasdf.com">~;
          }

          exit(0);
        Your message has been successfully submitted and would be delivered to recipients shortly.