Re: Putting 3 scalars at a time into an array

From: Tad McClellan (tadmc_at_augustmail.com)
Date: 06/06/04


Date: Sat, 5 Jun 2004 21:02:52 -0500

Rich Grise <null@example.net> wrote:

> I'm going through a file of the format:

[ snip data ]

I don't think we need to see the data, as I don't think you
are asking about how to get the 3 values, but rather how to
put the 3 values somewhere else once gotten?

If so, then where the 3 values come from is not relevant.

> which are kind of name-value pairs where "ItemX" is a name
> and item "ValueX" is a value.

That suggests using a hash data structure.

> So, I've got it going through the file, picking lines
> and building a little array: ($item1value,$item2value,$item3value),

That is a "list" not an "array".

> Now, I can rearrange the printf to put them in any order in
> the output, so I could run stdout thru sort and sort by
> any field, but then I'd have to rearrange that output
> again.

You are right to not like that idea. :-)

You should be able to get whatever you need right out of Perl.

You can do sorting in native Perl you know.

> So, obviously, I need to store these 3 items in an array,
> like @ITEMS[$count] = ($a,$b,$c); and then increment $count,
> is that right?
     ^^^^

Which "that" is that?

Yes, you need to store 3 items in an array.

No, that code is not going to get it done.

> Or would it make more sense to make a hash,

Let me pull out a quote I've enshrined on my hard disk:

   Message-ID: <slrn95u8og.1cr.rgarciasuarez@rafael.kazibao.net>

        I don't know what your original problem is,
        but I suggest to use a hash. --Rafael Garcia-Suarez

> like %ITEMS{"$a"} = ($b,$c); , since in a way, $a is a name
              ^ ^
              ^ ^ a useless use of quotes.

Don't use quotes where you don't need quotes.

> that values $b and $c are associated with.

You need some kind of multi-level data structure, but I can't
make enough sense of your description thus far to suggest one.

> Also, in my
> reading, ISTR something about adding items to an array by
> just adding them, and let perl take care of indexing, or

   perldoc -f push

> was that just hashes? Anyway, I'm reluctant to make a hash,
> because can you then sort it on one of the item values? i.e.

Of course you can.

It is a Frequently Asked Question.

Please do not ask Frequently Asked Questions, just read the answer
first, _then_ ask questions.

   perldoc -q sort

     How do I sort a hash (optionally by value instead of key)?

> sort "%ITEMS{"$a"} = ($b,$c)" on $b?

We can't really talk about it accurately with your psuedo-Perl, so
I'll attempt to translate what I think you mean into Perl:

Load up an HoL:

   $items{$a} = [ $b, $c ]; # this is inside of a loop, many such assignments

later, sort by the 1st field ($b) like in the FAQ answer:

     # untested
     @keys = sort {
                       $items{$a}->[0] cmp $items{$b}[0]
                  } keys %hash; # and by value

> I guess the simple straightforward, non-arcane, way is:
> $count = 0;
> while (<>) {
> # [get the 3 variables]
> @items[$count++] = ($a, $b, $c);
> }

Now that is a _different_ multi-level data structure.

Load up an LoL:

   push @items, [ $a, $b, $c ]; # this is inside of a loop too

and sort by the 2nd field

     # untested
     @keys = sort {
                       $a->[1] cmp $b->[1]
                  } @items; # and by value

> if anyone has any suggestions?

Please check the Perl FAQ *before* posting to the Perl newsgroup.

> Y'know, to make
> it more perl-y?

Learn about multi-level data structures in Perl:

   perldoc perlreftut
   perldoc perlref
   perldoc perllol
   perldoc perldsc

-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


Relevant Pages

  • beginners Digest 28 May 2008 16:42:36 -0000 Issue 3467
    ... First time using Perl & PDF::API2 ... Getting Segmentation fault ... 101000 by: Rajnikant ... I use sort to give the max of an array something like this ...
    (perl.beginners)
  • Re: sorting data - hash vs. list
    ... >> this group, but I won't be posting anymore, ... >> as to follow the guidlines would yeild from me little. ... But I never ask about anything other than perl here. ... and you did not want to read all of it to sort it. ...
    (comp.lang.perl.misc)
  • Re: So what is the point of darwinports then?
    ... Mind you, my use of LaTeX has just taken a downturn. ... I had to edit *all* the bloody quotes and apostrophes, ... No such concept in TeX. ... that I've yet to find a tool to let me do that sort of search+replace ...
    (uk.comp.sys.mac)
  • Re: how do I sort each text block in a file?
    ... the file and sort the chunks with perl, ... I'll leave it to the awk ... does not process input lines without explicit instructions. ...
    (comp.unix.shell)
  • Re: I am so lost... sort and writing a shell script in Perl
    ... > Hello, I just learnt Perl scripting, and I have been trying to do this ... a good approach will be an array ... You may, of course, also use a hash of hashes and only sort ...
    (comp.lang.perl.misc)