pejoya has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I have a script which parses line by line from a file. In order to make the changes "online" , I use the "Tie::File" module, so I have an array abstraction for the file, as each line in the file goes to each entry in the array.

The problem is, The regex patterns which worked before using Tie, fails to work afterwards. I've printed the array to see that each entry has gone chomp on it. 1. Does it affect the regex? (chomp operation?) 2. If not, why my regex doesn't work? code is displayed..

my $filename = "prefs_defaults.cfg"; tie my @array, 'Tie::File', $filename or die "Could not open file '$fi +lename'. $!"; # open (my $fh, "+<", $filename) or die "Could not open file '$filenam +e'. $!"; #while(my $current_line = <$fh>) for (my $i = 0; $i <= $#array; $i++){ my $current_line = $array[$i]; # this is the abstract array. #print "$array[$i]\n"; #print "$current_line\n"; foreach my $field (keys %user_options) { if (($current_line =~ $field) and ( defined ($user_options{$fi +eld}))) { #print "Line which modified: $current_line\n" ; #print "user options: $user_options{$field}\n"; print "hello"; $current_line = $current_line."\n"; print $current_line; if ($current_line =~ /(.*?)=(\w+)(\s+.*)/) { print "left: $1\n"; print "middle: $2\n"; print "right: $3\n"; } $current_line =~ s/(.*?)=(\w+)(\s+.*)/$1=$user_options{$fi +eld}$3/; #print "$current_line\n"; $array[$i] =$current_line; #print "$array[$i]\n"; } }
} Some lines from the file I am trying to read:.
[RECORD_OBJECT] START_GUARD_TIME=666 END_GUARD_TIME=0 AUTO_START_GUARD_TIME=2 # =PREFS_API_RECORD_AUTO_START_GUAR +D_TIME_ON AUTO_END_GUARD_TIME=2 # =PREFS_API_RECORD_AUTO_END_GUARD_ +TIME_ON DELETION_MODES=1 # =PREFS_API_RECORD_DELETION_MODES_ +AUTOMATIC REVIEW=1800 REMINDER_TIME=0 CONFLICT_TIME=0 WARN_PERCENT=90 [UI_OBJECT] TIME_OUT=5 TRIGGER_TIME_OUT=1 # =PREFS_API_UI_TRIGGER_TIME_OUT_OF +F MESSAGE_ALERT=1 # =PREFS_API_UI_MESSAGE_ALERT_OFF BACK_MUSIC=1 # =PREFS_API_UI_BACK_MUSIC_OFF BEEP=1 # =PREFS_API_UI_BEEP_OFF SUBTITLE=1 # =PREFS_API_UI_SUBTITLE_OFF NARRATIVE=1 # =PREFS_API_UI_NARRATIVE_OFF HIGHLIGHTED_PROG=1 # =PREFS_API_UI_HIGHLIGHTED_PROG_OF +F BEEP_IF_NARRATIVE=1 # =PREFS_API_UI_BEEP_IF_NARRATIVE_O +FF REMOVE_ADULT_CHAN=1 # =PREFS_API_UI_REMOVE_ADULT_CHAN_O +FF FRONT_PANEL=2 # =PREFS_API_UI_FRONT_PANEL_STANDAR +D KEY_REPEAT=1 # =PREFS_API_UI_KEY_REPEAT_OFF KEY_REPEAT_INITIAL_DELAY=0 KEY_REPEAT_DELAY=0 MINI_TV=2 # =PREFS_API_UI_MINI_TV_ON [ANYTIME_OBJECT] OPT_IN=1 BROADBAND=1 QUEUE=2 NOTIFY=2 CONTENT_GROUP=0x00000002 # bitmask: 0x1 - SD, 0x2 - HD

Replies are listed 'Best First'.
Re: Reuglar Expression with chomp and Tie
by svenXY (Deacon) on Feb 04, 2009 at 14:39 UTC
    Hi,
    can you also please give us some lines of the file you are trying to work on?
    Regards,
    svenXY
      added.