Well I'm at a loss why my code below does not work as expected... SO much so I don't even know what to search for.

My appologizes if a similar question has been asked before in the past.

I am trying to work around an issue I have with split (is their a more elegant solution?) in which my input data has null fields. Split unfortently doesn't set my vars to null.

My data also is not consistant in the number of fields (I am processing passwd files AND shadow files).

So to "fix" that I figure I'll just change the input and move on. The only prob is that it doesn't work as I expected.

Can anyone explain what is happening?

Thanks for your time!

#!/usr/local/bin/perl use strict; use warnings; # # I want to shove the string NULL into place to make # my split work as expected. # # Ie if I split $line I only get 3 fields when I want 9. # I want an empty/null field to be set to null. # my $line="root:XXXXXXXXXXXXX:12103::::::"; # I want the above to become This: print "My Goal: root:XXXXXXXXXXXXX:12103:NULL:NULL:NULL:NULL:NULL:N +ULL\n\n"; print "\nWhy does this not work?\n"; print "\tBEFORE\t$line\n" if ( $line =~ /^root:/ ); # UGLY UGLY UGLY UGLY. you have a better idea? $line=~s/::/:NULL:/g; $line=~s/:$/:NULL/g; print "\tAFTER\t$line\n" if ( $line =~ /^root:/ ); $line="root:XXXXXXXXXXXXX:12103::::::"; print "\nThis is an ugly ugly ugly solution:\n"; print "\tBEFORE\t$line\n" if ( $line =~ /^root:/ ); # UGLY UGLY UGLY UGLY. you have a better idea? $line=~s/::/:NULL:/g; $line=~s/::/:NULL:/g; # argh! $line=~s/:$/:NULL/g; print "\tAFTER\t$line\n" if ( $line =~ /^root:/ );

The output:

Why does this not work?

BEFORE root:XXXXXXXXXXXXX:12103::::::

AFTER root:XXXXXXXXXXXXX:12103:NULL::NULL::NULL:NULL

This is an ugly ugly ugly solution:

BEFORE root:XXXXXXXXXXXXX:12103::::::

AFTER root:XXXXXXXXXXXXX:12103:NULL:NULL:NULL:NULL:NULL:NULL


In reply to Perl subsititution oddities... by smellysocks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.