perlhaq1::Data Manipulation

Information on data types and dope manipulation of them.


Recommendation
If you need to know about basic data manipulation, data types, etc., read perldata or perlfaq4 (Data Manipulation).
Also, for all kinds of different base conversions, definately check out cseg's 2.pl. It's a very useful program and I find a use for it daily.

Data Conversion
There are a bunch of different functions for data conversion. Here are a few:

  • pack (I'll add some documentation here later on how to use pack and unpack)
  • unpack
  • sprintf
  • hex
  • chr
  • ord
    I recommend checking out cseg's 2.pl. It has a very wide range of conversions and is a very helpful tool written in perl.

    Permutation
    Permutation allows you to take a list and get every possible combination of the values in the list. You can use Algorithm::Permute which uses C or check out this example code that permutes through a list.

    $perm = newcombo main(@list);
    while (@comb = $perm->nextcombo) {
     print join(", ", @comb) . "\n";
    }
    
    sub newcombo {
     $class = shift;
     $list = [ @_ ];
     bless [$list, [0 .. $#$list]], $class;
    }
    
    sub nextcombo {
     $self = shift;
     $list = $self->[0];
     $tot = $self->[1];
     return unless @$tot;
     @next = @$tot;
     @end = pop @next;
     while (@next && $next[-1] > $end[-1]) {
      push(@end, pop(@next));
     }
     if (defined($extra = pop(@next))) {
      ($place) = grep $extra < $end[$_], 0 .. $#end;
      ($extra, $end[$place]) = ($end[$place], $extra);
      $self->[1] = [@next, $extra, @end];
     }
     else {
      $self->[1] = [];
     }
     return @$list[@$tot];
    }
    


    Changes

    August 29, 2001 - I began writing this.


    Author

    That would be me, Samy Kamkar. You can reach me at CommPort5@LucidX.com or on IRC on SUIDnet in #suid with the IRC name 'CommPort5'.