Newbie queries

Hi,

I'm learning perl now and would like to find out how to perform the
following if given in a text file?


AAAA,12
AAAA,437
BBBB,124
CCCC,45
BBBB,789
AAAA,67
CCC,567
DDD,5


How to sum up the 2nd column based on the 1st column with the
following result:


AAAA:
BBB:
CCC:
DDD:


Thanks in advance.



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
yc282004 [ Di, 25 Januar 2011 08:44 ] [ ID #2053819 ]

Re: Newbie queries

From: "dolphin" <yc282004 [at] yahoo.com.sg>
> Hi,
>
> I'm learning perl now and would like to find out how to perform the
> following if given in a text file?
>
>
> AAAA,12
> AAAA,437
> BBBB,124
> CCCC,45
> BBBB,789
> AAAA,67
> CCC,567
> DDD,5
>
>
> How to sum up the 2nd column based on the 1st column with the
> following result:
>
>
> AAAA:
> BBB:
> CCC:
> DDD:
>
>
> Thanks in advance.


You need to read the file line by line:


use strict;
use warnings;

my %items; #The hash with labels and their corresponding sums

open my $file, '<', 'file_name' or die $!;

while ( my $line = <$file> ) {

# Here split the line:
my ( $label, $value ) = split /,/, $line, 2;

# Then add each value for its corresponding label:
$items{$label} += $value;
}

close $file;

# Here print the sorted hash of items:
for my $label ( sort keys %items ) {
print "$label: $items{$label}\n";
}

HTH.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ Di, 25 Januar 2011 11:43 ] [ ID #2053820 ]

Re: Newbie queries

On Tue, Jan 25, 2011 at 2:43 AM, Octavian Rasnita <orasnita [at] gmail.com> wrot=
e:
> From: "dolphin" <yc282004 [at] yahoo.com.sg>
>>
>> Hi,
>>
>> I'm learning perl now and would like to find out how to perform the
>> following if given in a text file?
>>
>>
>> AAAA,12
>> AAAA,437
>> BBBB,124
>> CCCC,45
>> BBBB,789
>> AAAA,67
>> CCC,567
>> DDD,5
>>
>>
>> How to sum up the 2nd column based on the 1st column with the
>> following result:
>>
>>
>> AAAA:
>> BBB:
>> CCC:
>> DDD:
>>
>>
>> Thanks in advance.
>
>
> You need to read the file line by line:
>
>
> use strict;
> use warnings;
>
> my %items; =A0 =A0#The hash with labels and their corresponding sums
>
> open my $file, '<', 'file_name' or die $!;
>
> while ( my $line =3D <$file> ) {
>
> =A0 # Here split the line:
> =A0 my ( $label, $value ) =3D split /,/, $line, 2;
>
> =A0 # Then add each value for its corresponding label:
> =A0 $items{$label} +=3D $value;
> }
>
> close $file;
>
> # Here print the sorted hash of items:
> for my $label ( sort keys %items ) {
> =A0 print "$label: $items{$label}\n";
> }
>
> HTH.
>
> Octavian
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>

Thanks Octavian. I'm also new to Perl and yours was a very simple and
useful explanation.
Cheers,

Mariano

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Mariano Loza Coll [ Di, 25 Januar 2011 20:34 ] [ ID #2053822 ]

Re: Newbie queries

On Jan 25, 6:43=A0pm, orasn... [at] gmail.com ("Octavian Rasnita") wrote:
> From: "dolphin" <yc282... [at] yahoo.com.sg>
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > I'm learning perl now and would like to find out how to perform the
> > following if given in a text file?
>
> > AAAA,12
> > AAAA,437
> > BBBB,124
> > CCCC,45
> > BBBB,789
> > AAAA,67
> > CCC,567
> > DDD,5
>
> > How to sum up the 2nd column based on the 1st column with the
> > following result:
>
> > AAAA:
> > BBB:
> > CCC:
> > DDD:
>
> > Thanks in advance.
>
> You need to read the file line by line:
>
> use strict;
> use warnings;
>
> my %items; =A0 =A0#The hash with labels and their corresponding sums
>
> open my $file, '<', 'file_name' or die $!;
>
> while ( my $line =3D <$file> ) {
>
> =A0 =A0 # Here split the line:
> =A0 =A0 my ( $label, $value ) =3D split /,/, $line, 2;
>
> =A0 =A0 # Then add each value for its corresponding label:
> =A0 =A0 $items{$label} +=3D $value;
>
> }
>
> close $file;
>
> # Here print the sorted hash of items:
> for my $label ( sort keys %items ) {
> =A0 =A0 print "$label: $items{$label}\n";
>
> }
>
> HTH.
>
> Octavian

Hi,

Correct me that the "2" in the following means read?
:
my ( $label, $value ) =3D split /,/, $line, 2;


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
yc282004 [ Mi, 26 Januar 2011 23:13 ] [ ID #2053955 ]

Re: Newbie queries

From: "dolphin" <yc282004 [at] yahoo.com.sg>
> Hi,
>
> Correct me that the "2" in the following means read?
> :
> my ( $label, $value ) = split /,/, $line, 2;


2 means that the result of the split() function will be a list with 2
elements.
You could also use the split() function without that third parameter that
specifies the number of the returned elements:

my ( $label, $value ) = split /,/, $line;

But the first variant is a little bit faster.

Read:

perldoc -f split

Octavian



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ Do, 27 Januar 2011 11:26 ] [ ID #2053957 ]

Re: Newbie queries

dolphin wrote:
>
> Hi,

Hello,

> Correct me that the "2" in the following means read?
> :
> my ( $label, $value ) = split /,/, $line, 2;

The third argument to split determines how many list elements will be
returned. For example, if:

my $line = 'one,two,three,four,five,six,seven';

Then:

split /,/, $line, 2

Would return the list:

( 'one', 'two,three,four,five,six,seven' )

However if you did:

my ( $label, $value ) = split /,/, $line;

Then split would return the list:

( 'one', 'two', 'three,four,five,six,seven' )

Just as if you had done:

my ( $label, $value ) = split /,/, $line, 3;

Perl knows how many list elements are expected and only splits enough to
satisfy that request.

Also, if:

my $line = 'one,two,three,,,,';

And you do:

my [at] array = split /,/, $line;

Then split will return the list:

( 'one', 'two', 'three' )

But if the third argument is a negative number:

my [at] array = split /,/, $line, -1;

Then split will return the list:

( 'one', 'two', 'three', '', '', '', '' )




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/
jwkrahn [ Do, 27 Januar 2011 12:06 ] [ ID #2053958 ]
Perl » gmane.comp.lang.perl.beginners » Newbie queries

Vorheriges Thema: Dereference Links
Nächstes Thema: string index fucntion