Colin Johnstone wrote:
> 
> Gidday all,

Hello,

> I want to create an array and then remove elemnets one at a time and split on the 
>comma.
> 
> I've tried this with no success..
> 
> #!/usr/bin/perl -w
> 
> print "Content-Type: text/html\n\n";
> 
> my @arrayStuff;
> 
> $arrayStuff[0] = "albu.htm,3.4.1.5.1,albu_menu.inc,albu.inc";
> $arrayStuff[1] = "armi.php,3.4.1.5.2,armi_menu.inc,armi.inc";
> $arrayStuff[2] = "bank.htm,3.4.1.5.3,bank_menu.inc,bank.inc";
> $arrayStuff[3] = "bate.htm,3.4.1.5.4,bate_menu.inc,bate.inc";

You can declare and define the array at the same time:

my @arrayStuff = (
    'albu.htm,3.4.1.5.1,albu_menu.inc,albu.inc',
    'armi.php,3.4.1.5.2,armi_menu.inc,armi.inc',
    'bank.htm,3.4.1.5.3,bank_menu.inc,bank.inc',
    'bate.htm,3.4.1.5.4,bate_menu.inc,bate.inc',
    );

But since you are just going to split it up anyways, you should use an
array of arrays:

my @arrayStuff = (
    [ 'albu.htm', '3.4.1.5.1', 'albu_menu.inc', 'albu.inc' ],
    [ 'armi.php', '3.4.1.5.2', 'armi_menu.inc', 'armi.inc' ],
    [ 'bank.htm', '3.4.1.5.3', 'bank_menu.inc', 'bank.inc' ],
    [ 'bate.htm', '3.4.1.5.4', 'bate_menu.inc', 'bate.inc' ],
    );

Or you could even use an array of hashes:

my @arrayStuff = (
    { OutFile => 'albu.htm',
      PageId  => '3.4.1.5.1',
      Menu    => 'albu_menu.inc',
      Content => 'albu.inc'
    },
    { OutFile => 'armi.php',
      PageId  => '3.4.1.5.2',
      Menu    => 'armi_menu.inc',
      Content => 'armi.inc'
    },
    { OutFile => 'bank.htm',
      PageId  => '3.4.1.5.3',
      Menu    => 'bank_menu.inc',
      Content => 'bank.inc'
    },
    { OutFile => 'bate.htm',
      PageId  => '3.4.1.5.4',
      Menu    => 'bate_menu.inc',
      Content => 'bate.inc'
    } );


> my $fileName = "/web/schooled/www/skin.php";
> my $outFilePath = "/web/schooled/www/hscdata2/";
> 
> if( ! -e $fileName){
>  print "Nothing to Work with!<br>";
> }
> else{
> 
>   while( @arrayStuff ){
>     $element = shift( @arrayStuff )
>     @data = split( /\t/, $element);
> 
>     my $outFileName = $data[0];
>     my $strPageId = $data[1];
>     my $pageMenu = $data[2];
>     my $pageContent = $data[3];
> 
>     my $output = "";
>     open(IN, "<$fileName") or die("Cannot open: $!");

You are opening and reading $fileName once for each entry in
@arrayStuff.  It would be more effecient to open and read the contents
just once.


>     while( my $line = <IN>){
>       $line =~ s/<!-- pageId here -->/$strPageId/;
>       $line =~ s/<!-- menu here -->/<? include "$pageMenu.inc"; ?>/;
>       $line =~ s/<!-- content here -->/$pageContent/;
>       $output .= $line;
>     }
>     close(IN);
> 
>     open(OUT, ">$outFilePath$outFileName");
>     print OUT ($output);
>     close(OUT);
> 
>     print "Generating output file $outFileName<br/>";
> }
> 
> The output I get is:-
> 
> Generating output file
> Generating output file
> Generating output file
> Generating output file



#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;

print header;

my @arrayStuff = (
    { OutFile => 'albu.htm',
      PageId  => '3.4.1.5.1',
      Menu    => 'albu_menu.inc',
      Content => 'albu.inc'
    },
    { OutFile => 'armi.php',
      PageId  => '3.4.1.5.2',
      Menu    => 'armi_menu.inc',
      Content => 'armi.inc'
    },
    { OutFile => 'bank.htm',
      PageId  => '3.4.1.5.3',
      Menu    => 'bank_menu.inc',
      Content => 'bank.inc'
    },
    { OutFile => 'bate.htm',
      PageId  => '3.4.1.5.4',
      Menu    => 'bate_menu.inc',
      Content => 'bate.inc'
    } );

my $fileName    = '/web/schooled/www/skin.php';
my $outFilePath = '/web/schooled/www/hscdata2';

for my $stuff ( @arrayStuff ) {
    open my $fh, '>', "$outFilePath/$outFileName"
        or die "Cannot open $outFilePath/$outFileName: $!";
    $stuff->{Handle} = $fh;
    }

open my $fh, $fileName or die "Cannot open $fileName: $!";
while ( my $line = <$fh> ) {
    for my $stuff ( @arrayStuff ) {
        if ( $line =~ /^(.*?)<!-- pageId here -->(.*)\z/s ) {
            print {$stuff->{Handle}} $1, $stuff->{PageId}, $2;
            }
        elsif ( $line =~ /^(.*?)<!-- menu here -->(.*)\z/s ) {
            print {$stuff->{Handle}} qq[$1<? include "$stuff->{Menu}";
?>$2];
            }
        elsif ( $line =~ /^(.*?)<!-- content here -->(.*)\z/s ) {
            print {$stuff->{Handle}} $1, $stuff->{Content}, $2;
            }
        else {
            print {$stuff->{Handle}} $line;
            }
        }
    }
close $fh;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to