[19784] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1979 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 22 09:05:33 2001

Date: Mon, 22 Oct 2001 06:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1003755910-v10-i1979@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Mon, 22 Oct 2001     Volume: 10 Number: 1979

Today's topics:
        array or hash to do this sort <tamm@scotlegal.com>
    Re: array or hash to do this sort <Thomas@Baetzler.de>
    Re: array or hash to do this sort (Garry Williams)
    Re: IO::Socket broken on win <goldbb2@earthlink.net>
    Re: lookingglass.pl <tsee@gmx.net>
    Re: LTRIM & RTRIM ? (Martien Verbruggen)
    Re: LTRIM & RTRIM ? (Martien Verbruggen)
    Re: LTRIM & RTRIM ? <Thomas@Baetzler.de>
    Re: LTRIM & RTRIM ? (Martien Verbruggen)
    Re: perl algorithm (Martien Verbruggen)
    Re: perl algorithm (Rafael Garcia-Suarez)
    Re: perl algorithm <goldbb2@earthlink.net>
    Re: perl algorithm (Martien Verbruggen)
        Separating data from a long string <martin_andersen_90@yahoo.com>
    Re: Separating data from a long string <Thomas@Baetzler.de>
    Re: Separating data from a long string <goldbb2@earthlink.net>
    Re: Separating data from a long string <simon.oliver@umist.ac.uk>
    Re: Separating data from a long string <martin_andersen_90@yahoo.com>
        Sorting a hash <blnukem@hotmail.com>
    Re: Sorting a hash <bernard.el-hagin@lido-tech.net>
    Re: Sorting a hash <blnukem@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 22 Oct 2001 11:49:09 +0100
From: "Tam McLaughlin" <tamm@scotlegal.com>
Subject: array or hash to do this sort
Message-Id: <v8u0r9.9mc.ln@mail.scotlegal.com>

I  do not know much perl, just use it for small simple sysadmin tasks.
I usually have to relearn each time I go to use perl. I wish to sort a
file and getting confused as to how to do this.
The file consists of tablenames and associated disk usage:

table1  1234
table 1 999
table2 234
table2 3333
table2 888
table3 1234
table3 555

I wish to add the values in the second column for each tablename.
I thought  could read 1 line at a time into a 2D array and then
itterate through the array summing the second element

e.g.

while ( $line = <IN> ) {
  @row[$i,$j] = split (/ /,$line);
  $i++;
  $j++;


but when I do:

  print "$row[$i,$j] \n";

I do not get anything


any help on how I should approach this would be appreciated




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

Date: Mon, 22 Oct 2001 13:50:28 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: array or hash to do this sort
Message-Id: <lh18ttkgnlfd4l6gucupq0g719d2t1b2h2@4ax.com>

On Mon, 22 Oct 2001, "Tam McLaughlin" <tamm@scotlegal.com> wrote:

>I  do not know much perl, just use it for small simple sysadmin tasks.
>I usually have to relearn each time I go to use perl.

Then you're not using it often enough :-)


>I wish to sort a file and getting confused as to how to do this.

Do you insist on getting confused, or can we do away withit?

>The file consists of tablenames and associated disk usage:
>
>table1  1234
>table 1 999

should be table1 999, right?

>table2 234

>I wish to add the values in the second column for each tablename.

This is not really a sort.

>any help on how I should approach this would be appreciated

# assuming that file is opened as IN

# hash to hold table info
my %table;

while( defined( my $line = <IN> ) ){
  my( $id, $size ) = split $line;

  $table{$id} += size;
}

# normal output
foreach  my $id ( keys %table ){
  print "table $id: size $table{ $id }\n";
}

# sorted by table id
foreach  my $id ( sort keys %table ){
  print "table $id: size $table{ $id }\n";
}

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Mon, 22 Oct 2001 12:52:26 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: array or hash to do this sort
Message-Id: <slrn9t85k9.os9.garry@zfw.zvolve.net>

On Mon, 22 Oct 2001 11:49:09 +0100, Tam McLaughlin
<tamm@scotlegal.com> wrote:

> I  do not know much perl, just use it for small simple sysadmin tasks.
> I usually have to relearn each time I go to use perl. 


Maybe this time you didn't relearn enough?  :-)  


> I wish to sort a
> file and getting confused as to how to do this.
> The file consists of tablenames and associated disk usage:
> 
> table1  1234
> table 1 999


Are these two the same table?  Can the name of the table have a space
in it?  


> table2 234
> table2 3333
> table2 888
> table3 1234
> table3 555
> 
> I wish to add the values in the second column for each tablename.
> I thought  could read 1 line at a time into a 2D array and then
> itterate through the array summing the second element
> 
> e.g.
> 
> while ( $line = <IN> ) {
>   @row[$i,$j] = split (/ /,$line);


This is an array slice!  Not a two dimentional array.  See the
perldata manual page.  


>   $i++;
>   $j++;
> 
> 
> but when I do:
> 
>   print "$row[$i,$j] \n";
> 
> I do not get anything


Why not use a hash?  

  #!/usr/bin/perl
  use warnings;
  use strict;

  my %tables;
  while (<>) {
    my ($key, $value) = split;
    $tables{ $key } += $value;
  }
  print "$_ => $tables{ $_ }\n" for keys %tables;

  __END__

This assumes that table names do *not* have spaces in them.  

-- 
Garry Williams


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

Date: Mon, 22 Oct 2001 06:25:27 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: IO::Socket broken on win
Message-Id: <3BD3F417.E2D7E87D@earthlink.net>

Fe wrote:
> 
[snip]
> also i am missing a lot of error constants (even ubiquitous
> EWOULDBLOCK)

When you 'use Errno', you don't get this constant?

-- 
Klein bottle for rent - inquire within.


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

Date: Mon, 22 Oct 2001 14:50:29 +0200
From: "Steffen Müller" <tsee@gmx.net>
Subject: Re: lookingglass.pl
Message-Id: <9r14jl$pkv$05$1@news.t-online.com>

"Martien Verbruggen" <mgjv@tradingpost.com.au> schrieb im Newsbeitrag
news:slrn9t6lkf.e62.mgjv@verbruggen.comdyn.com.au...

[snip]

| Maybe you're thinking of a discussion where people stated a preference
| for @array instead of $#array + 1? or where people preferred
|
|     for (my $i = 0; $i < @array; $i++) {}
|
| above
|
|     for (my $i = 0; $i <= $#array; $i++) {}

Just as you mention it: I think that's exactly what I was remembering.

Steffen
--
$_=q;33352987319029872958319011313364356732192639127636833335345138283712
3712336415083973397340602842912;;s;\n;;;print"\n";$o=$_;push@o,substr($o,
$_*4,4)for(0..24);pop@o;for(@o){$i++;print' 'x(26-$i).(chr$_/29-$i)."\n"}





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

Date: Mon, 22 Oct 2001 20:05:09 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: LTRIM & RTRIM ?
Message-Id: <slrn9t7rql.cnu.mgjv@martien.heliotrope.home>

On Mon, 22 Oct 2001 10:00:25 +0200,
	Thomas Bätzler <Thomas@Baetzler.de> wrote:
> On Mon, 22 Oct 2001, "ad2ndhand" <ad2ndhand@yahoo.com> wrote:
>>For eg, if I have string "  table is table  " (notice the double spaces at
>>the front and at the back), how do I trim it to become "table is table"
>>without altering the space existing between the words ? The 's' operator I
>>suppose will affect the spaces in between the words.
> 
> Please don't use Jeopardy! style quoting:
> http://www.geocities.com/nnqweb/nquote.html#Q7
> 
> my $string = "  table is table  ";
> 
> $string =~ s/^\s*(.*?)\s*$/$1/;
> 
> print $string;

The FAQ (section 4, "How do I strip blank space from the beginning/end
of a string?") specifically mentions the exact code you propose, but
only to say that "not only is this unnecessarily slow and destructive,
it also fails with embedded newlines". It continues to say 

\begin[partial]{quote}
                                           It is much faster to
       do this operation in two steps:

           $string =~ s/^\s+//;
           $string =~ s/\s+$//;

       Or more nicely written as:

           for ($string) {
               s/^\s+//;
               s/\s+$//;
           }
\end{quote}

> This will remove leading and trailing whitespace characters like tabs,
> spaces and newlines.

Not unconditionally, and not very fast.

Martien
-- 
                                | Since light travels faster than
Martien Verbruggen              | sound, isn't that why some people
                                | appear bright until you hear them
                                | speak?


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

Date: Mon, 22 Oct 2001 20:06:18 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: LTRIM & RTRIM ?
Message-Id: <slrn9t7rsq.cnu.mgjv@martien.heliotrope.home>

On 22 Oct 2001 02:13:09 -0700,
	Bernd Dulfer <bdulfer@bigmailbox.net> wrote:
>> Anyway I can do left trim and right trim in Perl. The closest thing I can
>> think of is chomp and chop but that only takes care the back part but not
>> the front part.
> 
> Use a substitution with regular expression:
> $var =~ s/^\s+(.*?)\s+$/$1/

Not the best method, and it will not always work.

See my followup to Thomas Bätzl in this same thread, and see the Perl
FAQ section 4.

Martien
-- 
                                | Since light travels faster than
Martien Verbruggen              | sound, isn't that why some people
                                | appear bright until you hear them
                                | speak?


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

Date: Mon, 22 Oct 2001 12:47:26 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: LTRIM & RTRIM ?
Message-Id: <34u7ttsbnbdo689469epb565nperc28pb8@4ax.com>

On Mon, 22 Oct 2001, mgjv@tradingpost.com.au (Martien Verbruggen) wrote:

>See my followup to Thomas Bätzl in this same thread, and see the Perl
>FAQ section 4.

Thank you, Martien Verbrugg!

And please don't chomp() my name :-P

SCNR,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Mon, 22 Oct 2001 22:08:14 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: LTRIM & RTRIM ?
Message-Id: <slrn9t831e.cnu.mgjv@martien.heliotrope.home>

On Mon, 22 Oct 2001 12:47:26 +0200,
	Thomas Bätzler <Thomas@Baetzler.de> wrote:
> On Mon, 22 Oct 2001, mgjv@tradingpost.com.au (Martien Verbruggen) wrote:
> 
>>See my followup to Thomas Bätzl in this same thread, and see the Perl
>>FAQ section 4.
> 
> Thank you, Martien Verbrugg!
> 
> And please don't chomp() my name :-P

Whoops. Sorry, Slip of the cut'n'paste.

Marti
-- 
                                | 
Martien Verbrugg                | Can't say that it is, 'cause it
                                | ain't.
                                | 


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

Date: Mon, 22 Oct 2001 19:56:32 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: perl algorithm
Message-Id: <slrn9t7raf.cnu.mgjv@martien.heliotrope.home>

On 22 Oct 2001 07:59:29 GMT,
	Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
> Benjamin Goldberg wrote in comp.lang.perl.misc:
>> 
>> #!/usr/local/bin/perl -wMstrict
> 
> What's this new idiom ?

Not new. Just unusual to see someone include the strict pragma this way
in a more-than-one liner :)

$ man perlrun
[SNIP]

            -Mmodule executes "use" module ";" before executing
            your program.  You can use quotes to add extra code
            after the module name, e.g., "'-Mmodule qw(foo
            bar)'".

[SNIP of more on -M]

Martien
-- 
                                | 
Martien Verbruggen              | I'm just very selective about what I
                                | accept as reality - Calvin
                                | 


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

Date: 22 Oct 2001 10:08:57 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: perl algorithm
Message-Id: <slrn9t7s21.lcv.rgarciasuarez@rafael.kazibao.net>

Martien Verbruggen wrote in comp.lang.perl.misc:
} On 22 Oct 2001 07:59:29 GMT,
} 	Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
} > Benjamin Goldberg wrote in comp.lang.perl.misc:
} >> 
} >> #!/usr/local/bin/perl -wMstrict
} > 
} > What's this new idiom ?
} 
} Not new. Just unusual to see someone include the strict pragma this way
} in a more-than-one liner :)
} 
} $ man perlrun
} [SNIP]
} 
}             -Mmodule executes "use" module ";" before executing
}             your program.  You can use quotes to add extra code
}             after the module name, e.g., "'-Mmodule qw(foo
}             bar)'".
} 
} [SNIP of more on -M]

But not on the shebang line : quoting perldiag :

    Too late for ""-%s"" option
       (X) The #! line (or local equivalent) in a Perl script
       contains the -M or -m option.  This is an error
       because -M and -m options are not intended for use
       inside scripts.  Use the "use" pragma instead.

This idiom is disallowed, but I don't really see why.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
But what about a bit of artistic license?
    -- Monty Python, The Penultimate Supper


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

Date: Mon, 22 Oct 2001 06:16:48 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: perl algorithm
Message-Id: <3BD3F210.470D848D@earthlink.net>

Rafael Garcia-Suarez wrote:
> 
> Benjamin Goldberg wrote in comp.lang.perl.misc:
> >
> > #!/usr/local/bin/perl -wMstrict
> 
> What's this new idiom ?

It's kinda like perl -w -Mstrict, but shorter.

-- 
Klein bottle for rent - inquire within.


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

Date: Mon, 22 Oct 2001 20:36:57 +1000
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: perl algorithm
Message-Id: <slrn9t7tm9.cnu.mgjv@martien.heliotrope.home>

On 22 Oct 2001 10:08:57 GMT,
	Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
> Martien Verbruggen wrote in comp.lang.perl.misc:
> } On 22 Oct 2001 07:59:29 GMT,
> } 	Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
> } > Benjamin Goldberg wrote in comp.lang.perl.misc:
> } >> 
> } >> #!/usr/local/bin/perl -wMstrict
> } > 
> } > What's this new idiom ?
> } 
> } Not new. Just unusual to see someone include the strict pragma this way
> } in a more-than-one liner :)
> } 
> } $ man perlrun
> } [SNIP]
> } 
> }             -Mmodule executes "use" module ";" before executing
> }             your program.  You can use quotes to add extra code
> }             after the module name, e.g., "'-Mmodule qw(foo
> }             bar)'".
> } 
> } [SNIP of more on -M]
> 
> But not on the shebang line : quoting perldiag :
> 
>     Too late for ""-%s"" option
>        (X) The #! line (or local equivalent) in a Perl script
>        contains the -M or -m option.  This is an error
>        because -M and -m options are not intended for use
>        inside scripts.  Use the "use" pragma instead.

Oy. 

Interesting. I hadn't ever tried that one before. I've only ever seen
the 'too late for.." error for -T.

> This idiom is disallowed, but I don't really see why.

I don't see why either. -Mmodule could be made exactly equivalent to 

use Module;

as the first line of the program. Odd. Very odd.

Martien
-- 
                                | 
Martien Verbruggen              | That's not a lie, it's a
                                | terminological inexactitude.
                                | 


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

Date: Mon, 22 Oct 2001 12:08:21 +0200
From: "Martin" <martin_andersen_90@yahoo.com>
Subject: Separating data from a long string
Message-Id: <9r0pvv$guk$1@newstoo.ericsson.se>

Hi

I am a new beginer in Perl, i have a long string with many data in it, i
like to separate and save each of these data in to  my variables.

The long string is $msg  and it looks like:

$msg   = "Seq.No.=100, Id=20, Name=Advanced Perl, Desc=Student Book, Aval=
Not Avalable, Ack=Very Good Book, Authors=Steve, Publisher=Printice Hall";

And I want to save these data in my variables :

 my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher);



 Thank you very much Martin





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

Date: Mon, 22 Oct 2001 13:06:07 +0200
From: =?ISO-8859-1?Q?Thomas_B=E4tzler?= <Thomas@Baetzler.de>
Subject: Re: Separating data from a long string
Message-Id: <peu7tt84s9sd1s0adgcs28oo60nbiigpso@4ax.com>

On Mon, 22 Oct 2001, "Martin" <martin_andersen_90@yahoo.com> wrote:

>$msg   = "Seq.No.=100, Id=20, Name=Advanced Perl, Desc=Student Book, Aval=
>Not Avalable, Ack=Very Good Book, Authors=Steve, Publisher=Printice Hall";
>
>And I want to save these data in my variables :
>
> my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher);

my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher) =
  map { m/=(.*)/; $1 } split /,\s*/, $msg;

Depending on what you're trying to do, it may be worthwhile to store
this info in a hash instead of an array:

# reformat $msg so that Seq.No. becomes Seq
$msg =~ s/Seq\.No\./Seq/;

my %book = map { split /=/ } split /,\s*/, $msg;

# access book name:
print $book{Name};

You could file multiple books in another hash by using the (unique?) Seq
as the key value:

my %books; # hash of hashes that represent books

# file away a book
$books{ $book{Seq} } = \%book;

# title of book 100:
print $books{100}->{Name};

HTH,
-- 
use strict;my($i,$t,@r)=(0,'5 -.@BHJPT4acd6e2hk2lmn2o4r2s3tuz',map{ord}
split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while
$_--;$i++}@r;print"$t\n";# Thomas@Baetzler.de - http://baetzler.de/perl


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

Date: Mon, 22 Oct 2001 07:48:42 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Separating data from a long string
Message-Id: <3BD4079A.4864F2A5@earthlink.net>

Martin wrote:
> 
> Hi
> 
> I am a new beginer in Perl, i have a long string with many data in it,
> i like to separate and save each of these data in to  my variables.
> 
> The long string is $msg  and it looks like:
> 
> $msg   = "Seq.No.=100, Id=20, Name=Advanced Perl, Desc=Student Book,
> Aval=Not Avalable, Ack=Very Good Book, Authors=Steve,
> Publisher=Printice Hall";
> 
> And I want to save these data in my variables :
> 
>  my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher);

my %bookdesc = map { split /=/, $_, 2 } split /,\s*/, $msg;
my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher) =
@bookdesc{"Seq.No.",qw(Id Name Desc Aval Ack Authors Publisher)};

Of course, you could get rid of all the seperate variables, and simply
use %bookdesc -- so $Seq would be $bookdesc{"Seq.No."}, $Id would be
$bookdesc{Id}, $Name would be $bookdesc{Name}, etc.


-- 
Klein bottle for rent - inquire within.


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

Date: Mon, 22 Oct 2001 13:15:21 +0100
From: Simon Oliver <simon.oliver@umist.ac.uk>
To: Martin <martin_andersen_90@yahoo.com>
Subject: Re: Separating data from a long string
Message-Id: <3BD40DD9.94B74B10@umist.ac.uk>

There are probably lots of ways but here is one

my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher) = 
	map /[^=]*=(.*)/, split(/,\s*/, $msg);

Since you say you are a beginer I'll explain a little:

  split(/,\s*/, $msg)
this splits (returns an array) the contents of $msg on commas with
optionall trailing white space

  map expression, list
does work on a list, returning the results as a new list

/[^=]*=(.*)/
this regular expresion remembers () what came after the first equals
sign

--
  Simon Oliver 


> I am a new beginer in Perl, i have a long string with many data in it, i
> like to separate and save each of these data in to  my variables.
> 
> The long string is $msg  and it looks like:
> 
> $msg   = "Seq.No.=100, Id=20, Name=Advanced Perl, Desc=Student Book, Aval=
> Not Avalable, Ack=Very Good Book, Authors=Steve, Publisher=Printice Hall";
> 
> And I want to save these data in my variables :
> 
>  my ($Seq, $Id, $Name, $Desc, $Aval, $Ack, $Authors, $Publisher);


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

Date: Mon, 22 Oct 2001 14:43:28 +0200
From: "Martin" <martin_andersen_90@yahoo.com>
Subject: Re: Separating data from a long string
Message-Id: <9r132p$eai$1@newstoo.ericsson.se>

Thanks evry body, it is working perfect now


/Martin




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

Date: Mon, 22 Oct 2001 11:11:00 GMT
From: "Blnukem" <blnukem@hotmail.com>
Subject: Sorting a hash
Message-Id: <89TA7.25247$wS2.3454130@news02.optonline.net>

Hi group

I'm pushing an array into a hash and I want to sort it numerically my code
works but it is sorting it this order. "1,10,11,12,13,14,15,2,3,4,5,6,7,8,9"
What I want is: "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" How can I do this?

My code:

%TEST= ();

foreach (@TEST){
($questnumber,$questionanswer) = split(/\|/);

$TEST {$questnumber} = "$questnumber|$questionanswer";

}

foreach $key (sort keys %TEST) {
push ( @ANSWER, $TEST{$key});
}
print @ANSWER;

Thanx in advance Blnukem




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

Date: 22 Oct 2001 11:36:31 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: Sorting a hash
Message-Id: <slrn9t80ob.ec9.bernard.el-hagin@gdndev25.lido-tech>

On Mon, 22 Oct 2001 11:11:00 GMT, Blnukem <blnukem@hotmail.com> wrote:
> Hi group
> 
> I'm pushing an array into a hash and I want to sort it numerically my code
> works but it is sorting it this order. "1,10,11,12,13,14,15,2,3,4,5,6,7,8,9"
> What I want is: "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" How can I do this?
> 
> My code:
> 
> %TEST= ();
> 
> foreach (@TEST){
> ($questnumber,$questionanswer) = split(/\|/);
> 
> $TEST {$questnumber} = "$questnumber|$questionanswer";
> 
> }
> 
> foreach $key (sort keys %TEST) {


[snipped body of loop]


The problem is that you're sorting "asciibetically" not numerically.
To do a numerical sort try:


foreach $key ( sort{ $a <=> $b } keys %TEST ){}


Cheers,
Bernard


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

Date: Mon, 22 Oct 2001 12:00:43 GMT
From: "Blnukem" <blnukem@hotmail.com>
Subject: Re: Sorting a hash
Message-Id: <LTTA7.25259$wS2.3497913@news02.optonline.net>

Thanks Bernard it works perfect!




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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1979
***************************************


home help back first fref pref prev next nref lref last post