RE: [RMX:#] Re: How to add to Associative Array

RE: [RMX:#] Re: How to add to Associative Array

am 10.07.2007 12:02:24 von francois.bourgneuf

Your program works fine, you made a mistake in the final print.

Here is a more "perlish" version
=

$file =3D 'c:/temp/export/CONVERSION/test.txt'; # You can use / instead of =
\, it works fine even under Windows
open TEST, "$file" || die "Cant open file $file";
while()
{
chomp;
if (/PLMEJob Master/../^$/)
{
chomp;
next if /PLMEJob Master/ || /^$/;
my @a =3D split /=3D/;
$info{$a[0]} =3D $a[1]; =

} =

}
close(TEST);
print "$_ =3D> $info{$_}\n" foreach sort keys %info;


Big mistake :
foreach(%info)
{
print "$_ =3D> $info{$_}\n"
}

An associative array is an array indeed !
So when you print foreach %info, you print each key and each value. Of cour=
se it wont find any value for $info{value}


> -----Message d'origine-----
> De : activeperl-bounces@listserv.ActiveState.com =

> [mailto:activeperl-bounces@listserv.ActiveState.com] De la =

> part de Beri-Veera-ext, Reddy (n.a.)
> Envoy=E9 : mardi 10 juillet 2007 10:54
> =C0 : Nathan S. Haigh
> Cc : ActivePerl
> Objet : RE: [RMX:#] Re: How to add to Associative Array
> =

> Hi all,
> I wrote a sample program with your guide lines but the associative
> array has double entries. Please the attachment for my program and
> test.txt file. Please tell me what I did the wrong. =

> =

> =

> Thanks && Regards =

> Karunakar Reddy B.V. =

> =

> -----Original Message-----
> From: Nathan S. Haigh [mailto:N.Haigh@sheffield.ac.uk] =

> Sent: 10 July 2007 10:10
> To: Beri-Veera-ext, Reddy (n.a.)
> Cc: Bill Luebkert; ActivePerl
> Subject: RE: [RMX:#] Re: How to add to Associative Array
> =

> If you want to read the lines in from a file, you need to open a
> filehandle:
> open(IN, $path_to_my_file) or die "Couldn't open file: $!\n";
> =

> then use the IN filehandle in the while loop, such as::
> foreach () {
> # stuff to do with each line
> # ...
> }
> =

> The close the filehandle with:
> close IN;
> =

> Hope this helps
> Nath
> =

> =

> Quoting "Beri-Veera-ext, Reddy (n.a.)" =

> :
> =

> > Hi,
> > Thanks for your quick info. How I will get =

> > my @lines =3D ( # phoney test data simulating file data
> > "red,danger\n",
> > "blue,going smooth\n",
> > "yellow, pending\n",
> > "Green, accept\n",
> > );
> > How I will convert test.txt to @line. =

> > Please explain this.
> > =

> > =

> > Thanks && Regards =

> > Karunakar Reddy B.V. =

> > =

> > =

> > -----Original Message-----
> > From: Bill Luebkert [mailto:dbecoll@roadrunner.com] =

> > Sent: 10 July 2007 09:48
> > To: Beri-Veera-ext, Reddy (n.a.)
> > Cc: ActivePerl
> > Subject: [RMX:#] Re: How to add to Associative Array
> > =

> > Beri-Veera-ext, Reddy (n.a.) wrote:
> > > Hello,
> > > I want to add to Associative Array dynamically (means=3D> I don't
> > exact =

> > > size of Associative Array).
> > > =

> > > Ex: In test.txt I have the fallowing data.
> > > red,danger
> > > blue,going smooth
> > > yellow, pending
> > > Green, accept.
> > > ....etc
> > > =

> > > I have to add these values to Associative Array
> > > like
> > > %info=3D (red =3D>danger
> > > blue =3D>going smooth
> > > yellow =3D> pending
> > > Green=3D>accept.
> > > ...etc)
> > > =

> > > For these I have read the test.txt line by line and add to %info.
> > Please =

> > > guide me how to do this....
> > =

> > use strict;
> > use warnings;
> > =

> > my @lines =3D ( # phoney test data simulating file data
> > "red,danger\n",
> > "blue,going smooth\n",
> > "yellow, pending\n",
> > "Green, accept\n",
> > );
> > =

> > my %info =3D ();
> > foreach (@lines) { # would normally be: while () { # reading
> > from file
> > chomp;
> > my @a =3D split /\s*,\s*/;
> > $info{$a[0]} =3D $a[1];
> > }
> > print "$_ =3D> $info{$_}\n" foreach keys %info;
> > =

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

> =

> =

> =

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

RE: [RMX:#] Re: How to add to Associative Array

am 10.07.2007 12:40:34 von Brian Raven

Mostly being picky, but ...

Bourgneuf Francois <> wrote:
> Your program works fine, you made a mistake in the final print.
> =

> Here is a more "perlish" version
> =

> $file =3D 'c:/temp/export/CONVERSION/test.txt'; # You can use / instead
> of \, it works fine even under Windows =

> open TEST, "$file" || die "Cant open file $file";

It would be better if you added the reason for failure, i.e. $!.

> while() {
> chomp;

You don't need two chomps and this one seems the most redundant.

> if (/PLMEJob Master/../^$/)

That should be 'if (/PLMEJob Master/../^\s*$/' in case the line contains
white space characters (it will contain at least one if you remove the
above chomp).

> {
> chomp;
> next if /PLMEJob Master/ || /^$/;
> my @a =3D split /=3D/;

I would make the above two lines:
my @a =3D split /=3D/, $_, 2;
next unless @a > 1;

It makes sure that @a contains no more than 2 entries, and it's a
simpler test for the line(s) to skip.

> $info{$a[0]} =3D $a[1];
> }
> }
> close(TEST);
> print "$_ =3D> $info{$_}\n" foreach sort keys %info;

Or perhaps Data::Dumper.

> =

> =

> Big mistake :
> foreach(%info)
> {
> print "$_ =3D> $info{$_}\n"
> }
> =

> An associative array is an array indeed !
> So when you print foreach %info, you print each key and each value.
> Of course it wont find any value for $info{value} =


Yup.

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