As others have mentioned, you should profile the program to get an idea of what code is taking up time. That said, here are a couple of comments:
When you have that many arguments, it is usually preferrable (may be a little faster) to use the following (rather than shifts): my ($self, $data, $start, $length) = @_; That said, if you can, get rid of arguments you aren't using. Also, you are setting a variable named $startPos, and never using it. Also, for your regular expression, utilize the qr operator: my $regexp = qr#\|#; sub extractFieldValue { ... ... $val =~ s/$regexp/ /g; ... } HTH, Ken On Wed, Apr 10, 2013 at 8:02 AM, kavita kulkarni <kavitahkulka...@gmail.com>wrote: > Data file has the lines with same length and so the field positions I am > interested in (so unpack works for me). > Tried with "unpack" as well & it takes almost same time as substr(). > > Here is sample code: > > Below function is called for each line of input data file.. > > sub extractFieldValue { > my $self = shift; > my $data = shift; #Line from data file > my $start = shift; > my $length = shift; > my $startPos = $start - 1; > my $val = unpack("a$length", $data); > $val =~ s/\|/ /g; > return $val; > } > > $val is then used as key of a Hash for further processing. > > Cheers, > Kavita > > Regards, > Kavita :-) > > > On Wed, Apr 10, 2013 at 4:32 PM, Jenda Krynicky <je...@krynicky.cz> wrote: > > > From: timothy adigun <2teezp...@gmail.com> > > > On 10 Apr 2013 11:30, "Chankey Pathak" <chankey...@gmail.com> wrote: > > > > > > > > Hi Kavita, > > > > > > > > You may try unpack (http://perldoc.perl.org/functions/unpack.html) > > > > > > > unpack would not work if the OP has varying length of lines. > > > > Nope. It would work just fine as long as the bits he's interested in > > are fixed lengh and are on fixed positions. The length of the > > uninteresting trailing stuff is irrelevant. > > > > Jenda > > ===== je...@krynicky.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 > > > > > > -- > > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > > For additional commands, e-mail: beginners-h...@perl.org > > http://learn.perl.org/ > > > > > > >