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

i have multiple files containing data as name value pairs which are both numbers.

the data is as follows: name|rollno some1|1 some2|2 some3|3

the problem is i am getting repeated set of values from different text files like name:rollno some1:1 some2:2 some3:3 name:rollno some1:1 some2:2 some3:3 Since ssome1 some2 some3 is common is in everytext file iam making it a column name.I want to read a block of values(1,2,3) from each file and only read the column names once. the code i have tried so far is listed below which splits the data into

my @files=<*.text>;#scans for .txt extensn in the curent dir local @_=@files; foreach my $file(<>) { chomp($file); ($name[$i],$value[$i])=split(",",$file); $i++; }

Replies are listed 'Best First'.
Re: reading textfiles into 1 whole file
by jwkrahn (Abbot) on Jul 13, 2010 at 01:00 UTC
    my @files=<*.text>;#scans for .txt extensn in the curent dir local @_=@files; foreach my $file(<>)

    The @_ array has no relation to the special <> readline operator so your program is reading all lines from all files listed in the @ARGV array, or from STDIN if @ARGV is empty and storing them in a list in memory and then iterating over them from that list and the files listed in @files are ignored.

    If you want to use <> you have to store your file names in @ARGV and you should probably also use a while loop instead of a foreach loop:

    local @ARGV = <*.text>;#scans for .txt extensn in the curent dir while ( my $line = <> )
      tx for the help and helping me rectifying my mistakes!!:P
Re: reading textfiles into 1 whole file
by graff (Chancellor) on Jul 13, 2010 at 08:23 UTC
    You didn't say what you intend to do with your data after you read the files. What sort of output do you want to produce? Why is it a "problem" to have "repeated set of values from different text files"?

    You should put <code> tags around your sample data as well as around your code, to make it easier for us to understand your data.

      I need to use the data to plot graphs, the value of the column names should not be repeated and the values corresponding to each column should be listed one below the other where as in my case i am reading multiple files one after the other which also capture the header from each text files in between the data.Also i want to read each file separately so that i can associated a timestamps with it and its respective data.Thanks for the help and sorry for the code formatting i am new to Perl monks:P