please Perl help...
am 03.09.2011 05:42:31 von Charith LokuBogahawatta
Hi All,
I'm new to this group also for perl but nowadays I working around with
Perl and I need some help. this my first thread please anyone can help
me?
i have a file contains the following data.
charith 4 matara
saman 8 kandy
andrew 9 colombo
dilshan 3 galle
shanil 10 jafna
..
..
..
Here I want to replace second column (Number) with incremental number
sequence. output should be following.
charith 0 matara
saman 1 kandy
andrew 2 colombo
dilshan 3 galle
shanil 4 jafna
..
..
..
Thank you very much
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: please Perl help...
am 04.09.2011 08:22:12 von Shlomi Fish
×Hi Charith,
On Fri, 2 Sep 2011 20:42:31 -0700 (PDT)
Charith LokuBogahawatta wrote:
> Hi All,
>=20
> I'm new to this group also for perl but nowadays I working around with
> Perl and I need some help. this my first thread please anyone can help
> me?
>=20
> i have a file contains the following data.
>=20
> charith 4 matara
> saman 8 kandy
> andrew 9 colombo
> dilshan 3 galle
> shanil 10 jafna
> .
> .
> .
> Here I want to replace second column (Number) with incremental number
> sequence. output should be following.
>=20
> charith 0 matara
> saman 1 kandy
> andrew 2 colombo
> dilshan 3 galle
> shanil 4 jafna
> .
> .
> .
You can do it from the command line like:
shlomif@lap:~$ cat test.txt
charith 4 matara
saman 8 kandy
andrew 9 colombo
dilshan 3 galle
shanil 10 jafna
shlomif@lap:~$ perl -lan -e '$F[1] =3D ($counter++); print "@F";' test.txt=
=20
charith 0 matara
saman 1 kandy
andrew 2 colombo
dilshan 3 galle
shanil 4 jafna
shlomif@lap:~$=20
Note that the '...' delimiters won't work on Microsoft Windows, and you'll
need to use "..." instead. See http://perldoc.perl.org/perlrun.html for more
explanation on what I did here.=20
You can also use the -i flag (also from perlrun) to edit the file in-place.
Regards,
Shlomi Fish
>=20
> Thank you very much
>=20
>=20
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
â=9CWeâ=99re not doing it for moneyâ=A6 weâ=99re doing =
it for a shitload of money!â=9D
â=94 Spaceballs, http://www.imdb.com/title/tt0094012/
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: please Perl help...
am 04.09.2011 08:38:31 von Parag Kalra
use strict;
use warnings;
while(){
my $num =3D $. - 1;
s/\d+/$num/ if /\w+\s+\d+\s+\w/;
print $_;
}
__DATA__
charith 4 matara
saman 8 kandy
andrew 9 colombo
dilshan 3 galle
shanil 10 jafna
Parag
On Fri, Sep 2, 2011 at 8:42 PM, Charith LokuBogahawatta
wrote:
> Hi All,
>
> I'm new to this group also for perl but nowadays I working around with
> Perl and I need some help. this my first thread please anyone can help
> me?
>
> i have a file contains the following data.
>
> charith 4 matara
> saman 8 kandy
> andrew 9 colombo
> dilshan 3 Â galle
> shanil 10 jafna
> .
> .
> .
> Here  I want to replace second column (Number) with incremental numb=
er
> sequence. output should be following.
>
> charith 0 matara
> saman 1 kandy
> andrew 2 colombo
> dilshan 3 Â galle
> shanil  4 jafna
> .
> .
> .
>
> Â Thank you very much
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: please Perl help...
am 04.09.2011 08:47:38 von Uri Guttman
>>>>> "PK" == Parag Kalra writes:
PK> use strict;
PK> use warnings;
PK> while(){
PK> my $num = $. - 1;
PK> s/\d+/$num/ if /\w+\s+\d+\s+\w/;
there is no need for the if as you can just do a s/// directly. and it
can do the num part as well.
s/(\w+\s+)\d+(\s+\w+)/$1 . $. - 1 . $3/e ;
you could simplify the regex by assuming the only number is in the
middle as the data shows.
s/\d+/$. - 1/e ;
PK> print $_;
no need for the $_ as print defaults to printing it.
also please learn to edit quoted emails and to put your post below it.
uri
--
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
------------ Perl Developer Recruiting and Placement Services -------------
----- Perl Code Review, Architecture, Development, Training, Support -------
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: please Perl help...
am 04.09.2011 09:00:03 von Shlomi Fish
Hi Parag,
a few comments on your code.
On Sat, 3 Sep 2011 23:38:31 -0700
Parag Kalra wrote:
> use strict;
> use warnings;
> while(){
You should expect the contents of the file to be in a different place than
__DATA__, so one should use open or *ARGV or whatever here.=20
> my $num =3D $. - 1;
This is not needed because one can use the /e flag to s///.=20
You may wish to have a counter that is external to the loop to keep consist=
ency
and not rely on line numbers.
> s/\d+/$num/ if /\w+\s+\d+\s+\w/;
The problem here is that:
1. The regular expression match is not anchored to the beginning of the lin=
e.
2. The substitution won't work properly if the first field contains digits.
A better way to do it would be:
s/\A(\S+\s+)\d+(\s+\S)/$1 . ($. - 1) . $2/e;
Regards,
Shlomi Fish
> print $_;
> }
>=20
> __DATA__
> charith 4 matara
> saman 8 kandy
> andrew 9 colombo
> dilshan 3 galle
> shanil 10 jafna
>=20
>=20
> Parag
>=20
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
Microsoft â=94 making it all make sense. Ours.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/