concatenate a hash key

I have a hash table:

my %formInstance =3D ( "Cell" =3D> "csno",
"Servgrp" =3D> "svgrp",
"Sector" =3D> "antfc" );



While I am iterating through the rows of the input data how can I concatena=
te a hash key as the first three fields? (i.e. 871-0-1)

Input data looks like:

871,0,1,871,2,0,n,0,n
871,0,1,871,3,1,n,0,n
871,0,1,872,1,0,n,0,n
871,0,1,872,3,1,n,0,n
871,0,1,876,1,3,n,0,n
871,0,1,877,1,3,n,0,n
871,0,1,877,3,2,n,0,n
871,0,1,882,1,1,n,0,n
871,0,1,882,3,0,n,0,n
871,0,1,885,3,3,n,0,n
871,0,1,895,2,0,n,0,n
871,0,2,871,1,1,n,0,n
871,0,2,871,3,1,n,0,n
871,0,2,872,1,3,n,0,n
871,0,2,872,3,2,n,0,n
871,0,2,873,1,1,n,0,n
871,0,2,873,3,3,n,0,n
871,0,2,874,1,1,n,0,n
871,0,2,874,3,0,n,0,n
871,0,2,875,3,1,n,0,n
871,0,2,877,3,3,n,0,n
871,0,2,882,1,3,n,0,n
871,0,2,882,2,3,n,0,n
871,0,2,882,3,0,n,0,n
871,0,2,884,1,0,n,0,n
871,0,2,886,3,2,n,0,n
871,0,2,895,2,2,n,0,n
871,0,3,871,1,3,n,0,n
871,0,3,871,2,0,n,0,n
871,0,3,873,1,3,n,0,n
871,0,3,873,2,2,n,0,n





--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Chris Stinemetz [ Mi, 18 Mai 2011 20:29 ] [ ID #2059660 ]

Re: concatenate a hash key

On 5/18/11 Wed May 18, 2011 11:29 AM, "Chris Stinemetz"
<cstinemetz [at] cricketcommunications.com> scribbled:

>
> I have a hash table:
>
> my %formInstance = ( "Cell" => "csno",
> "Servgrp" => "svgrp",
> "Sector" => "antfc" );
>
>
>
> While I am iterating through the rows of the input data how can I concatenate
> a hash key as the first three fields? (i.e. 871-0-1)

while( my $line = <DATA> ) {
my [at] fields = split(/,/,$line);
my $key = join('-', [at] fields[0..2]);
# and you are good to go
}

>
> Input data looks like:
>
> 871,0,1,871,2,0,n,0,n
> 871,0,1,871,3,1,n,0,n
> 871,0,1,872,1,0,n,0,n



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 18 Mai 2011 20:34 ] [ ID #2059661 ]

Re: concatenate a hash key

On 18/05/2011 19:29, Chris Stinemetz wrote:
>
> I have a hash table:
>
> my %formInstance = ( "Cell" => "csno",
> "Servgrp" => "svgrp",
> "Sector" => "antfc" );
>
>
>
> While I am iterating through the rows of the input data how can I
> concatenate a hash key as the first three fields? (i.e. 871-0-1)
>
> Input data looks like:
>
> 871,0,1,871,2,0,n,0,n
> 871,0,1,871,3,1,n,0,n
> 871,0,1,872,1,0,n,0,n
> 871,0,1,872,3,1,n,0,n
> 871,0,1,876,1,3,n,0,n
> 871,0,1,877,1,3,n,0,n
> 871,0,1,877,3,2,n,0,n
> 871,0,1,882,1,1,n,0,n
> 871,0,1,882,3,0,n,0,n
> 871,0,1,885,3,3,n,0,n
> 871,0,1,895,2,0,n,0,n
> 871,0,2,871,1,1,n,0,n
> 871,0,2,871,3,1,n,0,n
> 871,0,2,872,1,3,n,0,n
> 871,0,2,872,3,2,n,0,n
> 871,0,2,873,1,1,n,0,n
> 871,0,2,873,3,3,n,0,n
> 871,0,2,874,1,1,n,0,n
> 871,0,2,874,3,0,n,0,n
> 871,0,2,875,3,1,n,0,n
> 871,0,2,877,3,3,n,0,n
> 871,0,2,882,1,3,n,0,n
> 871,0,2,882,2,3,n,0,n
> 871,0,2,882,3,0,n,0,n
> 871,0,2,884,1,0,n,0,n
> 871,0,2,886,3,2,n,0,n
> 871,0,2,895,2,2,n,0,n
> 871,0,3,871,1,3,n,0,n
> 871,0,3,871,2,0,n,0,n
> 871,0,3,873,1,3,n,0,n
> 871,0,3,873,2,2,n,0,n

Hi Chris

How about something like the code below?

HTH,

Rob


use strict;
use warnings;

while (<DATA>) {
my $key = join '-', (split /,/)[0..2];
print $key, "\n";
}

__DATA__
871,0,1,871,2,0,n,0,n
871,0,1,871,3,1,n,0,n
871,0,1,872,1,0,n,0,n
871,0,1,872,3,1,n,0,n
871,0,1,876,1,3,n,0,n
871,0,1,877,1,3,n,0,n
871,0,1,877,3,2,n,0,n
871,0,1,882,1,1,n,0,n
871,0,1,882,3,0,n,0,n
871,0,1,885,3,3,n,0,n
871,0,1,895,2,0,n,0,n

**OUTPUT**

871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1
871-0-1

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Rob Dixon [ Mi, 18 Mai 2011 21:30 ] [ ID #2059662 ]
Perl » gmane.comp.lang.perl.beginners » concatenate a hash key

Vorheriges Thema: memory doesn't free after lexical block
Nächstes Thema: help me for this error