Trying to "map" an array into a hash

Trying to "map" an array into a hash

am 03.01.2008 17:55:11 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============0281807353==
Content-Type: multipart/alternative;
boundary="=_alternative 005CF22E862573C5_="

This is a multipart message in MIME format.
--=_alternative 005CF22E862573C5_=
Content-Type: text/plain; charset="US-ASCII"

Gurus,

I'm trying to use map--instead of a for loop--to split an array into a
hash. As you can see by the output I'm getting, it ain't workin'. Am I
doing something wrong, or am I just trying the impossible? Here's the
code, followed by what I get, followed by what I *want* to get.

TIA!

#####
# program:
#####
use strict;
use warnings;

my @data = (
'########################################',
'# jhsdkjfhh - Copyright 2005, #',
'# #',
'# Name : asdfsdfsdas #',
'# #',
'# Description : This is the Inlue set) #',
'# file used byasder. #',
'# #',
'# Notes : Its somewhat pdded to #',
'# #',
'# Modification Log: #',
'# Rev. Date Init. Ch #',
'# ---- ----------- ----- ---------- #',
'# 1.0 31 Jan 2005 dcr Initial #',
'# 2.0 18 Jan 2006 dcr Windsdf, #',
'# and reo #',
'# 3.0 02 Jan 2008 dcr Add four #',
'########################################',
'',
'000 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'001 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'002 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'003 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'004 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'005 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'006 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'007 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'008 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'009 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'',
'# This is a COMMENT LINE!',
'010 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'011 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'012 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'013 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'014 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'015 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'016 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'017 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'018 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'019 |Somewhere/something/thisplace/someplace/Things/here//Value' ,
'',
);

my %hash = map { split /\s\|/ } @ary; # line 76

foreach my $key (sort( keys( %hash ))) {
print "$key: $hash{$key}\n"; # line 93
}


#####
# program output:
#####
STDERR:
Odd number of elements in hash assignment at E:\WSUp_Dev\map_test.pl line
76.
Use of uninitialized value in concatenation (.) or string at map_test.pl
line 93.

STDOUT:
------------------------------------------------------------ ---------------
Actual:
# #: # Description : This is the
Inlue set) #
# file used byasder. #: # #
# ---- ----------- ----- ---------- #: # 1.0 31 Jan 2005 dcr
Initial #
# 2.0 18 Jan 2006 dcr Windsdf, #: # and
reo #
# 3.0 02 Jan 2008 dcr Add four #:
########################################
# Modification Log: #: # Rev. Date Init. Ch
#
# Notes : Its somewhat pdded to #: # #
# This is a COMMENT LINE!: 010
########################################: # jhsdkjfhh - Copyright 2005,
#
000: Somewhere/something/thisplace/someplace/Things/here//Value
001: Somewhere/something/thisplace/someplace/Things/here//Value
002: Somewhere/something/thisplace/someplace/Things/here//Value
003: Somewhere/something/thisplace/someplace/Things/here//Value
004: Somewhere/something/thisplace/someplace/Things/here//Value
005: Somewhere/something/thisplace/someplace/Things/here//Value
006: Somewhere/something/thisplace/someplace/Things/here//Value
007: Somewhere/something/thisplace/someplace/Things/here//Value
008: Somewhere/something/thisplace/someplace/Things/here//Value
009: Somewhere/something/thisplace/someplace/Things/here//Value
Somewhere/something/thisplace/someplace/Things/here//Value:
------------------------------------------------------------ ---------------
Desired:
000: Somewhere/something/thisplace/someplace/Things/here//Value
001: Somewhere/something/thisplace/someplace/Things/here//Value
002: Somewhere/something/thisplace/someplace/Things/here//Value
003: Somewhere/something/thisplace/someplace/Things/here//Value
004: Somewhere/something/thisplace/someplace/Things/here//Value
005: Somewhere/something/thisplace/someplace/Things/here//Value
006: Somewhere/something/thisplace/someplace/Things/here//Value
007: Somewhere/something/thisplace/someplace/Things/here//Value
008: Somewhere/something/thisplace/someplace/Things/here//Value
009: Somewhere/something/thisplace/someplace/Things/here//Value
010: Somewhere/something/thisplace/someplace/Things/here//Value
011: Somewhere/something/thisplace/someplace/Things/here//Value
012: Somewhere/something/thisplace/someplace/Things/here//Value
013: Somewhere/something/thisplace/someplace/Things/here//Value
014: Somewhere/something/thisplace/someplace/Things/here//Value
015: Somewhere/something/thisplace/someplace/Things/here//Value
016: Somewhere/something/thisplace/someplace/Things/here//Value
017: Somewhere/something/thisplace/someplace/Things/here//Value
018: Somewhere/something/thisplace/someplace/Things/here//Value
019: Somewhere/something/thisplace/someplace/Things/here//Value

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Oh, what a tangled web do parents weave, when they think their children
are naive. -- Ogden Nash
--=_alternative 005CF22E862573C5_=
Content-Type: text/html; charset="US-ASCII"



Gurus,



I'm trying to use map--instead of a
for loop--to split an array into a hash. As you can see by the output I'm
getting, it ain't workin'. Am I doing something wrong, or am I just trying
the impossible?  Here's the code, followed by what I get, followed
by what I *want* to get.




TIA!



#####

# program:

#####

use strict;

use warnings;



my @data = (

   '########################################',

   '# jhsdkjfhh - Copyright
2005,          #',


   '#      
                     
         #',


   '# Name    
   : asdfsdfsdas            #',


   '#      
                     
         #',


   '# Description : This
is the Inlue set) #',


   '#      
        file used byasder.     #',


   '#      
                     
         #',


   '# Notes    
  : Its somewhat pdded to  #',


   '#      
                     
         #',


   '# Modification Log:  
                 #',


   '# Rev.    
Date      Init.  Ch         #',


   '# ----  -----------
 -----  ---------- #',


   '# 1.0   31 Jan 2005
  dcr   Initial    #',


   '# 2.0   18 Jan 2006
  dcr   Windsdf,   #',


   '#      
                    and
reo    #',


   '# 3.0   02 Jan 2008
  dcr   Add four   #',


   '########################################',

   '',

   '000 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '001 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '002 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '003 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '004 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '005 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '006 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '007 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '008 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '009 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '',

   '# This is a COMMENT LINE!',

   '010 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '011 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '012 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '013 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '014 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '015 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '016 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '017 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '018 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '019 |Somewhere/something/thisplace/someplace/Things/here//Value' ,

   '',

   );



my %hash = map { split /\s\|/ } @ary;
        # line 76




foreach my $key (sort( keys( %hash
))) {


   print "$key: $hash{$key}\n";
              # line 93


}





#####

# program output:

#####

STDERR:

Odd number of elements in hash assignment
at E:\WSUp_Dev\map_test.pl line 76.


Use of uninitialized value in concatenation
(.) or string at map_test.pl line 93.




STDOUT:

------------------------------------------------------------ ---------------

Actual:

#          
                     
     #: # Description : This is the Inlue set) #


#          
    file used byasder.     #: #      
                     
         #


# ----  -----------  -----
 ---------- #: # 1.0   31 Jan 2005   dcr   Initial
   #


# 2.0   18 Jan 2006   dcr
  Windsdf,   #: #              
            and reo    #


# 3.0   02 Jan 2008   dcr
  Add four   #: ########################################


# Modification Log:      
             #: # Rev.    
Date      Init.  Ch         #


# Notes       : Its
somewhat pdded to  #: #            
                     
   #


# This is a COMMENT LINE!: 010

########################################:
# jhsdkjfhh - Copyright 2005,          #


000: Somewhere/something/thisplace/someplace/Things/here//Value

001: Somewhere/something/thisplace/someplace/Things/here//Value

002: Somewhere/something/thisplace/someplace/Things/here//Value

003: Somewhere/something/thisplace/someplace/Things/here//Value

004: Somewhere/something/thisplace/someplace/Things/here//Value

005: Somewhere/something/thisplace/someplace/Things/here//Value

006: Somewhere/something/thisplace/someplace/Things/here//Value

007: Somewhere/something/thisplace/someplace/Things/here//Value

008: Somewhere/something/thisplace/someplace/Things/here//Value

009: Somewhere/something/thisplace/someplace/Things/here//Value

Somewhere/something/thisplace/someplace/Things/here//Value:


------------------------------------------------------------ ---------------

Desired:

000: Somewhere/something/thisplace/someplace/Things/here//Value

001: Somewhere/something/thisplace/someplace/Things/here//Value

002: Somewhere/something/thisplace/someplace/Things/here//Value

003: Somewhere/something/thisplace/someplace/Things/here//Value

004: Somewhere/something/thisplace/someplace/Things/here//Value

005: Somewhere/something/thisplace/someplace/Things/here//Value

006: Somewhere/something/thisplace/someplace/Things/here//Value

007: Somewhere/something/thisplace/someplace/Things/here//Value

008: Somewhere/something/thisplace/someplace/Things/here//Value

009: Somewhere/something/thisplace/someplace/Things/here//Value

010: Somewhere/something/thisplace/someplace/Things/here//Value

011: Somewhere/something/thisplace/someplace/Things/here//Value

012: Somewhere/something/thisplace/someplace/Things/here//Value

013: Somewhere/something/thisplace/someplace/Things/here//Value

014: Somewhere/something/thisplace/someplace/Things/here//Value

015: Somewhere/something/thisplace/someplace/Things/here//Value

016: Somewhere/something/thisplace/someplace/Things/here//Value

017: Somewhere/something/thisplace/someplace/Things/here//Value

018: Somewhere/something/thisplace/someplace/Things/here//Value

019: Somewhere/something/thisplace/someplace/Things/here//Value



Deane Rothenmaier

Programmer/Analyst

Walgreens Corp.

847-914-5150



Oh, what a tangled web do parents weave, when they think their children
are naive. -- Ogden Nash

--=_alternative 005CF22E862573C5_=--


--===============0281807353==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0281807353==--

Re: Trying to "map" an array into a hash

am 03.01.2008 18:03:06 von Andy_Bach

didyou expect the comments to be removed automatically? Hmm (note the
@ary/@data change)

my %hash = map { split /\s\|/ } grep { /\|/ } @data; # line 76

works:
000: Somewhere/something/thisplace/someplace/Things/here//Value
001: Somewhere/something/thisplace/someplace/Things/here//Value
002: Somewhere/something/thisplace/someplace/Things/here//Value
003: Somewhere/something/thisplace/someplace/Things/here//Value
004: Somewhere/something/thisplace/someplace/Things/here//Value
005: Somewhere/something/thisplace/someplace/Things/here//Value
006: Somewhere/something/thisplace/someplace/Things/here//Value
007: Somewhere/something/thisplace/someplace/Things/here//Value
008: Somewhere/something/thisplace/someplace/Things/here//Value
009: Somewhere/something/thisplace/someplace/Things/here//Value
010: Somewhere/something/thisplace/someplace/Things/here//Value
011: Somewhere/something/thisplace/someplace/Things/here//Value
012: Somewhere/something/thisplace/someplace/Things/here//Value
013: Somewhere/something/thisplace/someplace/Things/here//Value
014: Somewhere/something/thisplace/someplace/Things/here//Value
015: Somewhere/something/thisplace/someplace/Things/here//Value
016: Somewhere/something/thisplace/someplace/Things/here//Value
017: Somewhere/something/thisplace/someplace/Things/here//Value
018: Somewhere/something/thisplace/someplace/Things/here//Value
019: Somewhere/something/thisplace/someplace/Things/here//Value

a

Andy Bach
Systems Mangler
Internet: andy_bach@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

And the users exclaimed with a snarl and a taunt,
"It's just what we asked for, but not what we want!"
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Trying to "map" an array into a hash

am 03.01.2008 18:04:50 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============0723737211==
Content-Type: multipart/alternative;
boundary="=_alternative 005DD470862573C5_="

This is a multipart message in MIME format.
--=_alternative 005DD470862573C5_=
Content-Type: text/plain; charset="US-ASCII"

Thanks Andy! The "Systems Mangler" strikes again!

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Oh, what a tangled web do parents weave, when they think their children
are naive. -- Ogden Nash
--=_alternative 005DD470862573C5_=
Content-Type: text/html; charset="US-ASCII"



Thanks Andy!  The "Systems
Mangler" strikes again!




Deane Rothenmaier

Programmer/Analyst

Walgreens Corp.

847-914-5150



Oh, what a tangled web do parents weave, when they think their children
are naive. -- Ogden Nash

--=_alternative 005DD470862573C5_=--


--===============0723737211==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0723737211==--

Re: Trying to "map" an array into a hash

am 03.01.2008 18:22:31 von Ingo Schwarze

Deane.Rothenmaier@walgreens.com wrote on Thu, Jan 03, 2008 at 10:55:11AM -0600:

> my %hash = map { split /\s\|/ } @ary; # line 76

If you want to allow comments, you need to handle them:

my %hash = map { /^#/ ? () : split /\s\|/ } @data;

Note that this is not safe against invalid input,
i.e. non-empty non-comment lines not containing '|'.
Also, for simplicity, leading blanks are not allowed.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Trying to "map" an array into a hash

am 03.01.2008 18:22:39 von Brian Raven

Andy_Bach@wiwb.uscourts.gov <> wrote:
> didyou expect the comments to be removed automatically? Hmm (note
> the @ary/@data change) =

> =

> my %hash =3D map { split /\s\|/ } grep { /\|/ } @data; # line 76

Making sure that split can't produce more that 2 fields may be a bit
safer, just in case there is a second occurrence of the separator.

my %hash =3D map { split /\s\|/, $_, 2 } grep { /\|/ } @data;

HTH

-- =

Brian Raven =


==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================

The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =

Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.

L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.

Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Trying to "map" an array into a hash

am 03.01.2008 18:42:11 von Ingo Schwarze

> my %hash = map { split /\s\|/ } grep { /\|/ } @data; # line 76

Comments containing '|' will produce surprising results.

When parsing data, i would recommend not to aim for brevity,
but to aim for clarity, robustness and maintainability -
unless you are training for a code obfuscation contest.

That said, i would probably go for something like this:

my %hash;
foreach( @data ){
next if /^\s*$/; # skip blank lines
next if /^#/; # skip comments
# You should probably add more validation logic to the following line.
die "$_\nparse error" unless /^\s*(\d+)\s*\|(.*)/;
$hash{$1} = $2;
}

--
Ingo Schwarze | Software Engineer | Framework Team
Astaro AG | www.astaro.com
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs