Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Writing to file

    Hi all
    I have a little bit of code you may be able to help with. I load a file
    into an array with

    foreach $lines(@database_array) {
    @edit_array = split(/\:/,$lines);
    push(@member_array,[@edit_array]);
    $x++;
    } #loop

    and then play around with the array a bit, then I would like to load it
    back into the file with

    while($count<$x){
    $newline = join("\:",$member_array[$count],[@edit_array]);
    chomp($newline);
    print DAT "$newline\n";
    $count++;
    }

    but I can not retrieve the data into $newline with join. Is this bit wrong

    $member_array[$count],[@edit_array])

    or am I missing the point completely or is there another way of doing it.
    I am currently loading direct from the file working on that line and them
    loading back to file, but I can only change one line at a time. Doing it
    with as above I can alter the entire array and then load back to file.
    Any help is appreciated and please excuse if I'm in the wrong forum.
    Cheers
    Colin
    -----------------------------------------------------------------------------------------------

    [url]www.rentmyplace.co.nz[/url]
    The ultimate in that holiday spot away from the maddening crowd
    Join as a member today its FREE
    List your holiday accommodation for FREE
    ------------------------------------------------------------------------------------------------


    Outgoing mail is certified Virus Free.
    Checked by AVG Anti-Virus ([url]http://www.grisoft.com[/url]).
    Version: 7.0.211 / Virus Database: 261.8.1 - Release Date: 30/01/2004

    Support Guest

  2. Similar Questions and Discussions

    1. Writing a text file to the file system
      Using Visual Studio C# When I ran the following code: System.IO; private void Button1_Click(object sender, System.EventArgs e) {...
    2. Writing in to a xml file
      Can i write a text content in the swf to a xml file. Can i use the same xml file for authentifications
    3. Setting the file permissions of a file I'm writing to
      Is it possible to specify the permissions of a file I create when I: open ("FOO", "> ./bar") or die ("Could not create file"); Thanks in...
    4. File Access error - writing to .txt file
      Normally web sites run under the ASPNET user account. It appears that this account does not have write privileges to the file path you've...
    5. A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY?
      Hi. This is an error that comes up fairly regularly when trying to run the "Rebuild All" command in a Solution that contains more than one...
  3. #2

    Default RE: Writing to file

    > but I can not retrieve the data into $newline with join. Is this bit wrong

    Yes, it is a little off.
    > $member_array[$count]
    This *isn't* an array, it is an *array reference*
    > [@edit_array]
    Same with this.

    Join takes a list of scalars, not a list of references. So you need to
    dereference them. I think this is what you are after.

    join("\:",@{$member_array[$count]});

    Rob


    -----Original Message-----
    From: Support [mailto:support@emlgroup.co.nz]
    Sent: Sunday, February 01, 2004 2:31 PM
    To: [email]beginners@perl.org[/email]
    Subject: Writing to file

    Hi all
    I have a little bit of code you may be able to help with. I load a file into
    an array with

    foreach $lines(@database_array) {
    ****************@edit_array = split(/\:/,$lines);
    ****************push(@member_array,[@edit_array]);
    ****************$x++;
    ****************}** #loop

    and then play around with the array a bit, then I would like to load it back
    into the file with

    while($count<$x){*******
    $newline = join("\:",$member_array[$count],[@edit_array]);
    chomp($newline);
    print DAT "$newline\n";
    $count++;
    }

    but I can not retrieve the data into $newline with join. Is this bit wrong

    $member_array[$count],[@edit_array])

    or am I missing the point completely or is there another way of doing it.
    I am currently loading direct from the file working on that line and them
    loading back to file, but I can only change one line at a time. Doing it
    with as above I can alter the entire array and then load back to file.
    Any help is appreciated and please excuse if I'm in the wrong forum.
    Cheers
    Colin
    ----------------------------------------------------------------------------
    -------------------

    [url]www.rentmyplace.co.nz[/url]
    The ultimate in that holiday spot away from the maddening crowd
    Join as a member today its FREE
    List your holiday accommodation for FREE
    ----------------------------------------------------------------------------
    --------------------
    Rob Hanson Guest

  4. #3

    Default Re: Writing to file

    On Feb 2, Support said:
    >foreach $lines(@database_array) {
    > @edit_array = split(/\:/,$lines);
    > push(@member_array,[@edit_array]);
    > $x++;
    >} #loop
    First, it doesn't look like you're using 'strict', which means you're not
    declaring your variables. This is probably going to lead to trouble down
    the line.

    Second, colons don't need backslashing in regexes.

    Third, why are you using a counter variable, $x? I don't think you're
    going to need it (even though you use $count and $x later).

    Fourth, WHY do your array names end in "_array"? I think that's a waste
    of space and typing.

    I'd write your code as:

    for my $line (@database) {
    my @edit = split /:/, $line;
    push @members, [@edit];
    }

    But I'd probably use \@edit instead of [@edit]. This won't work in YOUR
    code, because in your code, @edit_array isn't lexically scoped, so every
    entry in @member_array would be the same array reference.

    for my $line (@database) {
    my @edit = split /:/, $line;
    push @members, \@edit;
    }

    But I'd go even further, by using the default variable for the loop and
    for split():

    for (@database) {
    my @edit = split /:/;
    push @members, \@edit;
    }

    But I don't even NEED @edit:

    for (@database) {
    push @members, [ split /:/ ];
    }

    And by this point, I'd probably just use map() which is like a for loop
    with a return value:

    my @members = map [ split /:/ ], @database;
    >while($count<$x){
    > $newline = join("\:",$member_array[$count],[@edit_array]);
    I don't know WHAT'S going on here.
    > chomp($newline);
    > print DAT "$newline\n";
    > $count++;
    >}
    I think your join() line should be

    $line = join ":", @{ $members[$count] };

    Since $members[$count] is an array reference, we need to DE-reference it
    (with the @{ ... } syntax) to get the array out of it. Also, colons don't
    need to be backslashed in strings.

    But again, I'd do things differently. First, instead of a while loop with
    a counter, I'd use another for loop.

    for (@members) {
    my $line = join ":", @$_;
    chomp $line;
    print DAT "$line\n";
    }

    Notice that instead of @{ $members[$x] }, I'm doing @$_. This is because
    $_ is each element of the array in turn (first it's $members[0], then it's
    $members[1], etc.).

    And I'm curious why you're chomp()ing the line... just to add a newline at
    the end. I have a feeling you're creating @database like this:

    open FILE, ...;
    @database = <FILE>;
    close FILE;

    If so, I'll ignore the whole "why are you slurping a file into an array?"
    talk and just tell you that you can chomp the lines there:

    chomp(@database = <FILE>);

    And then you don't need to chomp() them later:

    for (@members) {
    my $line = join ":", @$_;
    print DAT "$line\n";
    }

    I might even do without $line:

    for (@members) {
    print DAT join(":", @$_), "\n";
    }

    Then, since it looks like that, I might use an inverted for loop:

    print DAT join(":", @$_), "\n" for @members;

    I request that, when you get your program working, you submit it to this
    list for a critique.

    --
    Jeff "japhy" Pinyan [email]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
    [ I'm looking for programming work. If you like my work, let me know. ]

    Jeff 'Japhy' Pinyan Guest

  5. #4

    Default Re: Writing to file

    Support wrote:
    >
    > Hi all
    Hello,
    > I have a little bit of code you may be able to help with. I load a
    > file into an array with
    >
    > foreach $lines(@database_array) {
    You should limit the scope of $lines to the foreach loop. Does $lines
    contain multiple lines or just one line? Perhaps you should name it
    $line instead.

    foreach my $line ( @database_array ) {

    > @edit_array = split(/\:/,$lines);
    > push(@member_array,[@edit_array]);
    You are assigning the list returned from split() to the array
    @edit_array and then copying that array to an anonymous array. If you
    lexically scope @edit_array to the foreach loop you can push its
    reference instead of copying it twice:

    my @edit_array = split /\:/, $line;
    push @member_array, \@edit_array;

    Or you could just copy the list directly and avoid using a named array:

    push @member_array, [ split /\:/, $line ];

    > $x++;
    Is $x used anywhere?

    > } #loop
    Or you could declare and assign to @member_array using the map function:

    my @member_array = map [ split /\:/ ], @database_array;

    > and then play around with the array a bit, then I would like to load
    > it back into the file with
    >
    > while($count<$x){
    > $newline = join("\:",$member_array[$count],[@edit_array]);
    > chomp($newline);
    > print DAT "$newline\n";
    > $count++;
    > }
    Perhaps you want something like this:

    foreach my $line ( @member_array ) {
    print DAT join ':', @$line;
    }

    Or with the map function:

    print DAT map join( ':', @$_ ), @member_array;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default Re: Writing to file

    Support wrote:
    > Hi all
    > I have a little bit of code you may be able to help with.
    > I load a file into an array with
    >
    > foreach $lines(@database_array) {
    > @edit_array = split(/\:/,$lines);
    > push(@member_array,\@edit_array);
    > $x++;
    > } #loop
    >
    > and then play around with the array a bit, then I would
    > like to load it back into the file with
    >
    > while($count<$x){
    > $newline = join('\:',@{$member_array[$count]});
    > chomp($newline);
    > print DAT "$newline\n";
    > $count++;
    > }
    See if that is closer to what you want.

    Joseph

    R. Joseph Newton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139