I need some explanation on following: 1. my skill of perl tells me so far to handle exceptions and write : open(FH,$file) || die "can not open $file : $! \n"; I've tried your code with a wrong file_name and got following message: "Can't open in.txt1: No such file or directory at parser2.pl line 22." How do you magically handle exception without writting any code for that ? :-)
2. where are you reading here : while(<>) ? There is no file handle ? Link to perldoc appreciated... 3. why did you put the main loop into a lexical scope ? { local @ARGV = $ARGV[0]; while (<>) { } } 4. why have you added the 'x' at / (\d{2}-\d{2}-\d{4}:\d{3}) \s+ \d\d (.+) /x 5. the $format from your code is : 'A1 A6 A3 A1 A5 A1 A5 A1 A5 A1 A5 A1 A5 A1 A5 A1 A5 A1 A5 A1 A5' What i want is : 'A1 A6 A3 x1 A5 x1 A5 x1 A5 x1 A5 x1 A5 x1 A5 x1 A5 x1 A5 x1 A5' Skip the delimeter! Could you make the change here please ? Thanks, José. -----Original Message----- From: Rob Dixon [mailto:[EMAIL PROTECTED] Sent: Friday, September 19, 2003 4:47 PM To: [EMAIL PROTECTED] Subject: Re: Split based on length Nyimi Jose wrote: > > IMHO > $string =~ /.{1,$len}/g; > Is the best suggestion i have seen so far ;) > > I have an other request : "code review" :-) > Below is my final code. > I'm sure that you guys will find > Some better style to write it ... > > #!/usr/local/bin/perl -w > use strict; > #------------------ > #Global variables > #------------------ > my $group_len=64; > my $index_len='A1'; > my $name_len='A6'; > my $way_len='A3'; > my $delim_len='x1'; > my $meas_len='A5'; > my $nof_meas=9; > #------ > #Main > #------ > my $file=$ARGV[0]; > open(FH,$file) || die "can not open $file : $! \n"; > > while(my $line=<FH>){ > next if $. < 3; > > my($date,$dummy,$str)=$line=~/(\d{2}-\d{2}-\d{4}:\d{3})\s+(\d{2})(.+)/; > for( &split_len($str,$group_len) ){ > print $date."\t", join( "\t",&get_fields($_) ),"\n"; > } > } > close(FH); > #------------ > #Subroutines > #------------ > sub split_len{ > my ($str, $len) = @_; > $str =~ /.{1,$len}/g; > } > > sub get_fields{ > my($str)[EMAIL PROTECTED]; > my $format="$index_len $name_len $way_len"; > $format.=" $delim_len $meas_len" x $nof_meas; > unpack($format,$str); > } Well you did ask :) How about this. Cheers, Rob use strict; use warnings; use constant GROUP_LEN => 64; use constant INDEX_LEN => 1; use constant NAME_LEN => 6; use constant WAY_LEN => 3; use constant DELIM_LEN => 1; use constant MEAS_LEN => 5; use constant NOF_MEAS => 9; my $format = join ' ', map "A$_", ( INDEX_LEN, NAME_LEN, WAY_LEN, (DELIM_LEN, MEAS_LEN) x NOF_MEAS ); { local @ARGV = $ARGV[0]; while (<>) { next if $. < 3; my ($date, $str) = / (\d{2}-\d{2}-\d{4}:\d{3}) \s+ \d\d (.+) /x; foreach ( split_len ($str, GROUP_LEN) ) { print join ("\t", $date, unpack $format, $str), "\n"; } } } sub split_len{ my ($str, $len) = @_; $str =~ /.{1,$len}/g; } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] **** DISCLAIMER **** "This e-mail and any attachment thereto may contain information which is confidential and/or protected by intellectual property rights and are intended for the sole use of the recipient(s) named above. Any use of the information contained herein (including, but not limited to, total or partial reproduction, communication or distribution in any form) by other persons than the designated recipient(s) is prohibited. If you have received this e-mail in error, please notify the sender either by telephone or by e-mail and delete the material from any computer". Thank you for your cooperation. For further information about Proximus mobile phone services please see our website at http://www.proximus.be or refer to any Proximus agent. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]