R
Rich Grise
Admittedly, I don't know the terminology well - suffice it to say,
I'm going through a file of the format:
Item1:<discard>Value1<discardthis>
<discard>
<discardItem2<discard>Value2<discard>
<discard>
<discard>Value3<discard>Item3<discard>
Item1:<discard>Value4<discardthis>
<discard>
<discardItem2<discard>Value5<discard>
<discard>
<discard>Value6<discard>Item3<discard>
which are kind of name-value pairs where "ItemX" is a name
and item "ValueX" is a value.
So, I've got it going through the file, picking lines
and building a little array: ($item1value,$item2value,$item3value),
which it printfs.
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.
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? Or would it make more sense to make a hash,
like %ITEMS{"$a"} = ($b,$c); , since in a way, $a is a name
that values $b and $c are associated with. Also, in my
reading, ISTR something about adding items to an array by
just adding them, and let perl take care of indexing, or
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.
sort "%ITEMS{"$a"} = ($b,$c)" on $b?
I guess the simple straightforward, non-arcane, way is:
$count = 0;
while (<>) {
# [get the 3 variables]
@items[$count++] = ($a, $b, $c);
}
But ISTVR something about #items, but don't know where to
look for it, if anyone has any suggestions? Y'know, to make
it more perl-y?
Thanks!
Rich
I'm going through a file of the format:
Item1:<discard>Value1<discardthis>
<discard>
<discardItem2<discard>Value2<discard>
<discard>
<discard>Value3<discard>Item3<discard>
Item1:<discard>Value4<discardthis>
<discard>
<discardItem2<discard>Value5<discard>
<discard>
<discard>Value6<discard>Item3<discard>
which are kind of name-value pairs where "ItemX" is a name
and item "ValueX" is a value.
So, I've got it going through the file, picking lines
and building a little array: ($item1value,$item2value,$item3value),
which it printfs.
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.
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? Or would it make more sense to make a hash,
like %ITEMS{"$a"} = ($b,$c); , since in a way, $a is a name
that values $b and $c are associated with. Also, in my
reading, ISTR something about adding items to an array by
just adding them, and let perl take care of indexing, or
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.
sort "%ITEMS{"$a"} = ($b,$c)" on $b?
I guess the simple straightforward, non-arcane, way is:
$count = 0;
while (<>) {
# [get the 3 variables]
@items[$count++] = ($a, $b, $c);
}
But ISTVR something about #items, but don't know where to
look for it, if anyone has any suggestions? Y'know, to make
it more perl-y?
Thanks!
Rich