help in scripting
--001636310709fff7dc04a102f369
Content-Type: text/plain; charset=ISO-8859-1
hi .. i am new to perl ..
i have a input file something pasted as below ..
16 50
16 30
16 23
17 88
17 99
18 89
18 1
...
--
and i want the output something like this :
16 50 30 23
17 88 99
18 99 1
i.e for each values in the first column, i want the elements in the second
column to be a one row ..
can be anybody give me psuedocode , to how to go about this ..
thank you
--001636310709fff7dc04a102f369--
Re: help in scripting
On Saturday 16 Apr 2011 09:06:02 Gurunath Katagi wrote:
> hi .. i am new to perl ..
> i have a input file something pasted as below ..
>
> 16 50
> 16 30
> 16 23
> 17 88
> 17 99
> 18 89
> 18 1
> ..
> --
>
> and i want the output something like this :
> 16 50 30 23
> 17 88 99
> 18 99 1
>
> i.e for each values in the first column, i want the elements in the second
> column to be a one row ..
>
> can be anybody give me psuedocode , to how to go about this ..
> thank you
This should work (tested):
Regards,
Shlomi Fish
=2D---------------------------- SNIP ------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $filename =3D shift( [at] ARGV);
open my $in_fh, '<', $filename
or die "Cannot open '$filename' for reading";
my $last_key =3D undef;
my [at] values;
sub print_line
{
print $last_key, ' ', join(' ', [at] values), "\n";
return;
}
while (my $line =3D <$in_fh>)
{
chomp($line);
my ($new_key, $new_value) =3D split(/\s+/, $line);
if ( ( !defined($last_key) ) or ($last_key eq $new_key))
{
push [at] values, $new_value;
}
else
{
print_line();
[at] values =3D ($new_value);
}
$last_key =3D $new_key;
}
print_line();
close ($in_fh);
=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
"We're not doing it for money=E2=80=A6 we're doing it for a shitload of mon=
ey!"
-- 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: help in scripting
Hi.
On 16 April 2011 16:06, Gurunath Katagi <gurunath.katagi [at] gmail.com> wrote:
> hi .. i am new to perl ..
> i have a input file something pasted as below ..
>
> 16 50
> 16 30
> 16 23
> 17 88
> 17 99
> 18 89
> 18 1
> ..
> --
>
> and i want the output something like this :
> 16 50 30 23
> 17 88 99
> 18 99 1
>
> i.e for each values in the first column, i want the elements in the secon=
d
> column to be a one row ..
>
> can be anybody give me psuedocode , to how to go about this ..
> thank you
[isg [at] tarzan:perl]# cat -n foo.pl
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4 use Data::Dumper;
5 open FH,"<", "/tmp/foo" || die "aww... $!";
6 my $i =3D {};
7 while(<FH>)
8 {
9 chop;
10 my ($key, $value) =3D split(/\space/,$_);
11 push $ [at] {$i->{$key}},$value; # push $value into $i->{$key}
12 }
13 close FH;
14 local $Data::Dumper::Sortkeys =3D 1;
15 print Dumper($i);
[isg [at] tarzan:perl]# ./foo.pl
$VAR1 =3D {
'16' =3D> [
'i',
'<3',
'Python'
],
'17' =3D> [
'but',
'i'
],
'18' =3D> [
'do',
'Perl'
],
'19' =3D> [
'too'
]
};
Cheers.
--
Regards
Ishwor Gurung
Key id:0xa98db35e
Key fingerprint:FBEF 0D69 6DE1 C72B A5A8=A0 35FE 5A9B F3BB 4E5E 17B5
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
>>>>> "IG" == Ishwor Gurung <ishwor.gurung [at] gmail.com> writes:
IG> [isg [at] tarzan:perl]# cat -n foo.pl
don't post code with line numbers. then it can't be cut/pasted by
others.
IG> 1 #!/usr/bin/env perl
IG> 2 use warnings;
IG> 3 use strict;
IG> 4 use Data::Dumper;
IG> 5 open FH,"<", "/tmp/foo" || die "aww... $!";
don't use bareword file handles. use lexical handles.
IG> 6 my $i = {};
no need to initialize that to a hash ref. autovivification will do that
for you.
IG> 7 while(<FH>)
IG> 8 {
IG> 9 chop;
use chomp as it is safer.
IG> 10 my ($key, $value) = split(/\space/,$_);
what is \space? that is actually \s (a space char) followed by 'pace'
which isn't in the input stream.
IG> 11 push $ [at] {$i->{$key}},$value; # push $value into $i->{$key}
why is there a $ before the [at] ? that isn't legal perl. it means you
didn't run the code that you posted.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
On Sat, Apr 16, 2011 at 11:36 AM, Gurunath Katagi
<gurunath.katagi [at] gmail.com> wrote:
> hi .. i am new to perl ..
> i have a input file something pasted as below ..
>
> 16 50
> 16 30
> 16 23
> 17 88
> 17 99
> 18 89
> 18 1
> ..
> --
>
> and i want the output something like this :
> 16 50 30 23
> 17 88 99
> 18 99 1
>
> i.e for each values in the first column, i want the elements in the second
> column to be a one row ..
>
> can be anybody give me psuedocode , to how to go about this ..
> thank you
>
HI
i am not sure here is what you want done but , here is what i came up with .
#!/usr/bin/perl
use strict;
use warnings ;
use Data::Dumper;
my $filename = $ARGV[0];
my (%tag,$dkey,$dval);
open(INPUT_FILE, $filename) or die "cannot opnen file $!";
while (<INPUT_FILE>) {
m/(\d+)\s+(\d+)$/;
push ( [at] {$tag{$1}}, $2);
}
close(INPUT_FILE);
foreach my $key ( keys %tag ) {
print "$key --> [at] {$tag{$key}}\n";
}
Except in your question you mentioned that you require 99 in the
2nd and 3rd line also , was that a type error .
> 16 50 30 23
> 17 88 99
> 18 99 1
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
>>>>> "AG" == Agnello George <agnello.dsouza [at] gmail.com> writes:
AG> use strict;
AG> my $filename = $ARGV[0];
AG> my (%tag,$dkey,$dval);
you don't use $dkey or $dval anywhere.
AG> open(INPUT_FILE, $filename) or die "cannot opnen file $!";
don't use bareword filehandles. this is said all the time here. use
lexical handles.
AG> while (<INPUT_FILE>) {
AG> m/(\d+)\s+(\d+)$/;
you don't check if that regex succeeds or not.
and just for fun here is a one liner that works:
perl -MFile::Slurp -e '/(\d+)\s+(\d+)/ && push [at] {$h{$1}}, $2 for read_file \*STDIN; print map "$_ => [at] {$h{$_}}\n", sort keys %h'
1 2
3 4
1 9
4 8
3 00
2 4
27
1 4
1 => 2 9 4
2 => 4
3 => 4 00
4 => 8
input was ended with ^D.
i used file::slurp as that is best when reading from a file. it isn't
needed here as STDIN is already open.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
On Sat, Apr 16, 2011 at 2:36 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
>
> =A0AG> open(INPUT_FILE, $filename) =A0or die "cannot opnen file $!";
>
> don't use bareword filehandles. this is said all the time here. use
> lexical handles.
>
Could you Give me a example for lexical handles or reference me to a websi=
te .
Thanks
--
Regards
Agnello D'souza
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
>>>>> "AG" =3D=3D Agnello George <agnello.dsouza [at] gmail.com> writes:
AG> On Sat, Apr 16, 2011 at 2:36 PM, Uri Guttman <uri [at] stemsystems.com> wr=
ote:
>>
>> =A0AG> open(INPUT_FILE, $filename) =A0or die "cannot opnen file $!";
>>
>> don't use bareword filehandles. this is said all the time here. use
>> lexical handles.
>>
AG> Could you Give me a example for lexical handles or reference me to a=
website .
look in the archives of this list. perldoc -f open covers this some.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
This works.=0A=0A#!/usr/bin/perl=0Ause strict;=0Ause warnings;=0A=0Amy %has=
h=3D();=0Awhile(<>){=0A=09chomp(my $line=3D$_);=0A=09my($col1, $col2)=3Dspl=
it(/\s+/, $line);=0A=09push( [at] { $hash{$col1} }, $col2);=0A}=0A=0Afor my $key=
(sort keys %hash){=0A print "$key";=0A for ( [at] { $hash{$key} }=
){=0A=09=09print " $_";=0A=09}=0A=09print "\n";=0A}=0A=0A=0A--- On Sat, 4/1=
6/11, Shlomi Fish <shlomif [at] iglu.org.il> wrote:=0A=0A> From: Shlomi Fish <sh=
lomif [at] iglu.org.il>=0A> Subject: Re: help in scripting=0A> To: beginners [at] per=
l.org=0A> Cc: "Gurunath Katagi" <gurunath.katagi [at] gmail.com>=0A> Date: Satur=
day, April 16, 2011, 2:41 AM=0A> On Saturday 16 Apr 2011 09:06:02=0A> Gurun=
ath Katagi wrote:=0A> > hi .. i am new to perl ..=0A> > i have a input file=
something pasted as below ..=0A> > =0A> > 16 50=0A> > 16 30=0A> > 16 23=0A=
> > 17 88=0A> > 17 99=0A> > 18 89=0A> > 18 1=0A> > ..=0A> > --=0A> > =0A> >=
and i want the output something like this :=0A> > 16 50 30 23=0A> > 17 88 =
99=0A> > 18 99 1=0A> > =0A> > i.e for each values in the first column, i wa=
nt the=0A> elements in the second=0A> > column to be a one row ..=0A> > =0A=
> > can be anybody give me psuedocode , to how to go about=0A> this ..=0A> =
> thank you=0A> =0A> This should work (tested):=0A> =0A> Regards,=0A> =0A> =
=C2=A0=C2=A0=C2=A0 Shlomi Fish=0A> =0A> ----------------------------- SNIP=
=0A> ------------------------=0A> =0A> #!/usr/bin/perl=0A> =0A> use strict;=
=0A> use warnings;=0A> =0A> my $filename =3D shift( [at] ARGV);=0A> =0A> open my=
$in_fh, '<', $filename=0A> =C2=A0 =C2=A0 or die "Cannot open '$filename' f=
or=0A> reading";=0A> =0A> my $last_key =3D undef;=0A> my [at] values;=0A> =0A> =
sub print_line=0A> {=0A> =C2=A0 =C2=A0 print $last_key, ' ', join(' ', [at] val=
ues),=0A> "\n";=0A> =0A> =C2=A0 =C2=A0 return;=0A> }=0A> =0A> while (my $li=
ne =3D <$in_fh>)=0A> {=0A> =C2=A0 =C2=A0 chomp($line);=0A> =0A> =C2=A0 =C2=
=A0 my ($new_key, $new_value) =3D split(/\s+/,=0A> $line);=0A> =C2=A0 =C2=
=A0 =0A> =C2=A0 =C2=A0 if ( ( !defined($last_key) ) or ($last_key eq=0A> $n=
ew_key))=0A> =C2=A0 =C2=A0 {=0A> =C2=A0 =C2=A0 =C2=A0 =C2=A0 push [at] values, =
$new_value;=0A> =C2=A0 =C2=A0 }=0A> =C2=A0 =C2=A0 else=0A> =C2=A0 =C2=A0 {=
=0A> =C2=A0 =C2=A0 =C2=A0 =C2=A0 print_line();=0A> =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 [at] values =3D ($new_value);=0A> =C2=A0 =C2=A0 }=0A> =0A> =0A> =C2=A0 =C2=
=A0 $last_key =3D $new_key;=0A> }=0A> =0A> print_line();=0A> =0A> close ($i=
n_fh);=0A> =0A> =0A> -- =0A> ----------------------------------------------=
-------------------=0A> Shlomi Fish=C2=A0 =C2=A0 =C2=A0=C2=A0=C2=A0http://w=
ww.shlomifish.org/=0A> My Favourite FOSS - http://www.shlomifish.org/open-s=
ource/favourite/=0A> =0A> "We're not doing it for money=E2=80=A6 we're doin=
g it for a=0A> shitload of money!"=0A> =C2=A0 =C2=A0 -- Spaceballs, http://=
www.imdb.com/title/tt0094012/=0A> =0A> Please reply to list if it's a maili=
ng list post - http://shlom.in/reply .=0A> =0A> --=0A> To unsubscribe, e-ma=
il: beginners-unsubscribe [at] perl.org=0A> For additional commands, e-mail: beg=
inners-help [at] perl.org=0A> http://learn.perl.org/=0A> =0A> =0A>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
On 11-04-16 11:06 AM, tianjun xu wrote:
> print "$key";
> for ( [at] { $hash{$key} }){
> print " $_";
> }
> print "\n";
Try:
print "$key [at] {$hash{$key}}\n";
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
>>>>> "tx" == tianjun xu <tianjunx [at] yahoo.com> writes:
tx> my %hash=();
no need to initialize that to ().
tx> while(<>){
tx> chomp(my $line=$_);
why read the line into $_ and then copy it?
while( my $line = <> ) {
also please don't quote entire long emails. quote the needed part and
put your code and comments below it.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
Shlomi Fish wrote:
> On Saturday 16 Apr 2011 09:06:02 Gurunath Katagi wrote:
>> hi .. i am new to perl ..
>> i have a input file something pasted as below ..
>>
>> 16 50
>> 16 30
>> 16 23
>> 17 88
>> 17 99
>> 18 89
>> 18 1
>> ..
>> --
>>
>> and i want the output something like this :
>> 16 50 30 23
>> 17 88 99
>> 18 99 1
>>
>> i.e for each values in the first column, i want the elements in the second
>> column to be a one row ..
>>
>> can be anybody give me psuedocode , to how to go about this ..
>> thank you
>
> This should work (tested):
>
> Regards,
>
> Shlomi Fish
>
> ----------------------------- SNIP ------------------------
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $filename = shift( [at] ARGV);
>
> open my $in_fh, '<', $filename
> or die "Cannot open '$filename' for reading";
You should include the $! or $^E variable in the error message so you
know why it failed.
> my $last_key = undef;
ICK!
> my [at] values;
>
> sub print_line
> {
You are using a subroutine for this??? Really?
Why are you using global variables instead of lexically scoped
variables? You should know better.
> print $last_key, ' ', join(' ', [at] values), "\n";
Or:
print join( ' ', $last_key, [at] values ), "\n";
Or even:
print "$last_key [at] values\n";
Is there really a good reason to make it more complicated than it needs
to be?
> return;
> }
>
> while (my $line =<$in_fh>)
> {
> chomp($line);
>
> my ($new_key, $new_value) = split(/\s+/, $line);
What if there is leading whitespace?
You do realize that split(/\s+/) removes ALL (trailing) whitespace so
using chomp() is redundant.
> if ( ( !defined($last_key) ) or ($last_key eq $new_key))
> {
> push [at] values, $new_value;
> }
> else
> {
> print_line();
> [at] values = ($new_value);
> }
>
>
> $last_key = $new_key;
> }
>
> print_line();
>
> close ($in_fh);
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
[ Please do not top-post. TIA ]
tianjun xu wrote:
> This works.
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my %hash=();
> while(<>){
> chomp(my $line=$_);
> my($col1, $col2)=split(/\s+/, $line);
Or just:
while ( <> ) {
my ( $col1, $col2 ) = split;
> push( [at] { $hash{$col1} }, $col2);
> }
>
> for my $key (sort keys %hash){
> print "$key";
> for ( [at] { $hash{$key} }){
> print " $_";
> }
> print "\n";
Or just:
for my $key ( sort keys %hash ) {
print "$key [at] {$hash{$key}}\n";
> }
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: help in scripting
Hi John,
thanks for your E-mail. See below for my response.
On Sunday 17 Apr 2011 04:17:32 John W. Krahn wrote:
> Shlomi Fish wrote:
> > On Saturday 16 Apr 2011 09:06:02 Gurunath Katagi wrote:
> >> hi .. i am new to perl ..
> >> i have a input file something pasted as below ..
> >>
> >> 16 50
> >> 16 30
> >> 16 23
> >> 17 88
> >> 17 99
> >> 18 89
> >> 18 1
> >> ..
> >> --
> >>
> >> and i want the output something like this :
> >> 16 50 30 23
> >> 17 88 99
> >> 18 99 1
> >>
> >> i.e for each values in the first column, i want the elements in the
> >> second column to be a one row ..
> >>
> >> can be anybody give me psuedocode , to how to go about this ..
> >> thank you
> >
> > This should work (tested):
> >
> > Regards,
> >
> > Shlomi Fish
> >
> > ----------------------------- SNIP ------------------------
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > my $filename =3D shift( [at] ARGV);
> >
> > open my $in_fh, '<', $filename
> >
> > or die "Cannot open '$filename' for reading";
>
> You should include the $! or $^E variable in the error message so you
> know why it failed.
Correct - it was not oversight on my part.
>
> > my $last_key =3D undef;
>
> ICK!
>
Well, I'm trying to specify that I initialise it to a value where it needs =
to
be set.
> > my [at] values;
> >
> > sub print_line
> > {
>
> You are using a subroutine for this??? Really?
>
Yes, because it is duplicate code.
> Why are you using global variables instead of lexically scoped
> variables? You should know better.
>
Well, =C2=ABmy [at] values;=C2=BB is a lexically scoped variable - it's just pa=
rt of the
global scope of the program. You cannot access it from a different module.
Regarding global variables - I could have created an object and a module ou=
t
of it, but that's overengineering for such a silly script, which is all the=
author has asked for. See:
http://en.wikipedia.org/wiki/You_ain%27t_gonna_need_it
> > print $last_key, ' ', join(' ', [at] values), "\n";
>
> Or:
>
> print join( ' ', $last_key, [at] values ), "\n";
>
> Or even:
>
> print "$last_key [at] values\n";
>
> Is there really a good reason to make it more complicated than it needs
> to be?
>
Well, I dislike interpolating arrays, and I was trying to be explicit. But=
=C2=ABprint "$last_key [at] values\n";=C2=BB will also work nicely.
> > return;
> >
> > }
> >
> > while (my $line =3D<$in_fh>)
> > {
> >
> > chomp($line);
> >
> > my ($new_key, $new_value) =3D split(/\s+/, $line);
>
> What if there is leading whitespace?
What if there's an earthquake? It wasn't in the SPEC.
>
> You do realize that split(/\s+/) removes ALL (trailing) whitespace so
> using chomp() is redundant.
I realise it now. But using chomp on a line explicitly, in case you are not=
interested in keeping the newline is a good habit which should be followed=
religiously.
Regards,
Shlomi Fish
=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap
Every successful open-source project will eventually spawn a sub-project.
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: help in scripting
On 11-04-16 08:30 PM, Uri Guttman wrote:
>>>>>> "tx" == tianjun xu<tianjunx [at] yahoo.com> writes:
> tx> my %hash=();
>
> no need to initialize that to ().
>
There is always the need. Not that long ago, mod_perl use to use the
same memory as the previous run but it did not zero the memory. You got
what every was in there, including things like the session ID.
Program defensively: always initialize your variables.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/