FAQ
Hello
I need to read some data in a file and add in another file part of the
line with some changes

input data
./data01;data02;data03;data04;data05;data06;data07;
./data11;data12;data13;data14;data15;data16;data17;
./data21;data22;data23;data24;data25;data26;data27;
./data31;data32;data33;data34;data35;data36;data37;

out put data need
can be like zis

data01;data02;data03;data01_sametexttoall;data04
data11;data12;data03;data11_sametexttoall;data14
data21;data22;data23;data21_sametexttoall;data24
data31;data32;data33;data31_sametexttoall;;data34

out data can be also

./data01;data02;data03;data01_sametexttoall;data04
./data11;data12;data03;data11_sametexttoall;data14
./data21;data22;data23;data21_sametexttoall;data24
./data31;data32;data33;data31_sametexttoall;;data34

I have tryed this but not not working some error "Global symbol "@f"
requires explicit package name" I am using activeperl if I need to add
some pach please tell me what is this because I add all filesys and
same error

my code is this but not working :(

use strict;
use warnings;


# GET a value for accout.txt and outaccount
my $OUTPUTFILE ="acounts.txt";
my $ACC_FILE = "outaccounts.txt";

#open input file
open my $ACC_IN "<", $ACC_FILE
or die "Cannot open '$ACC_FILE' for reading - $!;

#split by line
while (my $line = <ACC_IN>)

{ @F = split(';' $line);
chomp ($line);
#open output file
open $OUTPUTFILE;
#print value what is interested for me in the file and then go to the
next line
print {$OUTPUTFILE}
"$f[0]",'/',"$f[1]",'/',"$f[2]","$f[0]","$f[3]"\n";
#close outfile
close $OUTPUTFILE;
}

Search Discussions

  • Shlomi Fish at Nov 9, 2010 at 10:03 am
    Hi,

    a few comments on your code:
    On Tuesday 09 November 2010 10:52:39 yo RO wrote:
    Hello
    I need to read some data in a file and add in another file part of the
    line with some changes

    input data
    ./data01;data02;data03;data04;data05;data06;data07;
    ./data11;data12;data13;data14;data15;data16;data17;
    ./data21;data22;data23;data24;data25;data26;data27;
    ./data31;data32;data33;data34;data35;data36;data37;

    out put data need
    can be like zis

    data01;data02;data03;data01_sametexttoall;data04
    data11;data12;data03;data11_sametexttoall;data14
    data21;data22;data23;data21_sametexttoall;data24
    data31;data32;data33;data31_sametexttoall;;data34

    out data can be also

    ./data01;data02;data03;data01_sametexttoall;data04
    ./data11;data12;data03;data11_sametexttoall;data14
    ./data21;data22;data23;data21_sametexttoall;data24
    ./data31;data32;data33;data31_sametexttoall;;data34

    I have tryed this but not not working some error "Global symbol "@f"
    requires explicit package name" I am using activeperl if I need to add
    some pach please tell me what is this because I add all filesys and
    same error

    my code is this but not working :(

    use strict;
    use warnings;
    It's good that you have strict and warnings on.
    # GET a value for accout.txt and outaccount
    my $OUTPUTFILE ="acounts.txt";
    my $ACC_FILE = "outaccounts.txt";

    #open input file
    open my $ACC_IN "<", $ACC_FILE
    or die "Cannot open '$ACC_FILE' for reading - $!;
    Nice - but you need a comma after $ACC_IN.

    Also please consider naming at least some of the variables in lowercase.
    Uppercase make it look like you are shouting.
    #split by line
    while (my $line = <ACC_IN>)

    { @F = split(';' $line);
    1. You need to fix you rindentation.

    2. Please declare @F using my (Perl will accept it otherwise because @F is a
    built-in - see http://perldoc.perl.org/perlvar.html ).

    3. this should read:

    my @f = split (/;/, $line);
    chomp ($line);
    Why do you chomp the line *after* you've split it instead of before that.
    #open output file
    open $OUTPUTFILE;
    That does not do the right thing. Please use three-args open with or. Also
    make sure you open it before the loop. so you can use it later.
    #print value what is interested for me in the file and then go to the
    next line
    This comment should read:

    [code]
    # Print the interesting values in the file and then go to the next line.
    [/code]
    print {$OUTPUTFILE}
    "$f[0]",'/',"$f[1]",'/',"$f[2]","$f[0]","$f[3]"\n";
    You don't need to explicitly stringify $f[0], etc. because they are strings.
    And you can use interpolation for clarity. You also cannot print into the
    filename.

    And naturally, Perl distinguishes between @f (in lower-case) and @F (in upper-
    case). Which one do you want?

    Write it as:

    print {$out_fh} "$f[0]/$f[1]/$f[2]$f[0]$f[3]\n";
    #close outfile
    close $OUTPUTFILE;
    The comment here is redundant.

    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/

    <rindolf> She's a hot chick. But she smokes.
    <go|dfish> She can smoke as long as she's smokin'.

    Please reply to list if it's a mailing list post - http://shlom.in/reply .
  • Shlomi Fish at Nov 9, 2010 at 7:38 pm
    Hi Lynx,

    I'm CCing my reply to the list. Next time, please use the GMail "reply to all"
    feature.
    On Tuesday 09 November 2010 15:59:25 Lynx RO wrote:
    Hello Shlomu Fish
    It's "Shlomi" - not "Shlomu".
    thanks for your comment I did changes , about chomp I think that is first
    step is taking by default, about the rest I change @F and $f with @val and
    $val[] but I still have error about some package
    my @val = split (/;/, $line);
    and
    Global symbol "@val" requires explicit package name at test.pl . Can you
    explain me were I am doing some mistake I find in the link about @ here
    http://perldoc.perl.org/perlvar.html#@- but I can not see were is my
    mistake in the script :( can you help me?
    You have not applied all of my commentary. Here are some notes:
    [code]
    use strict;
    use warnings;


    # GET a value for accout.txt and outaccount
    my $OUTPUTFILE ="acounts.txt";
    my $ACC_FILE = "inaccount.txt";
    Be consistent - use «my $OUTPUT_FILE» instead of «my $OUTPUTFILE»
    #open input file
    open my $ACC_IN ,"<", $ACC_FILE
    or die "Cannot open '$ACC_FILE' for reading - $!;

    #split by line
    while (my $line = <ACC_IN>)

    {
    chomp ($line);

    my @val = split (/;/, $line);


    #open output file
    open my $out_fh, ">" , $OUTPUTFILE
    or die "Cannot open '$OUTPUTFILE' for reading - $!;
    1. Here you are opening the file for writing and over-writing it in the
    process for each line in the loop. The opening should be done before the loop
    and the closing afterwards to prevent the file from being over-written each
    time.

    2. You're missing the ending double-quotes («"»).
    #print value what is interested for me in the file and then go to the next
    line
    I gave you a fixed comment as it is not phrased well.
    print {$out_fh} "$val[0]$val[1]$val[2]$val[0]_mytext$val[3]\n";


    #close outfile
    close $out_fh;
    Move this close out of the loop.
    }
    [/code]
    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    What Makes Software Apps High Quality - http://shlom.in/sw-quality

    <rindolf> She's a hot chick. But she smokes.
    <go|dfish> She can smoke as long as she's smokin'.

    Please reply to list if it's a mailing list post - http://shlom.in/reply .
  • Sheppy R at Nov 9, 2010 at 7:51 pm
    The "Global symbol "@val" requires explicit package name at test.pl ."
    should be fixed by declaring @val with 'my':

    my @val = **something**;
    On Tue, Nov 9, 2010 at 2:37 PM, Shlomi Fish wrote:

    Hi Lynx,

    I'm CCing my reply to the list. Next time, please use the GMail "reply to
    all"
    feature.
    On Tuesday 09 November 2010 15:59:25 Lynx RO wrote:
    Hello Shlomu Fish
    It's "Shlomi" - not "Shlomu".
    thanks for your comment I did changes , about chomp I think that is first
    step is taking by default, about the rest I change @F and $f with @val and
    $val[] but I still have error about some package
    my @val = split (/;/, $line);
    and
    Global symbol "@val" requires explicit package name at test.pl . Can you
    explain me were I am doing some mistake I find in the link about @ here
    http://perldoc.perl.org/perlvar.html#@- but I can not see were is my
    mistake in the script :( can you help me?
    You have not applied all of my commentary. Here are some notes:
    [code]
    use strict;
    use warnings;


    # GET a value for accout.txt and outaccount
    my $OUTPUTFILE ="acounts.txt";
    my $ACC_FILE = "inaccount.txt";
    Be consistent - use «my $OUTPUT_FILE» instead of «my $OUTPUTFILE»
    #open input file
    open my $ACC_IN ,"<", $ACC_FILE
    or die "Cannot open '$ACC_FILE' for reading - $!;

    #split by line
    while (my $line = <ACC_IN>)

    {
    chomp ($line);

    my @val = split (/;/, $line);


    #open output file
    open my $out_fh, ">" , $OUTPUTFILE
    or die "Cannot open '$OUTPUTFILE' for reading - $!;
    1. Here you are opening the file for writing and over-writing it in the
    process for each line in the loop. The opening should be done before the
    loop
    and the closing afterwards to prevent the file from being over-written each
    time.

    2. You're missing the ending double-quotes («"»).
    #print value what is interested for me in the file and then go to the next
    line
    I gave you a fixed comment as it is not phrased well.
    print {$out_fh} "$val[0]$val[1]$val[2]$val[0]_mytext$val[3]\n";


    #close outfile
    close $out_fh;
    Move this close out of the loop.
    }
    [/code]
    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    What Makes Software Apps High Quality - http://shlom.in/sw-quality

    <rindolf> She's a hot chick. But she smokes.
    <go|dfish> She can smoke as long as she's smokin'.

    Please reply to list if it's a mailing list post - http://shlom.in/reply .

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

  • Lynx RO at Nov 10, 2010 at 10:35 am
    Hello

    Thanks a lot for your help, comment and sorry for my mistakes that I did not
    see first time that you solved my problems.

    Have a nice day
    On Tue, Nov 9, 2010 at 8:51 PM, Sheppy R wrote:

    The "Global symbol "@val" requires explicit package name at test.pl ."
    should be fixed by declaring @val with 'my':

    my @val = **something**;
    On Tue, Nov 9, 2010 at 2:37 PM, Shlomi Fish wrote:

    Hi Lynx,

    I'm CCing my reply to the list. Next time, please use the GMail "reply to
    all"
    feature.
    On Tuesday 09 November 2010 15:59:25 Lynx RO wrote:
    Hello Shlomu Fish
    It's "Shlomi" - not "Shlomu".
    thanks for your comment I did changes , about chomp I think that is first
    step is taking by default, about the rest I change @F and $f with @val and
    $val[] but I still have error about some package
    my @val = split (/;/, $line);
    and
    Global symbol "@val" requires explicit package name at test.pl . Can you
    explain me were I am doing some mistake I find in the link about @ here
    http://perldoc.perl.org/perlvar.html#@- but I can not see were is my
    mistake in the script :( can you help me?
    You have not applied all of my commentary. Here are some notes:
    [code]
    use strict;
    use warnings;


    # GET a value for accout.txt and outaccount
    my $OUTPUTFILE ="acounts.txt";
    my $ACC_FILE = "inaccount.txt";
    Be consistent - use «my $OUTPUT_FILE» instead of «my $OUTPUTFILE»
    #open input file
    open my $ACC_IN ,"<", $ACC_FILE
    or die "Cannot open '$ACC_FILE' for reading - $!;

    #split by line
    while (my $line = <ACC_IN>)

    {
    chomp ($line);

    my @val = split (/;/, $line);


    #open output file
    open my $out_fh, ">" , $OUTPUTFILE
    or die "Cannot open '$OUTPUTFILE' for reading - $!;
    1. Here you are opening the file for writing and over-writing it in the
    process for each line in the loop. The opening should be done before the
    loop
    and the closing afterwards to prevent the file from being over-written
    each
    time.

    2. You're missing the ending double-quotes («"»).
    #print value what is interested for me in the file and then go to the next
    line
    I gave you a fixed comment as it is not phrased well.
    print {$out_fh} "$val[0]$val[1]$val[2]$val[0]_mytext$val[3]\n";


    #close outfile
    close $out_fh;
    Move this close out of the loop.
    }
    [/code]
    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    What Makes Software Apps High Quality - http://shlom.in/sw-quality

    <rindolf> She's a hot chick. But she smokes.
    <go|dfish> She can smoke as long as she's smokin'.

    Please reply to list if it's a mailing list post - http://shlom.in/reply.

    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 9, '10 at 8:53a
activeNov 10, '10 at 10:35a
posts5
users3
websiteperl.org

3 users in discussion

Lynx RO: 2 posts Shlomi Fish: 2 posts Sheppy R: 1 post

People

Translate

site design / logo © 2022 Grokbase