Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Binary data handling with unpack in Perl

During today's Perl course, I was asked to provide an example of the unpack function for extracting multiple values from a piece of data - typically binary data extracted from a file into a scalar variable.


Dorothy-2:de$ perl imgsize sd*.gif
sd1.gif is GIF, 89a, 450 by 250
sd2.gif is GIF, 89a, 250 by 300
sd3.gif is GIF, 89a, 300 by 250
sd4.gif is GIF, 89a, 333 by 333
Dorothy-2:de$



And what better images to illustrate the article that ones of the delegates on the course?


Here's the program:

for $file(@ARGV) {
  open (FH,$file);
  read (FH,$header,10);
  ($type,$st,$wid,$hei) = unpack("a3a3vv",$header);
  print ("$file is $type, $st, $wid by $hei\n");
}


From memory (!) I recalled that a .gif file starts with 3 bytes to confirm that it's a GIF file, 3 bytes to tell us which subformat has been used, and 2 2-byte 16 bit unsigned integers, bigendian, which tell the width and height of the image in pixels.

You'll notice that the course had a lot of delegate on it. Where there's a high number of delegates, all at a similar level, all from the same company, and all doing similar applications, I will let the customer place up to 20 delegates on a course. Where the delegates background differs, I request a reduced top number. And on a public course, where people from differnt companies book, we restrict delegates to just 8 so that there's a good opportunity for the tutor to talk to each and every delegate about their own individual use of the subject being taught.


P.S. There's another example of unpack on a .gif file - [here] - that I wrote a wile back; the techniques used are slightly different, and the program is one that came from a series named after British Rivers - in this case the Ystwyth from Aberystwyth fame.

(written 2010-06-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
G401 - Well House Consultants - On Site courses
  [4739] A year of changes for Lisa and Graham Ellis, and Well House - (2017-05-27)
  [4602] Training course and hotel room prices - 2016 - (2015-12-31)
  [4230] Well House Consultants - course prices for 2014 - (2013-12-26)
  [4132] Using your own laptop on our courses - now even easier! - (2013-07-05)
  [3950] 10 reasons why I travel the night before a course starts - (2012-12-10)
  [3898] The course must go on - improvements to tutor travel plans, with immediate effect - (2012-10-17)
  [3715] Changing face - Filton - (2012-05-01)
  [3691] Back in Cambridge to give a Lua course - (2012-04-09)
  [3137] Training Classes - should the training company provide a system for each delegate to use? - (2011-01-18)
  [3131] Lua, Tcl, Python; Worldwide training classes - (2011-01-14)
  [2956] On site course - travel and accommodation expenses - (2010-09-14)
  [2928] Public (scheduled) or private course? Book direct, or through a third party? - (2010-08-19)
  [2810] A course review - for the tutor to complete - (2010-06-14)
  [2311] Floor to ceiling - (2009-07-30)
  [2309] Lua training class in Spanish - (2009-07-29)
  [2041] httpd, Tomcat and PHP course enhancements - (2009-02-14)
  [1781] Reception - (2008-09-01)
  [1600] Cambidge - Tcl, Expect and Perl courses - (2008-04-04)
  [1465] Perl, PHP, Python, Tcl, Linux, MySQL, Ruby courses ... - (2007-12-09)
  [1027] Cue the music, I'm happy. - (2007-01-09)
  [439] You cant - (2005-09-08)
  [438] Fair system for travel and accommodation expenses - (2005-09-07)
  [382] Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18)
  [241] Dress for training - (2005-03-10)
  [213] Off to Munich - (2005-02-13)
  [191] Setting up a training room for a course - (2005-01-27)
  [139] Just provide a room and the students - (2004-12-03)

P212 - Perl - More on Character Strings
  [4452] Binary data handling - Python and Perl - (2015-03-09)
  [3927] First match or all matches? Perl Regular Expressions - (2012-11-19)
  [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
  [3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
  [3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
  [3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
  [3322] How much has Perl (and other languages) changed? - (2011-06-10)
  [3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2874] Unpacking a Perl string into a list - (2010-07-16)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
  [2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
  [2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
  [1947] Perl substitute - the e modifier - (2008-12-16)
  [1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1251] Substitute operator / modifiers in Perl - (2007-06-28)
  [1230] Commenting a Perl Regular Expression - (2007-06-12)
  [1222] Perl, the substitute operator s - (2007-06-08)
  [943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [608] Don't expose your regular expressions - (2006-02-15)
  [597] Storing a regular expression in a perl variable - (2006-02-09)
  [586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
  [583] Remember to process blank lines - (2006-01-31)
  [453] Commenting Perl regular expressions - (2005-09-30)


Back to
The Merchants And
Previous and next
or
Horse's mouth home
Forward to
After the Perl course in Nurnberg
Some other Articles
How are you getting on?
Regular Expression Myths
Travelling across Europe
After the Perl course in Nurnberg
Binary data handling with unpack in Perl
The Merchants And
Nuremberg - some pictures
Perl - skip the classics and use regular expressions
Sur la T.G.V.
Sunday - waiting for the ferry.
4728 posts, page by page
Link to page ... 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 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2801_Bin ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat