Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How would I do this in perl?

Reply
Thread Tools

How would I do this in perl?

 
 
laredotornado
Guest
Posts: n/a
 
      10-14-2009
Hi,

I'm not so familiar with perl but it seems this is the kind of task it
is suited for. I have a file of numbers, one number per line. Then I
have a template file that contains ...

public void testXMatchValid_UCASE() throws java.lang.Exception {
_testMatchUpperCase(X);
}

public void testXMatchValid_lcase() throws java.lang.Exception {
_testMatchLowerCase(X);
}

I want the final file to have each number replace the "X" in the
template file and the template would repeat for the number of lines in
the numbers file. So if the numbers file contained

10
20

the resulting output file would contain

public void test10MatchValid_UCASE() throws java.lang.Exception {
_testMatchUpperCase(10);
}

public void test10MatchValid_lcase() throws java.lang.Exception {
_testMatchLowerCase(10);
}

public void test20MatchValid_UCASE() throws java.lang.Exception {
_testMatchUpperCase(20);
}

public void test20MatchValid_lcase() throws java.lang.Exception {
_testMatchLowerCase(20);
}


HOw would I pull this off using perl? Thanks, - Dave
 
Reply With Quote
 
 
 
 
laredotornado
Guest
Posts: n/a
 
      10-14-2009
On Oct 14, 3:16*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth laredotornado <laredotorn...@zipmail.com>:
>
>
>
>
>
> > I'm not so familiar with perl but it seems this is the kind of task it
> > is suited for. *I have a file of numbers, one number per line. *Then I
> > have a template file that contains ...

>
> > * *public void testXMatchValid_UCASE() throws java.lang.Exception {
> > * * * * * *_testMatchUpperCase(X);
> > * *}

>
> > * *public void testXMatchValid_lcase() throws java.lang.Exception {
> > * * * * * *_testMatchLowerCase(X);
> > * *}

>
> > I want the final file to have each number replace the "X" in the
> > template file and the template would repeat for the number of lines in
> > the numbers file. *So if the numbers file contained

>
> (untested)
>
> * * use File::Slurp qw/slurp/;
>
> * * my $template = slurp "template";
> * * my @n = slurp "numbers";
>
> * * for (@n) {
> * * * * (my $out = $template) = s/X/$_/g;
> * * * * print $out;
> * * }
>
> Ben


Thanks for this response. Unfortunately, I get

Can't locate File/Slurp.pm in @INC (@INC contains: /opt/local/lib/
perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
lib/perl5/vendor_perl .) at test.pl line 1

Is there a quick way to download and install the slurp module? I'm on
a Mac OS X, using Perl 5.8.8. Thanks, - Dave
 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-14-2009
On Wed, 14 Oct 2009 13:31:23 -0700 (PDT), laredotornado <> wrote:

>Hi,
>
>I'm not so familiar with perl but it seems this is the kind of task it
>is suited for. I have a file of numbers, one number per line. Then I
>have a template file that contains ...
>
> public void testXMatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(X);
> }
>
> public void testXMatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(X);
> }
>
>I want the final file to have each number replace the "X" in the
>template file and the template would repeat for the number of lines in
>the numbers file. So if the numbers file contained
>
>10
>20
>
>the resulting output file would contain
>
> public void test10MatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(10);
> }
>
> public void test10MatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(10);
> }
>
> public void test20MatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(20);
> }
>
> public void test20MatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(20);
> }
>
>
>HOw would I pull this off using perl? Thanks, - Dave


Its fairly easy, but if your iffy on Perl, this won't help much.
-sln
----------
use strict;
use warnings;

# Dummy template file ..
my $tfile = "
public void testXMatchValid_UCASE() throws java.lang.Exception {
_testMatchUpperCase(X);
}
public void testXMatchValid_lcase() throws java.lang.Exception {
_testMatchLowerCase(X);
}
";
# Dummy number file ..
my $nfile = "
10
20
";

# The program ..

open my $tfh, '<', \$tfile or die "can't open template file: $!";
open my $nfh, '<', \$nfile or die "can't open number file: $!";
#open my $outfh, '>', "output.txt" or die "can't open output file: $!";

while (<$nfh>)
{
my ($number) = /(\d+)/;
next if !defined($number);

seek $tfh,0,0;
while (my $line = <$tfh>)
{
$line =~ s/X/$number/;
#print $outfh $line;
print $line;
}
}

close $tfh;
close $nfh;
#close $outfh;

 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      10-14-2009
In article
<9daad6a5-c771-49c7-89bf->,
laredotornado <> wrote:

> On Oct 14, 3:16*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth laredotornado <laredotorn...@zipmail.com>:
> >


> > * * use File::Slurp qw/slurp/;
> >
> > * * my $template = slurp "template";
> > * * my @n = slurp "numbers";
> >
> > * * for (@n) {
> > * * * * (my $out = $template) = s/X/$_/g;
> > * * * * print $out;
> > * * }
> >
> > Ben

>
> Thanks for this response. Unfortunately, I get
>
> Can't locate File/Slurp.pm in @INC (@INC contains: /opt/local/lib/
> perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
> perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
> 5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
> 5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
> lib/perl5/vendor_perl .) at test.pl line 1
>
> Is there a quick way to download and install the slurp module? I'm on
> a Mac OS X, using Perl 5.8.8. Thanks, - Dave


Try the following in the Terminal:

sudo cpan
install File::Slurp

You may have to answer some questions if this is the first time you
have run cpan.

You also may want to add after:

my @n = slurp "numbers";

the following:

chomp(@n);

to remove line ending characters from your numbers.

You might also want to read the advice in

perldoc -q entire "How can I read an entire file all at once?"

--
Jim Gibson
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-14-2009
On Wed, 14 Oct 2009 14:37:38 -0700 (PDT), laredotornado <> wrote:

>On Oct 14, 3:16*pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth laredotornado <laredotorn...@zipmail.com>:
>>
>>
>>
>>
>>
>> > I'm not so familiar with perl but it seems this is the kind of task it
>> > is suited for. *I have a file of numbers, one number per line. *Then I
>> > have a template file that contains ...

>>
>> > * *public void testXMatchValid_UCASE() throws java.lang.Exception {
>> > * * * * * *_testMatchUpperCase(X);
>> > * *}

>>
>> > * *public void testXMatchValid_lcase() throws java.lang.Exception {
>> > * * * * * *_testMatchLowerCase(X);
>> > * *}

>>
>> > I want the final file to have each number replace the "X" in the
>> > template file and the template would repeat for the number of lines in
>> > the numbers file. *So if the numbers file contained

>>
>> (untested)
>>
>> * * use File::Slurp qw/slurp/;
>>
>> * * my $template = slurp "template";
>> * * my @n = slurp "numbers";
>>
>> * * for (@n) {
>> * * * * (my $out = $template) = s/X/$_/g;
>> * * * * print $out;
>> * * }
>>
>> Ben

>
>Thanks for this response. Unfortunately, I get
>
>Can't locate File/Slurp.pm in @INC (@INC contains: /opt/local/lib/
>perl5/5.8.8/darwin-2level /opt/local/lib/perl5/5.8.8 /opt/local/lib/
>perl5/site_perl/5.8.8/darwin-2level /opt/local/lib/perl5/site_perl/
>5.8.8 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl/
>5.8.8/darwin-2level /opt/local/lib/perl5/vendor_perl/5.8.8 /opt/local/
>lib/perl5/vendor_perl .) at test.pl line 1
>
>Is there a quick way to download and install the slurp module? I'm on
>a Mac OS X, using Perl 5.8.8. Thanks, - Dave


But then again before you install the world, slurp is not
even an issue (nor needed) for your problem description.

-sln
 
Reply With Quote
 
sharma__r@hotmail.com
Guest
Posts: n/a
 
      10-15-2009
On Oct 15, 1:31*am, laredotornado <laredotorn...@zipmail.com> wrote:
> Hi,
>
> I'm not so familiar with perl but it seems this is the kind of task it
> is suited for. *I have a file of numbers, one number per line. *Then I
> have a template file that contains ...
>
> * * * * public void testXMatchValid_UCASE() throws java.lang.Exception {
> * * * * * * * * _testMatchUpperCase(X);
> * * * * }
>
> * * * * public void testXMatchValid_lcase() throws java.lang.Exception {
> * * * * * * * * _testMatchLowerCase(X);
> * * * * }
>
> I want the final file to have each number replace the "X" in the
> template file and the template would repeat for the number of lines in
> the numbers file. *So if the numbers file contained
>
> 10
> 20
>
> the resulting output file would contain
>
> * * * * public void test10MatchValid_UCASE() throws java.lang.Exception {
> * * * * * * * * _testMatchUpperCase(10);
> * * * * }
>
> * * * * public void test10MatchValid_lcase() throws java.lang.Exception {
> * * * * * * * * _testMatchLowerCase(10);
> * * * * }
>
> * * * * public void test20MatchValid_UCASE() throws java.lang.Exception {
> * * * * * * * * _testMatchUpperCase(20);
> * * * * }
>
> * * * * public void test20MatchValid_lcase() throws java.lang.Exception {
> * * * * * * * * _testMatchLowerCase(20);
> * * * * }
>
> HOw would I pull this off using perl? *Thanks, - Dave


(untested)

perl -wlne '
do{ $temp .= qq{\n} .$_; next; } if $ARGV eq q{template};

($temp_copy = $temp) =~ s/X/$_/;

print $temp_copy;
' template_file nums_file


Note: The ordering of the file arguments to perl is essential.


--Rakesh
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-15-2009
laredotornado wrote:

> Is there a quick way to download and install the slurp module? I'm on
> a Mac OS X, using Perl 5.8.8.


I use ports: http://darwinports.com/

--
Ruud
 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      10-15-2009
On Oct 14, 4:31*pm, laredotornado <laredotorn...@zipmail.com> wrote:
> I have a file of numbers, one number per line. *Then I
> have a template file that contains ...
>
> * * * * public void testXMatchValid_UCASE() throws java.lang.Exception {
> * * * * * * * * _testMatchUpperCase(X);
> * * * * }
>
> * * * * public void testXMatchValid_lcase() throws java.lang.Exception {
> * * * * * * * * _testMatchLowerCase(X);
> * * * * }
>
> I want the final file to have each number replace the "X" in the
> template file and the template would repeat for the number of lines in
> the numbers file. *So if the numbers file contained
>
> 10
> 20


This is much more verbose than the other suggestions, but maybe easier
to understand. I have tested the code, and it produces the correct
output.

use strict;
use warnings;
#open number file for reading
open NUMBERS, '<', 'numbers.dat';
#open output file for appending
open OUTFILE, '>>', 'output.java';
#iterate through number file
while (<NUMBERS>)
{
#check for digits
next unless /\d/;
#remove the newline
chomp;
#save each number for later use
my $number = $_;
#open template file for reading
open TEMPLATE, '<', 'template.java';
#iterate through template file
while(<TEMPLATE>)
{
#replace X with $number
$_ =~ s/X/$number/;
# print to outfile
print OUTFILE $_;
}
close TEMPLATE;
}
close NUMBERS;
close OUTFILE;
exit(0);
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-15-2009
On Wed, 14 Oct 2009 13:31:23 -0700 (PDT), laredotornado <> wrote:

>Hi,
>
>I'm not so familiar with perl but it seems this is the kind of task it
>is suited for. I have a file of numbers, one number per line. Then I
>have a template file that contains ...
>
> public void testXMatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(X);
> }
>
> public void testXMatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(X);
> }
>
>I want the final file to have each number replace the "X" in the
>template file and the template would repeat for the number of lines in
>the numbers file. So if the numbers file contained
>
>10
>20
>
>the resulting output file would contain
>
> public void test10MatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(10);
> }
>
> public void test10MatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(10);
> }
>
> public void test20MatchValid_UCASE() throws java.lang.Exception {
> _testMatchUpperCase(20);
> }
>
> public void test20MatchValid_lcase() throws java.lang.Exception {
> _testMatchLowerCase(20);
> }
>
>
>HOw would I pull this off using perl? Thanks, - Dave


Just out of curiosity, a couple of questions.
Why would you define totally static wrapper function's, distinct
in name for each number, when they all call the same function?
And how did you plan on calling these?

test10MatchValid_UCASE();
test20MatchValid_UCASE();
test30MatchValid_UCASE();
....

Why not just call a single function from a loop with
variable data?

int num[] = {10,20, ...}; // or num array can be generated
// or generated in the for(,,)
for (int i = 0; i < (sizeof(num)/int); i++) {
testMatchValid_UCASE(num[i]); // what are we testing, no exceptions?
testMatchValid_lcase(num[i]);
}

I think you might be just curious about Perl's ability to do things
well, like mail-merge (macro's ?).

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      10-15-2009
On Thu, 15 Oct 2009 09:19:14 -0700, wrote:

>On Wed, 14 Oct 2009 13:31:23 -0700 (PDT), laredotornado <> wrote:
>
>Why not just call a single function from a loop with
>variable data?
>
> int num[] = {10,20, ...}; // or num array can be generated
> // or generated in the for(,,)
> for (int i = 0; i < (sizeof(num)/int); i++) {

^^^^^^^^^^^
sizeof(num)/sizeof(int)

Its been a while.
-sln

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
an oddball scary kind of thing you would think would never happen richard Computer Support 4 01-31-2010 06:34 PM
Would this be cool or not? Genaio Case Modding 4 07-27-2005 10:18 PM
any help would be appreciated! Kreepz86 Wireless Networking 4 07-01-2005 04:55 AM
would a TV cause interference? djc Wireless Networking 1 09-19-2004 01:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57