RE: [RMX:#] Re: How to add to Associative Array
am 10.07.2007 12:02:24 von francois.bourgneufYour 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 (
> > 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