Re: Newbie queries

2011-01-27 Thread John W. Krahn

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 @array = split /,/, $line;

Then split will return the list:

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

But if the third argument is a negative number:

my @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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie queries

2011-01-27 Thread Octavian Rasnita

From: "dolphin" 

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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie queries

2011-01-27 Thread dolphin
On Jan 25, 6:43 pm, orasn...@gmail.com ("Octavian Rasnita") wrote:
> From: "dolphin" 
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > I'm learning perl now and would like to find out how to perform the
> > following if given in a text file?
>
> > ,12
> > ,437
> > ,124
> > ,45
> > ,789
> > ,67
> > CCC,567
> > DDD,5
>
> > How to sum up the 2nd column based on the 1st column with the
> > following result:
>
> > :
> > 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

Hi,

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


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie queries

2011-01-25 Thread Mariano Loza Coll
On Tue, Jan 25, 2011 at 2:43 AM, Octavian Rasnita  wrote:
> From: "dolphin" 
>>
>> Hi,
>>
>> I'm learning perl now and would like to find out how to perform the
>> following if given in a text file?
>>
>>
>> ,12
>> ,437
>> ,124
>> ,45
>> ,789
>> ,67
>> CCC,567
>> DDD,5
>>
>>
>> How to sum up the 2nd column based on the 1st column with the
>> following result:
>>
>>
>> :
>> 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-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Newbie queries

2011-01-25 Thread Octavian Rasnita

From: "dolphin" 

Hi,

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


,12
,437
,124
,45
,789
,67
CCC,567
DDD,5


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


:
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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Newbie queries

2011-01-25 Thread dolphin
Hi,

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


,12
,437
,124
,45
,789
,67
CCC,567
DDD,5


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


:
BBB:
CCC:
DDD:


Thanks in advance.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/