Perl - New line in array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - New line in array elements
# 1  
Old 04-02-2008
Perl - New line in array elements

Hello,
I have a comma delimited input feed file. The first field has directory location and the second field has file name.
Ex of input feed:

/export/appl/a,abc*.dat
/export/appl/b,xyz*.dat
/export/appl/c,pmn*.dat


Under each directory, there would be many files like...
.
.
.
abc.200801.dat
abc.200802.dat
abc.200803.dat
abc.200804.dat

I'm writing a perl script, in which part of the logic is to get the stat() of the latest file in the corresponding directory. (in this case I need to get stat() of abc.200804.dat

So, first I'm getting the directory and then chdir to that location and doing a ls -ltr with the wild card file names and storing them in array. Then using $

Code:
foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=$list_files[-1];    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

The problem is, I think when I'm trying to get the last changed file into the array element, its storing the latest file in the last element, but I think its appending new line to the element along with file name. So, the stat($f_nme) isn't working.

Can you please let me know what I'm missing?
Thanks!
# 2  
Old 04-02-2008
Evaluating backticks in list context, Perl does not automatically drop the newlines from the command output for you. You should do that yourself.

Compare the following two cases:

Code:
[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = `ls /usr/local`; print Dumper(\@lines)'
$VAR1 = [
          'Adobe
',
          'apps
',
          'bin
',
          'Brother
',
          'etc
',
          'games
',
          'include
',
          'lib
',
          'libexec
',
          'man
',
          'RealPlayer
',
          'sbin
',
          'share
',
          'src
'
        ];
[bernardchan@bernardchan ~]$ perl -MData::Dumper -e '@lines = map { chomp; $_ } (`ls /usr/local`); print Dumper(\@lines)'
$VAR1 = [
          'Adobe',
          'apps',
          'bin',
          'Brother',
          'etc',
          'games',
          'include',
          'lib',
          'libexec',
          'man',
          'RealPlayer',
          'sbin',
          'share',
          'src'
        ];

As you can see, there are trailing newline with the first case, but not after post-processing each item removing the trailing newlines.
# 3  
Old 04-02-2008
Basically what he said... Use chomp($f_nme[-1]) first, then assign it. Use chomp() above where you get the directory list from your input file too... Is safer. chop() will strip the last char of a string ALWAYS. chomp() strips only if is a LINE ENDING, so is much safer usually if you are not always SURE that the last char of your string is the line ending you want to remove....
# 4  
Old 04-02-2008
Quote:
Originally Posted by quine
Basically what he said... Use chomp($f_nme[-1]) first, then assign it. Use chomp() above where you get the directory list from your input file too... Is safer. chop() will strip the last char of a string ALWAYS. chomp() strips only if is a LINE ENDING, so is much safer usually if you are not always SURE that the last char of your string is the line ending you want to remove....
I should have mentioned it.... I've used both chomp($list_files[-1]) and chop($list_files[-1]). It doesn't seem to work.

Code:
my $f_nme=chomp($list_files[-1]); 
print " last file name is $f_nme \n";

It doesn't assign any value to $f_nme.

And chomp(($list_files[-1]) returns "1" ?!


Code:
foreach my $input_var (@i_raw_data)
{
 chop($input_var);
 my ($d_loc,$wild_f_nme)=split(/\,/,$input_var);
 chdir($d_loc);
 my @list_files = `ls -c $wild_f_nme`;  #store all file names
 my $f_nme=chomp($list_files[-1]);    #it will have latest modified filename

 my ($accesstime, $modtime, $createtime, $fsize) = (stat($f_nme))[8,9,10,7];

Anyways, I have used the way cbhikong suggested and it worked. Thanks!

Last edited by bperl; 04-02-2008 at 11:47 PM..
# 5  
Old 04-03-2008
Yes, you misunderstand chomp() chop(). They actually transform their argument. What they RETURN is a 1 or 0 (I think)...

So $y = chop($x); actually changes $x and assigns 1 (success) to $y. So you don't ASSIGN the results, you just do it....

$x = "abcd";

chop($x);

Now $x is "abc"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

3. Shell Programming and Scripting

Multiplication of array elements

Hi, I can't find out how to create correct code to get multiplication of each elements of array. Let's say I enter array into command line (2 3 4 5 6 8) and i need output 2*3*4*5*6*8=5760. I tried this one, but answer is 0. for i in $@; do mult=$((mult*i))done echo "mult: " $mult ... (4 Replies)
Discussion started by: rimasbimas
4 Replies

4. Shell Programming and Scripting

Array elements comparison using perl

Experts, I am looking to compare elements of 2 array using perl. Below is not the actual code but logic wise something like this. my $version = "MYSQlcl-5.2.4-264.x86_64"; <-- split this word into array as (5 2 4 264) ( which is to extract only the version number from the package name) my... (1 Reply)
Discussion started by: solaix14
1 Replies

5. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

6. Shell Programming and Scripting

Perl Array Elements Replacement

Hello, I have the following perl array: @longname = (Fasthernet0/0 Fasthernet0/1 Serial0/1/0 Serial0/2/1 Tunnel55 Tunnel77) with the followinh array: @shortname = (Fa0/0 Fa0/1 Se0/1/0 Se0/2/1 Tu55 Tu77) in other words, I need to remove the following from each element in the array... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

7. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

8. Shell Programming and Scripting

Perl:Use of array elements in pattern matching

I need to use array elements while pattern matching. @myarr = (ELEM1, ELEM2, ELEM3); following is the statement which I am using in my code. Basically I want to replace the ELEM1/2/3 with other thing which is mentioned as REPL here. if (condition) { s/(ELEM1|ELEM2|ELEM3): REPL: /; } I... (3 Replies)
Discussion started by: deo_kaustubh
3 Replies

9. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

10. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies
Login or Register to Ask a Question