Read and change the file
--0-1992804172-1300353821=:73533
Content-Type: text/plain; charset=us-ascii
Hi Guys!
I am newbie in Perl.
I have problem in reading a file and change it to get some specific output. For
example : I want to read a file : filename.txt which contain 2 lines:
Data A 1 2 3 4 5
Data B 6 7 8 9 10
the name of the data and the values are tab separated. my wish is to change it
into :Data A = (1,2,3,4,5); Data B = (6,7,8,9,10);
Thanks in advanced for your kindly help!
Regards,
-student-
--0-1992804172-1300353821=:73533--
Re: Read and change the file
ä=BA=8E 2011-3-17 17:23, wisma laili =E5=86=99=E9=81=93:
> I have problem in reading a file and change it to get some specific ou=
tput. For
> example : I want to read a file : filename.txt which contain 2 lines:
> Data A 1 2 3 4 5
> Data B 6 7 8 9 10
> the name of the data and the values are tab separated. my wish is to c=
hange it
> into :Data A =3D (1,2,3,4,5); Data B =3D (6,7,8,9,10);
>
> Thanks in advanced for your kindly help!
So, split each line and get a list, then create a hash with the first
two elements in the list as key and the left elements as value?
use strict;
my %hash;
while(<DATA>) {
chomp;
my [at] list =3D split;
my $name =3D join " ",(shift [at] list,shift [at] list);
$hash{$name} =3D [ [at] list];
}
__DATA__
Data A 1 2 3 4 5
Data B 6 7 8 9 10
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
RE: Read and change the file
Hi Wisma
You can try following set of code :
------------------
#!/usr/local/bin/perl -w
use warnings;
open (DATA , "data.txt") or die "can not open data.txt, $!";
open (DATA1, ">data1.txt") or die "can not open data1.txt, $!";
while(<DATA>){
$string =3D $_;
($data1,$data2) =3D $string =3D~ /(Data \w)\s+(.*)/;
$data2 =3D join (",",split(/\s+/,$data2));
$string =3D "$data1 =3D ($data2);\n";
print DATA1 $string;
}
close DATA;
close DATA1;
--------------------
Thanks
Sunita
-----Original Message-----
From: wisma laili [mailto:wsmlaili [at] yahoo.com]
Sent: Thursday, March 17, 2011 2:54 PM
To: beginners [at] perl.org
Subject: Read and change the file
Hi Guys!
I am newbie in Perl.
I have problem in reading a file and change it to get some specific
output. For
example : I want to read a file : filename.txt which contain 2 lines:
Data A 1 2 3 4 5
Data B 6 7 8 9 10
the name of the data and the values are tab separated. my wish is to
change it
into :Data A =3D (1,2,3,4,5); Data B =3D (6,7,8,9,10);
Thanks in advanced for your kindly help!
Regards,
-student-
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/