concatenate a hash key

concatenate a hash key

am 18.05.2011 20:29:16 von Chris Stinemetz

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

Re: concatenate a hash key

am 18.05.2011 20:34:27 von Jim Gibson

On 5/18/11 Wed May 18, 2011 11:29 AM, "Chris Stinemetz"
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 = ) {
my @fields = split(/,/,$line);
my $key = join('-',@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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: concatenate a hash key

am 18.05.2011 21:30:32 von Rob Dixon

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 () {
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/