please Perl help...
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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: please Perl help...
=D7=81Hi Charith,
On Fri, 2 Sep 2011 20:42:31 -0700 (PDT)
Charith LokuBogahawatta <charith079 [at] gmail.com> 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 number
> sequence. output should be following.
>
> 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 [at] lap:~$ cat test.txt
charith 4 matara
saman 8 kandy
andrew 9 colombo
dilshan 3 galle
shanil 10 jafna
shlomif [at] lap:~$ perl -lan -e '$F[1] =3D ($counter++); print " [at] F";' test.txt=
charith 0 matara
saman 1 kandy
andrew 2 colombo
dilshan 3 galle
shanil 4 jafna
shlomif [at] lap:~$
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.
You can also use the -i flag (also from perlrun) to edit the file in-place.
Regards,
Shlomi Fish
>
> Thank you very much
>
>
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
=E2=80=9CWe=E2=80=99re not doing it for money=E2=80=A6 we=E2=80=99re doing =
it for a shitload of money!=E2=80=9D
=E2=80=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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: please Perl help...
use strict;
use warnings;
while(<DATA>){
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
<charith079 [at] gmail.com> 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 =C2=A0galle
> shanil 10 jafna
> .
> .
> .
> Here =C2=A0I 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 =C2=A0galle
> shanil =C2=A04 jafna
> .
> .
> .
>
> =C2=A0Thank you very much
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: please Perl help...
>>>>> "PK" == Parag Kalra <paragkalra [at] gmail.com> writes:
PK> use strict;
PK> use warnings;
PK> while(<DATA>){
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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: please Perl help...
Hi Parag,
a few comments on your code.
On Sat, 3 Sep 2011 23:38:31 -0700
Parag Kalra <paragkalra [at] gmail.com> wrote:
> use strict;
> use warnings;
> while(<DATA>){
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.
> my $num =3D $. - 1;
This is not needed because one can use the /e flag to s///.
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 $_;
> }
>
> __DATA__
> charith 4 matara
> saman 8 kandy
> andrew 9 colombo
> dilshan 3 galle
> shanil 10 jafna
>
>
> Parag
>
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
Microsoft =E2=80=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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/