From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 08 March 2006 16:55 To: [email protected] Subject: Trouble with File::Slurp - ing into a hash
> Wizards, > > I'm trying to borrow a page from perl.com's "Perl Slurp-Eze" piece by Guttman > (http://www.perl.com/lpt/a/2003/11/21/slurp.html). What I'm trying to do is adapt the following for my data: > > my $text = read_file( $file ); > my %config = $text =~ /^(\w+)=(.+)$/mg; > > my data files consist of a header block of comments, followed by data lines (text followed by zero or more > blanks, a '|' separator, and a number), followed by POD documentation (here's an abbreviated sample): > > ######################################################################## ######## > # - Copyright 2004, 2005, 2006 All Rights Reserved # > # # > # Name : AP_Tags_XP.lst # > # # > # Description : This is the tag (key/value set) file used by the project's # > # HTML-file update script for Windows XP machines only. # > # # > # . . . # > ######################################################################## ######## > > Date Column Updated |0 > Software Release Number |1 > Windows Type-Version (Build) |2 > Service Pack |3 > Anybodys Software - SomeProgram.exe |4 > > # Multiple labels for line 5: > ActiveState Tool Corp. - ActivePerl |5 > ActiveState, a division of Sophos - ActivePerl |5 > > > =pod > > =head1 > > And here's the code I'm trying to use: > > use strict; > use File::Slurp; > > my $key_file = 'AP_Tags_XP.lst'; > > open my $LOG, '>', 'tsthash3.log'; > > my $text_of_file = read_file( $key_file ); > > print $LOG "--------------------------------------------------------------\n"; > print $LOG "$text_of_file\n"; > print $LOG "--------------------------------------------------------------\n\n"; > > my %key = $text_of_file =~ /^([\w|\s]+[\w])\s*\|(\d+)$/mg; > > my $x = scalar( %key ); > my $y = keys( %key ); > > print $LOG "for hash \%key: scalar = $x ($y elements)\n\n"; > > foreach my $k (sort {$key{$a} <=> $key{$b}} keys %key) { > print $LOG "$k: $key{$k}\n"; > } > > The dump of $text_of_file shows that the file read in fine, but I get 0 for the scalar(%key) and 0 for keys(% > key). What am I doing wrong, anybody know? If it helps, each line in the dump ends in a 0x0d (little musical > note) character... Your regex is broken. First, there seems to be some white space at the end of each line, so lose the end of line anchor. Second \w doesn't include some of the characters in your data. Try /^(.+?)\s+\|(\d+)/mg. HTH -- Brian Raven ================================= Atos Euronext Market Solutions Disclaimer ================================= The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful. If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions. L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions. _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
