Saturday, February 5, 2011

PERL : File I/O Split

pico split1.pl
#!/usr/bin/perl -w
#split, join and flexible input files from shell
$data4 = "Mark Sloan";
@array1 = split /\s+/, $data4;#space in perl means \n \t \s+
foreach(@array1)
{
print $_\n;

#End


#!/usr/bin/perl -w
$data4 = "Mark,Sloan";
@array1 = split /,/, $data4;
foreach(@array1)
{
print $_\n;



#End
#!/usr/bin/perl -w
$lc = 0;
while(<>)#expects as a parameter a file
{
print $_;
@array2 = split /:/,$_;
#print "array2[0..2]\n";
foreach (@array2)
{
print "$_\n";
}
$lc += 1;
}
print "Line count : ".$lc;
#END
./split1.pl data4# returns 4 because data4 has 4 lines


#!/usr/bin/perl -w
my @array = ("Mark","Sloan","Vincent");
$newarray1 = join ":",@array1;#join interpozes delimiter between array elements
print "New delimiter is : ", $newarray1\n;
while (<>)
{

}
#end


#!/usr/bin/perl -w
while (<>)
{
@array1 = split /:/,$_;
$newarray1 = join ",",@array1;
print "New records are delimited by \",\"","$newarray1";
}
#end
perl -w split1.pl data4


#!/usr/bin/perl -w
while (<>)
{
@array1 = split /:/,$_;
$newarray1 = join ",",@array1;
print "New records are delimited by \",\"","$newarray1";
}
#end


#!/usr/bin/perl -w
open (HAN1, ">data4a") || die "Problems:$!";
while (<>)
{
@array1 = split /:/,$_;
$newarray1 = join ",",@array1;
print "New records are delimited by \",\"","$newarray1";
print HAN1 $newarray
}
#end


nano totals1.pl
#!/usr/bin/perl -w
use warnings;
use strict;
my $data1 = "dataproducts1";
open (han1, "$data") || die "Errors opening $data1 : $!";
@f1 = <han1>;
my $total = 0;
foreach(@f1)
{
#print $_;
@columns = split;#by default split operates on $_ 
print "$columns[4]\n";
$total = $columns[3] + $total;
}
#end
chmod u+x total1.pl
./total1.pl

No comments:

Post a Comment