create @AoAref from a file

E

Edo

Hello

I am having hard time with these compex data structure although I have
been doing lots of reading, but with out doing it my self I will not
learn it. so after few hours last night and today, I gave up.
I have a file

a A
b B
c C
d D

I need to create array like this
@array = (a [A B C D]);

this is puting out
Can't use string ("a") as an ARRAY ref while "strict refs" in use ...
while (<FH>) {
$. == 1 ?
push @array, (split //)[0] :
push @{ $array[0]}, (split //)[1];
}

thanks
 
B

Ben Morrow

Edo said:
I have a file

a A
b B
c C
d D

I need to create array like this
@array = (a [A B C D]);

....but with commas :).
this is puting out
Can't use string ("a") as an ARRAY ref while "strict refs" in use ...
while (<FH>) {
$. == 1 ?
push @array, (split //)[0] :

If you want the 'A' in your sub-array you want
$. == 1 and push @array, (split //)[0];
rather than ?:.
push @{ $array[0]}, (split //)[1];

This says:
take the first element of @array,
de-ref it as an ARRAY ref,
push something onto it.

The first element of @array is the 'a' you just pushed on, as the
error told you. What you meant was
push @{$array[1]}, (split //)[1];
..

Ben
 
G

Gunnar Hjalmarsson

Edo said:
I am having hard time with these compex data structure although I
have been doing lots of reading, but with out doing it my self I
will not learn it. so after few hours last night and today, I gave
up. I have a file

a A
b B
c C
d D

I need to create array like this
@array = (a [A B C D]);

Do you mean

@array = ('a', ['A', 'B', 'C', 'D']);

??
this is puting out
Can't use string ("a") as an ARRAY ref while "strict refs" in use
...
while (<FH>) {
$. == 1 ?
push @array, (split //)[0] :
push @{ $array[0]}, (split //)[1];
}

1. Provided that my guess about what it is you want to create is
correct (se above), it shall be $array[1] in the last 'push'
statement. Was that so hard to figure out? The error message let you
know which line caused the error, didn't it?

2. The last 'push' statement shall be executed also for the first
line, or did you change your mind?

3. You dont want "split //", but rather just "split". Please re-read
the docs for the split() function.
 
E

Edo

2. The last 'push' statement shall be executed also for the first
line, or did you change your mind?

no, I don't have a strong base in perl, bad habbit being more of a goal
than process orinted.

my $start = 5; #start at line 5 of the file
my $field = 2; #that will be in the arrayref of the array
my $number = 15 # collect 15 lines including the start line
my @seg;

while (<FH>) {
next if $. < $start;
if ( $. == $start ) {
push @seg, (split /,/)[1];
push @{ $seg[1]}, (split /,/)[$field];
} else {
push @{ $seg[1]}, (split /,/)[$field];
}
last if $. == $start + $number -1;
}

I just wanted to use the ?: all in one line, gives me the "maybe-false"
feeling that it will run faster than 6 line substitute.

thanks
 
B

Ben Morrow

Edo said:
my $start = 5; #start at line 5 of the file
my $field = 2; #that will be in the arrayref of the array
my $number = 15 # collect 15 lines including the start line
my @seg;

while (<FH>) {
next if $. < $start;

last if $. == $start + $number;

(unless it matters that this will read one more line than what you had.)
if ( $. == $start ) {
push @seg, (split /,/)[1];
push @{ $seg[1]}, (split /,/)[$field];
} else {
push @{ $seg[1]}, (split /,/)[$field];
}

First rule of programming: never ever write the same thing twice.

$. == start and push @seg, (split /,/)[1];
push @{$seg[1]}, (split /,/)[$field];
last if $. == $start + $number -1;
}

I just wanted to use the ?: all in one line, gives me the "maybe-false"
feeling that it will run faster than 6 line substitute.

Second rule of programming: don't think about whether things will run
faster, benchmark. If it matters, which it usually doesn't.

A ?: construct will run exactly as fast as the equivalent if...else:
the extra lines of code make no difference. What matters is what the
code is doing.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top