Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

writing blank array to file?

by dusk (Friar)
on Jun 18, 2001 at 19:56 UTC ( #89342=perlquestion: print w/replies, xml ) Need Help??

dusk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I have a new quandary, here it is:

.... loop .... @array = ($refnum, $scalar); push @chunk, @array; print "@array\n"; $refnum++; next; } warn "\ndebugging: @chunk\n"; print "delete?: "; chomp (my $number = <STDIN>); delete ($chunk[$number]); open (TO_WRITE, ">$ARGV[0]"); while (<TO_WRITE>) { print TO_WRITE @chunk; }

The basic idea is, the script should open a file, extract certain fragments of data (lets pretend these 'certain fragments' are $scalar), then the user can choose which $scalar to delete from the file; then delete it.
The problem in this script, is that it is (I think) deleting all the elements of the array, and then writing to the file.
Please help :)

Replies are listed 'Best First'.
Re: writing blank array to file?
by wog (Curate) on Jun 18, 2001 at 20:13 UTC

    Others have pointed out some of your other problems.

    That's not what's happening. First of all you're better of using splice then delete for this, or using some sort of foreach loop to not write undef items of the array to the file.

    What you are doing is opening the file $ARGV[0] for writing, clobbering the previous file, forgetting to check the return value (open can and will fail, be sure not to ignore it!), and then you are trying to read from this write only filehandle, and somehow not noticing that this is happening, probably because you don't have warnings on to get a message like: Filehandle  TO_WRITE opened only for output.... Nevertheless, the read attempt returns undef and you never do anything in the while loop.

    You should, among other things, get rid of the while loop and probably use splice, and use -w and strict.

    update (in response to malloc's node): Yes, you do want to clobber the file in this case. The reason I mention that its happening is that it (combined with how the print is never ran) explains why the file is blank.

Re: writing blank array to file?
by dimmesdale (Friar) on Jun 18, 2001 at 20:08 UTC
    There are several problems with this script.
    (1)Delete deletes values from an hash. . . i.e, not an array.
    (2)Your file is opened to write, and yet the while(<TO_WRITE>) line is *reading* from the file. You may wish, however, to open the file for appending, and then seek your way to the top(or open it for reading, store the results in an array, close it, and open it for writing, etc.).

    As to what you think your problem is, You're Wrong. You are opening the file for writing, but while doing so you are truncating it, i.e., "deleting all the elements". Type 'open' in the search box for this site and read the page for some help.

Re: writing blank array to file?
by bikeNomad (Priest) on Jun 18, 2001 at 20:09 UTC
    For one thing, you're adding two items to the array @chunk every time through the loop: $refnum and $scalar. So when you delete a given element, it's really going to be an item much earlier in the array. And delete just makes the value of that slot undef, it doesn't actually remove the item (see splice for that behavior)

    Anyway, why bother adding the numbers to the array if they're sequential? Arrays already have sequential behavior. You can display an array with index numbers (assuming your array members have newlines) using:

    my $num = 1; print map { $num++, " $_" } @myArray;

    And you can delete a given index using:

    foreach my $i (0..$#chunk) { print TO_WRITE $chunk[$i] unless $i == $number; }
Re: writing blank array to file?
by malloc (Pilgrim) on Jun 18, 2001 at 20:10 UTC
    Hmm, it seems to me like it would be simpler to use a hash. In your loop, try:
    $hash{$refnum} = $scalar; $refnum++; next;
    i believe that the problem that you are having now is because you are pushing a reference to an array onto an array. The expression $chunk$number (imagine square brackets there) doesn't really mean anything in the context of how you created @chunk, see?

    well, i hope my ramblings helped somewhat, good luck.
    -malloc

    Update: Wow, these guys are fast. The two above posters explained things much more clearly, and we all got so caught up in the nonsensical array references that we missed your actual problem, which wog picked out nicely below, file clobbering. However, it should be okay to clobber the file in this case, since you are going to be rewriting the whole thing from your data structure, with the deleted entries removed, right?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://89342]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-11-29 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?