Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Perl code to format the text file by inserting tag in blank line

by AnomalousMonk (Bishop)
on Feb 12, 2020 at 06:26 UTC ( #11112839=note: print w/replies, xml ) Need Help??

in reply to Perl code to format the text file by inserting tag in blank line

$dimens=scalar(keys(%ind)); for ($jj=1; $jj <= $dimens; $jj++) { # Writing output mt reserve meas list print OUTFILE "<mt>$kpi[$jj]<\/mt>\n"; } ... ... for ($jk=1; $jk <= $dimens; $jk++) { # Writing output r meas list result sorted in the same ord +er than mt list print OUTFILE "$result{$value}{$ext}{$kpi[$jk]}\n"; } ... ... ...

Just something that caught my eye... I don't know how you are initializing the  @kpi array, but with this code, it needs one more element than the number of elements in the  %ind hash (Perl arrays are 0-based by default).

Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Perl code to format the text file by inserting tag in blank line
by Marshall (Abbot) on Feb 13, 2020 at 02:01 UTC
    I think the OP is probably ok here.
    He is using arrays in a "non-Perl" way and with explicit indices starting at 1 instead of 0. Not using array[0] is allowed. $kpi[0] is probably undef.

    "Off by one" is one of the most common errors in programming. I personally would rewrite the code so that it doesn't use indices at all like: foreach my $element (@array){} However a re-write like appears to be outside the scope of the question before us. Perl code like that would use index 0. However there would not be any direct reference to that zero index within the code at all. Creating the kpi array would use "push" instead of explicit indices from 1..n. If the OP creates the kpi array with starting with $kpi[1] that just makes index 0 undef (skips it). I don't see any indication that the OP is aware of: push,pop,shift,unshift.

      I think the OP is probably ok here.

      I'm not so sure. DAN0207 is a self-described Perl "beginner", and looking at other posts by this monk doesn't give me the sense that he or she is necessarily aware of the issue of 0- versus 1-based arrays. We also cannot see anything about how the  @kpi array is initialized. The whole thing made my buggy-sense tingle.

      However, DAN0207 hasn't seen the need to follow up on the point I made here (indeed, replying with a question on a completely different topic), so I guess everything's ok...

      Give a man a fish:  <%-{-{-{-<

        Well to completely digest the collective advice given so far would take days, not hours for a beginner. At this point, I figure the OP has learned some bits of what was presented, but not everything presented. If the OP has no further questions at this time, I'm ok with that.
Re^2: Perl code to format the text file by inserting tag in blank line
by DAN0207 (Novice) on Feb 12, 2020 at 08:05 UTC

    I tried a perl one liner sepertaely on the output xml file as below:

    perl -i -lpe "s/^\s*$/<r><\/r>/g" test(filename i used to check separa +tely)
    But when i write this one liner in the main code as below, its not giving any output..(No xml output file is getting generated)
    print OUTFILE "$result{$moid}{$ext}{$kpi[$jk]}\n"; perl -i -lpe "s/^\s*$/<r><\/r>/g" $OUTFILE
    Could you pl. help on this.
      print OUTFILE "$result{$moid}{$ext}{$kpi[$jk]}\n"; perl -i -lpe "s/^\s*$/<r><\/r>/g" $OUTFILE

      Command-line syntax cannot be included directly in Perl code. I'm surprised this would even compile!

      Be that as it may, you seem to be trying to check if the string  "$result{$moid}{$ext}{$kpi[$jk]}\n" is a blank line and output  "<r></r>\n" if it is. Try (untested):

      my $out = "$result{$moid}{$ext}{$kpi[$jk]}\n"; $out =~ s{ \A \s* \z }{<r></r>\n}xms; print OUTFILE $out;

      Give a man a fish:  <%-{-{-{-<

        Thanks a ton!! it worked.I am a perl beginner and iam still trying hard to code better in PERL. Would you mind explaining the 3 lines of code that you have replied me with?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11112839]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-05-31 13:51 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (173 votes). Check out past polls.