Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

each() function used with Associative array gives unordered incorrect output. #4873

Closed
p5pRT opened this issue Jan 18, 2002 · 5 comments
Closed

Comments

@p5pRT
Copy link

p5pRT commented Jan 18, 2002

Migrated from rt.perl.org#8283 (status was 'resolved')

Searchable as RT8283$

@p5pRT
Copy link
Author

p5pRT commented Jan 18, 2002

From arvindt@tis.co.in

Hi.
Please find attached the bug report.
I need to know about this bug ASAP due to a deadline else I will have to
stop using ActivePerl as I have to complete the project.

SOS....
Regards.

Arvind Tiwari
Software Engineer
Tata Interactive Systems
India.

@p5pRT
Copy link
Author

p5pRT commented Jan 18, 2002

From arvindt@tis.co.in

Created by Administrator@localhost

%array = ( "100", "Green", "200", "Orange");

while (($key, $value) = each(%array)) {
  print("$key = $value\n");
}

Acc. to the functionality of above code, each() function used on associative array, This program should print the following​:

100 = Green
200 = Orange

But on running it on my machine, it returns​:

200 = Orange
100 = Green

On adding more key, value pairs it gives different results which I cannot find a pattern for.
This same code when run on other machine with different perl running on it works. I really need to know if this can be sorted as I have a deadline to meet.

Regards.

Perl Info

Flags:
    category=core
    severity=critical

Site configuration information for perl v5.6.1:

Configured by Administrator at Wed Jan  2 17:16:07 2002.

Summary of my perl5 (revision 5 version 6 subversion 1) configuration:
  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname=''
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    usethreads=undef use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=undef d_sfio=undef uselargefiles=undef usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
  Compiler:
    cc='cl', ccflags ='-nologo -O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT  -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX',
    optimize='-O1 -MD -DNDEBUG',
    cppflags='-DWIN32'
    ccversion='', gccversion='', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
    alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='link', ldflags ='-nologo -nodefaultlib -release  -libpath:"D:\Perl\lib\CORE"  -machine:x86'
    libpth="D:\Perl\lib\CORE"
    libs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib msvcrt.lib
    perllibs=  oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib  comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib  netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib  version.lib odbc32.lib odbccp32.lib msvcrt.lib
    libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl56.lib
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release  -libpath:"D:\Perl\lib\CORE"  -machine:x86'

Locally applied patches:
    ACTIVEPERL_LOCAL_PATCHES_ENTRY


@INC for perl v5.6.1:
    D:/Perl/lib
    D:/Perl/site/lib
    .


Environment for perl v5.6.1:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=D:\Perl\bin\;D:\Perl\bin\;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem
    PERL_BADLANG (unset)
    SHELL (unset)

@p5pRT
Copy link
Author

p5pRT commented Jan 18, 2002

From [Unknown Contact. See original ticket]

On Jan 19, Arvind Tiwari said​:

Please find attached the bug report.
I need to know about this bug ASAP due to a deadline else I will have to
stop using ActivePerl as I have to complete the project.

Hashes are not kept in the order that you create them in. This is an
intrinsic feature of hashes in virtually any language that supports them.

If you need a sorted hash in Perl, you can use the Tie​::IxHash module,
available on CPAN.

--
Jeff "japhy" Pinyan japhy@​pobox.com http​://www.pobox.com/~japhy/
RPI Acacia brother #734 http​://www.perlmonks.org/ http​://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

@p5pRT
Copy link
Author

p5pRT commented Jan 18, 2002

From [Unknown Contact. See original ticket]

Thus spake Arvind Tiwari (arvindt@​tis.co.in)​:

You don't seem to have a bug.

%array = ( "100", "Green", "200", "Orange");

while (($key, $value) = each(%array)) {
print("$key = $value\n");
}

Acc. to the functionality of above code, each() function used
on associative array, This program should print the following​:

100 = Green
200 = Orange

But on running it on my machine, it returns​:

200 = Orange
100 = Green

This is not a bug. The docs say each() returns its results in
apparently random order (it's not really random, but it's to
do with the hashing order instead of the sorted order).

On adding more key, value pairs it gives different results
which I cannot find a pattern for.

The pattern is in the hashing code.

This same code when run on other machine with different
perl running on it works. I really need to know if this
can be sorted as I have a deadline to meet.

The first case is working, and the machine which seems to be
auto-sorting is doing something else under the hood as well.

If you really need your values sorted, perhaps you should do
something like this​:

  my @​key = sort ( keys %hash );
  foreach ( @​key ) {
  print "$_ = $hash{$_}\n";
  }

It takes a little more memory, but it's how I'd do it.

For future programming questions, please see Usenet groups, the
Perlmonks website, or one of the Perl programming mailing lists.
This list is for the development of the perl system. It's not
fair to the community to ask us to search the docs for you.

Chris Stith

--
People understand instinctively that the best way for computer programs to
communicate with each other is for each of the them to be strict in what they
emit, and liberal in what they accept. The odd thing is that people themselves
are not willing to be strict in how they speak, and liberal in how they listen.
-- Larry Wall, 2nd State of the Onion Address, August 1998

@p5pRT
Copy link
Author

p5pRT commented Jan 18, 2002

From @schwern

User had a misconception that hashes are ordered.

--

Michael G. Schwern <schwern@​pobox.com> http​://www.pobox.com/~schwern/
Perl Quality Assurance <perl-qa@​perl.org> Kwalitee Is Job One
Let's leave my ass out of this, shall we?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant