i have file in that i have one line is like {<pN_SLOT>,<pN_Port>}
,user will give the value N for example N=9 ;then these lines should print 9 times in the file with increment of N value .
Example: input file contains one line like this {<pN_SLOT>,<pN_Port>}
then in output file it should update like this {<p0_SLOT>,<p0_Port>}
,{<p1_SLOT>,<p1_Port>}
,{<p2_SLOT>,<p2_Port>}
,...upto {<p8_SLOT>,<p8_Port>}
.
if any perl module please suggest
any help/idea will be appreciable thank you
Something like this will do it:
#!/usr/bin/perl
use strict;
use warnings;
my $count = 9;
for my $num ( 0 .. $count ) {
print "{<p${num}_SLOT>,<p${num}_Port>},\n";
}
If you're taking an input file, and wanting to replace 'N' with 'number', then what you need* is a regular expression:
#!/usr/bin/perl
use strict;
use warnings;
my $line = '{<pN_SLOT>,<pN_Port>}';
my $count = 9;
for my $num ( 0 .. $count ) {
my $newline = ( $line =~ s/N/$num/gr );
print "$newline\n";
}
The perlre
page will give you some more regular expressions you can use to transform text.
As for reading in from files and numbers - this is actually the same problem. STDIN
is a 'standard input' filehandle.
You can read from it with <STDIN>
.
e.g.
print "Enter Number:\n";
my $response = <STDIN>;
chomp ( $response ); #necessary, because otherwise it includes a linefeed.
Or for reading from a file on disk:
open ( my $input_file, "<", "your_file_name" ) or die $!;
my $line = <$input_file>; #note - reads just ONE LINE here.
chomp ( $line );
close ( $input_file );
If you want to read more than one line in the second example, you may want to look at perlvar
and especially the $/
operator.
* need is strong - perl usually has many ways to do things. So in this case - "need" means "I suggest as a course of action".