Click to See Complete Forum and Search --> : perl array reference quest/prob


corrumpu
01-18-2001, 02:37 AM
trying to pass in a search string for matching purposes and the references
to two arrays that will be used to store the results.. and + the array i want to search
ok...maybe this is crazy looking and there is a one line solution. new to perl, coming from a c background. any help is greatly appreciated. my main problem is that the arrays are are filled with nothing as if my assignment statements using push() do not work right.. thanx in advance

email or post
please no RTFM or general flaming cuz my code sux. i've alot of books/man pages, and i'm not a coding guru.
#i call it like this
&split($ext,\@ext_acc,\@ext_blo,@ipf_log);

#this is how i'm trying to use it
sub split {
my ($pattern,$temp1,$temp2) = shift(@_);
my $found;
print (@_);
foreach $_ (@_) {
if ($found =~ /$pattern/) {
push(@{$temp1},$_);
print "found $pattern\n";
}
else {
push(@{$temp2},$_);
print "didn't find $pattern\n";
}
}
for (my $j = 0; $j < 10; $j++) {
print $temp1->[$j];
}
}


EDIT: i can't get the indentions to take.. sorry.. my code isn't always LEFT justified : (

[This message has been edited by corrumpu (edited 18 January 2001).]

jemfinch
01-18-2001, 04:40 AM
I can't really help you with your code (one of the reasons I love python is that I never have to worry about "references" or "pointers") but if you put your code in [ code ] brackets, it'll indent properly.

Jeremy

YaRness
01-18-2001, 09:11 AM
i'd prolly try it like this. i do refs like @$ref or $$ref[0]. you can use the -> if you like, i'm just not as familiar with it yet. also, i noticed a bug.. you aren't assigning anything to $found

here's your call:

&split($ext,\@ext_acc,\@ext_blo,@ipf_log);


sub split {
#get all the parameters at once
my ($pattern,$temp1ref,$temp2ref, @temparray) = @_;
#
#umm, shouldn't you be assigning something to
#this variable somewhere (see next comment as
#well?
my $found;
#
print (@temparray);
#
#this assigns, in turn, each element to $_
foreach (@temparray) {
#
#maybe you mean "if (/$pattern/)", so it
#will compare each element of @temparray
#(also the same as "if ($_ =~ /$pattern/)"
if ($found =~ /$pattern/) {
push(@$temp1ref,$_);
print "found $pattern\n";
}
else {
push(@$temp2ref,$_);
print "didn't find $pattern\n";
}
}
for (my $j = 0; $j < 10; $j++) {
print $$temp1ref[$j];
}
}


------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

kel
01-18-2001, 11:06 AM
Part of your problem is that when you define $found, you don't assign it a value. That would explain why your first array is getting populated. $found isn't matching anything.

On the other hand, the else brand of your conditional statement should populate the second array with everything. I don't see why that's not happening. I leave off the brackets when dereferencing, but that shouldn't make a difference. Try running your script with warnings turned on.
$ perl -w script.pl

Or turn them on in the very first line of your script:

#! /usr/bin/perl -w

# code
# code
# code


That might clue you in as to an error that's not fatal but that's screwing up the whole script.

corrumpu
01-18-2001, 11:52 AM
yep.. it was the line
if ($found =~ /$pattern/) {
do some stuff;
}


i thought that $found was a place to hold a 0/1 for true or false that a match was found.
one of those mistakes you just can't always see on your own. thanx for all the help. MUCH
appreciated.

::chris::