in reply to Re: Nested While loop not working
in thread Nested While loop not working

Hi Vinoth and Ken, Actually my requirement is there are two files

F1,F2 F1 has data NAMES ** name1 name2 ADDRESS street1 street2 PHONE ** num1 is num2 is F2 has data name1 sirname1 street1 housenumber 1 num1 is 8784794 num2 is 9888948 street2 housenumber23 name2 sirname2

so i read each line in F1 and see if its mandatory(ends with (**) then under that heading i take the values and its corressponding complete value is taken from F2 so finally what i should get is

NAMES name1 sirname1 NAMES name2 sirname2 PHONE num1 is 8784794 PHONE num2 is 9888948
Thanks , my code is
#! usr/bin/perl use strict; use warnings; my $line; my $str; use diagnostics; open my $file_one,'<','F1' or die "unable to open"; while(defined($line=<$file_one>)) { chomp($line); if ($line =~m/(\*\*)/) { $str = $line ; # print " $str \n"; } else { open my $file2,'<','F2' or die "unable to open "; while(defined(my $line2=<$file2>)) { if($line2=~m/$line/) { print " line : $str line2: $line2\n"; } } close($file2); } }

its giving error

Invalid [] range "=-," in regex; marked by <-- HERE in m/ 144 x Lc +b (P CAL_LCBMS [CAL_LCBMS] T lcb I [ACT=-, <-- HERE NCLK=chpl::ec0+clk, FORCE_T=-, SG=chpl::ec0+sg+region1+no_l +cc+plat_flush_nto1, THOLD_B=chpl::ec0+sl_thold+ab st+region1+no_lcc, MPW1_B=-, MPW2_B=-, DLY_LCKR=-, DLY0=chpl::ec0+time ++region1+no_lcc+slat, DLY1=chpl::ec0+time+region1 +no_lcc+slat, PW0=chpl::ec0+time+region1+no_lcc+slat, PW1=chpl::ec0+ti +me+region1+no_lcc+slat, PW2=chpl::ec0+time+region 1+no_lcc+slat] E [SG=OR(chpl::ec0+lbist_ary at ./stest line 21, <$file +2> line 1. at ./stest line 21
where line 21 is the pattern matching statement if($line2=~m/$line/)

Replies are listed 'Best First'.
Re^3: Nested While loop not working
by kcott (Archbishop) on Mar 12, 2014 at 06:49 UTC

    Firstly, please put your error output in <code>...</code> tags. Does what you've posted there look the same as the error message you received?

    When posting, before hitting the [ create ] button, keep editing then hitting the [ preview ] button until your post accurately reflects the information you're attempting to communicate. When updating a post, there's no [ preview ] button; you need to keep editing then hitting the [ update ] button.

    The input data you described looks nothing like what you're reading. Did you open the correct file?

    The data you've read into $line starts off something like (my best guess at reconstructing it):

    144 x Lcb (P CAL_LCBMS [CAL_LCBMS] T lcb I [ACT=-, NCLK=chpl::ec0+clk +, ...

    That (along with all the text that follows it) contains many characters that have special meanings within a regex. A character class is specified within square brackets (i.e. [...]). The "[ACT=-," part of $line starts a character class which includes all the charcters in the range "=-,"; however, ord('=') == 61 and ord(',') == 44, so the start of that range is after its end and, therefore, invalid.

    That explains the error which would have looked something like:

    Invalid [] range "=-," in regex; marked by <-- HERE in m/ 144 x Lcb (P CAL_LCBMS [CAL_LCBMS] T lcb I [ACT=-, <-- HERE ...

    While I suspect this is a mistake on your part (e.g. you're reading the wrong file), if you want to interpolate data like that into a regex, you'll need to do something like:

    /\Q$line/

    See quotemeta for details.

    -- Ken

      Thank you so much Ken :) it did help

      Thank you so much Ken :) it did help and Sorry for the wrong format of text