Re: Putting 3 scalars at a time into an array
From: Tad McClellan (tadmc_at_augustmail.com)
Date: 06/06/04
- Next message: Bob Walton: "Re: Putting 3 scalars at a time into an array"
- Previous message: dirtWater: "Re: $NF for perl"
- In reply to: Rich Grise: "Putting 3 scalars at a time into an array"
- Next in thread: Bob Walton: "Re: Putting 3 scalars at a time into an array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Bob Walton: "Re: Putting 3 scalars at a time into an array"
- Previous message: dirtWater: "Re: $NF for perl"
- In reply to: Rich Grise: "Putting 3 scalars at a time into an array"
- Next in thread: Bob Walton: "Re: Putting 3 scalars at a time into an array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|