Professional Web Applications Themes

Conceptual questions about 'pack' - PERL Beginners

Hi, I am very newbie for using the function 'pack', and here I have some questions : Q1. Can I expect that pack can do this for me ? - compress a text file in smaller size - besize template 'aAuU', anything else tempplate I can use to prepare fix length data ? - if yes, but how do I assuming the block size is? In case, if I write a binary file and I wanna use seek. Q2. Does the below results of code can be explained ? my arr = ('aa', '999', '!!'); my back = (); my ...

  1. #1

    Default Conceptual questions about 'pack'

    Hi, I am very newbie for using the function 'pack', and here I have some questions :

    Q1. Can I expect that pack can do this for me ?
    - compress a text file in smaller size
    - besize template 'aAuU', anything else tempplate I can use to prepare fix length data ?
    - if yes, but how do I assuming the block size is? In case, if I write a binary file and I wanna use seek.

    Q2. Does the below results of code can be explained ?

    my arr = ('aa', '999', '!!');
    my back = ();

    my p = qw/a A Z b B h H c C s S i I l L n N v V j J f d F p P u U w x X/;


    for $x(p)
    { eval "\nprint \"$x ->\"; \back = unpack \"${x}3${x}3${x}3\", \(pack \"${x}3${x}3${x}3\", \arr); print \"\<\$_>\" for \back; print \"\\n\"; " };

    Then I got results :

    < snipped results... >
    b -><110><111><110>
    B -><110><111><110>

    what is b and B's return value stands for now ?

    h -><aa0><999><110>
    H -><aa0><999><110>

    what is h and H's return value stands for now ?
    Why 'aa' returns 'aa', but '!!' return 11 ( or maybe 110 ) ?

    < snipped results... >
    f -><0><999><0><0><0><0><0><0><0>
    d -><0><999><0><0><0><0><0><0><0>
    F -><0><999><0><0><0><0><0><0><0>

    Is that this kind of templates are just fine for handling numbers ?
    The pack way is just like doing something like print 'X' + 1, so got zero result ?
    but, how come the return array contains so many elems ? Where are they from ?
    And I make a guess is have I convert my string to ascii code before I try to pack them ?

    < snipped results... >
    u -><aa999!!><><>

    Why arrays joined while other elems are still given ?

    < snipped results... >

    Q3. Is that I always have to do something more before I do unpack ?
    but not just unpack with the same way as I pack ?

    This is really a uneasy understanding function in perl... I do really very apperciate if anybody would kindly explain some more... and perhaps give some more practical exmple reference...

    TIA



    Bee Guest

  2. #2

    Default Re: Conceptual questions about 'pack'

    Bee wrote: 

    Have you read the doentation for pack?

    perldoc -f pack

    And unpack?

    perldoc -f unpack

    Have you read the pack and unpack tutorial?

    perldoc perlpacktut

     

    You could implement a compression algorithm with pack/unpack, if you really
    wanted to.
     

    You are going to have to explain that in more detail.
     

    You *DO* *NOT* have to use eval() to do that! The format strings are
    interpolated just like any other string.

    for my $x ( p ) {
    print "$x ->";
    my back = unpack "${x}3${x}3${x}3", pack "${x}3${x}3${x}3", arr;
    print "<$_>" for back;
    print "\n";
    }



    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  3. #3

    Default Re: Conceptual questions about 'pack'

    > Have you read the pack and unpack tutorial? 

    Thanks for this, I missed this one.
     
    >
    > You could implement a compression algorithm with pack/unpack, if you really
    > wanted to.
    >  
    >
    > You are going to have to explain that in more detail.
    > [/ref]

    In case, I am doing something like a log with User v TimesOfSignIn. So, user name will set as 30 char long, and the Signin times is about in scope of a normal integer. I wanna make this a simple DB for this but not a list of single files for each user. So I wanna make this doable for ramdom read write, so, that should be in a binary file. I can't do random read write with a text file anyway, right ? So I don't want to use 'A' or 'a' as pack templates.

    However, as the file is in a binary, I think there could be a size benfit for me to compress the data length. Actually, am I on the right point for starrting this ? Any starting hints ? or very simple example perhaprs ?

     
    > print " ->"; back = unpack "333", (pack "333", arr); print "<$_>" for back; print "\n"; " };
    >
    > You *DO* *NOT* have to use eval() to do that! The format strings are
    > interpolated just like any other string.[/ref]

    hehe... sorry for bugging =)
    I made this for other copy paste job for me to eval the template vs result experiment.
    Bee Guest

  4. #4

    Default Re: Conceptual questions about 'pack'

    From: "Bee" <org> 
    > >
    > > You are going to have to explain that in more detail.[/ref]
    >
    > In case, I am doing something like a log with User v TimesOfSignIn.
    > So, user name will set as 30 char long, and the Signin times is about
    > in scope of a normal integer. I wanna make this a simple DB for this
    > but not a list of single files for each user. So I wanna make this
    > doable for ramdom read write, so, that should be in a binary file.[/ref]

    You want to have a look at DBM files. Read
    perldoc DB_File
    or
    perldoc SDBM_File

    Or you may try to install DBD::SQLite. That would give you the full
    power of SQL without having to install anything but the module.
     

    "Premature optimization is the root of all evil."

    How many users are we talking about? Millions? I would not sweat over
    a few KB you might save.


    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  5. #5

    Default Re: Conceptual questions about 'pack'

    > > > > - besize template 'aAuU', anything else tempplate I can use to 
    > >
    > > In case, I am doing something like a log with User v TimesOfSignIn.
    > > So, user name will set as 30 char long, and the Signin times is about
    > > in scope of a normal integer. I wanna make this a simple DB for this
    > > but not a list of single files for each user. So I wanna make this
    > > doable for ramdom read write, so, that should be in a binary file. [/ref]
    >
    > You want to have a look at DBM files. Read
    > perldoc DB_File
    > or
    > perldoc SDBM_File
    >
    > Or you may try to install DBD::SQLite. That would give you the full
    > power of SQL without having to install anything but the module.
    >  
    >
    > "Premature optimization is the root of all evil."
    >
    > How many users are we talking about? Millions? I would not sweat over
    > a few KB you might save.[/ref]

    Yes, that's very right, I should have to do that with some db modules!!! I
    am sorry I missed something to declare here . I just do it as an experiment
    to know and learn how pack works in this ways.

    So, I still looking for a pack way to get this job done. Actaully, I am still
    quite confuse on what I can expect pack and unpack can giving help to me,
    in furture. I guess I would have some light to know what pack is while this
    get done.

    Would you help for a little bit more ? Thanks in advise.


    Bee Guest

  6. #6

    Default Re: Conceptual questions about 'pack'

    Bee wrote: 
    >>
    >>You want to have a look at DBM files. Read
    >> perldoc DB_File
    >>or
    >> perldoc SDBM_File
    >>
    >>Or you may try to install DBD::SQLite. That would give you the full
    >>power of SQL without having to install anything but the module.
    >> 
    >> 
    >>
    >> "Premature optimization is the root of all evil."
    >>
    >>How many users are we talking about? Millions? I would not sweat over
    >>a few KB you might save.[/ref]
    >
    >
    > Yes, that's very right, I should have to do that with some db modules!!! I
    > am sorry I missed something to declare here . I just do it as an experiment
    > to know and learn how pack works in this ways.
    >
    > So, I still looking for a pack way to get this job done. Actaully, I am still
    > quite confuse on what I can expect pack and unpack can giving help to me,
    > in furture. I guess I would have some light to know what pack is while this
    > get done.[/ref]

    pack and unpack simply provide a way to translate the bits of a string
    in a specific way. eg. you can look at a string of bits as a series of
    32-bit signed integers or you can look at the same string as a series of
    8-bit characters. (It's sort of like a casting operator in C/C++.) It
    provides a way of looking at or storing data in a very specific way.
    It's primary use is looking at or creating binary data to be shared with
    other (C-like) applications.

    Randy.
    Randy Guest

  7. #7

    Default RE: Conceptual questions about 'pack'

    Bee wrote: [/ref]
    > ...
    > Yes, that's very right, I should have to do that with some db
    > modules!!! I am sorry I missed something to declare here . I just do
    > it as an experiment to know and learn how pack works in this ways.[/ref]

    What you're talking about here is serialization of data structures. Look
    into the MLDBM module or the newer Tie::MLDBM module.
    Bob Guest

Similar Threads

  1. Replies: 4
    Last Post: November 11th, 08:33 AM
  2. Conceptual -- Spam stopper script
    By Dan Muey in forum PERL Beginners
    Replies: 10
    Last Post: September 27th, 08:23 PM
  3. OT: Conceptual -- Spam stopper script
    By Jerry Rocteur in forum PERL Beginners
    Replies: 3
    Last Post: September 27th, 07:19 PM
  4. 2 questions :) - portal questions
    By John Weinshel in forum FileMaker
    Replies: 3
    Last Post: July 14th, 04:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139