This might not be exactly what you're after, but I wrote it how I'd approach things. and I added more comments than I normally would in order to make it clear what direction I was after and explain things. It appears to do what you want.

#!/usr/bin/perl use warnings; use strict; use FindBin qw($Bin); use Tie::File; # A list of IPs and subnet masks to substitute for '*' as we read thro +ugh a file of share info. If # you need this from a file, consider File::Slurp. my @ips = qw(192.169.32.0/255.255.255.0 192.169.72.0/255.255.255.0 192.169.73.0/255.255.255.0); # The path and name of the share file. my $sharefile = "$Bin/sharefile"; # Will hold the position in the @ips array as we progress through the +file looking for matches. my $last_index = '0'; # Holds the contents of the share file. my @contents; # This maps array elements to lines in the share file. tie(@contents, 'Tie::File', $sharefile) or die "Cannot tie $sharefile +: $!\n"; # Read each line in the file. If we see a '*' we replace it with an en +try in the @ips array. If a # replacement happens, we increment $last_index to point to the index +number of the next element in # the @ips array. If we matched the last element, we reset the counter + back to zero and start at # the beginning. for (my $i = 0; $i <= $#contents; $i++) { if ($contents[$i] =~ s/\*/$ips[$last_index]/) { if (++$last_index == @ips) { $last_index = '0'; } } } untie(@contents); print "Done editing $sharefile.\n";

That assumes you have the share info in a file called 'sharefile' in the same directory as the script. After running, the sharefile file looks like this:

/file1 192.169.32.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file1 192.169.72.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file1 192.169.73.0/255.255.255.0(rw,sync,no_root_squash,no_subtree_ch +eck) /file2 192.169.32.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) /file2 192.169.72.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) /file2 192.169.73.0/255.255.255.0(ro,sync,no_root_squash,no_subtree_ch +eck) # shares for Tsp InstallServer file3 192.169.32.0/255.255.255.0/255.255.255.0(ro,sync,no_subtree_chec +k) file3 192.169.72.0/255.255.255.0/255.255.255.0(ro,sync,no_subtree_chec +k) file3 192.169.73.0/255.255.255.0/255.255.255.0(ro,sync,no_subtree_chec +k)

I think that's what you were after anyway...


In reply to Re: Replace an asterisk '*" with the content of array by wee
in thread Replace an asterisk '*" with the content of array by deep27ak

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.