Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Perl: how to insert words at the end of each line and open the file?

by WWq (Novice)
on Oct 31, 2013 at 17:35 UTC ( #1060610=perlquestion: print w/replies, xml ) Need Help??
WWq has asked for the wisdom of the Perl Monks concerning the following question:

I have a file (file A) which contains different libraries shown as below. Each row is a different library (eg. b05_ln_rmmm_0.85v_0.00c_core is a library name). I would like to open each library in .lib format in unix. I have tried the coding below. However It doesn't work as what I want. I failed to insert words (.lib) behind those library names and save new library names in an array. And how to open these library files in perl?

file A:

b05_ln_rmmm_0.85v_0.00c_core /p/ldb/ln/b05_ln_rmmm_0.85v_0.00c_c +ore.ldb:b05_ln_rmmm_0.85v_0.00c_core.ldb b05_nn__rmmm_g9v_098c_core /p/ldb/nn/b05_nn__rmmm_g9v_098c_core. +ldb:b05_nn__rmmm_g9v_098c_core b05_un_rsss_0.66v_0.44c_core /p/ldb/un/b05_un_rsss_0.66v_0.44c_c +ore.ldb:b05_un_rsss_0.66v_0.44c_core


while (@arr2 = <CF>){ foreach $line (@arr2) { if ($line =~ m/(.*) (.*)/) { $libn = "$1"; #$libn =~ s/^\s+//; $libn =~ s/ /${1}.lib/x; print "$libn\n"; push (@arr3, $libn); $string = "vi $libn"; system ($string); } } } }

expected result:

b05_ln_rmmm_0.85v_0.00c_core.lib b05_nn__rmmm_g9v_098c_core.lib b05_un_rsss_0.66v_0.44c_core.lib

Replies are listed 'Best First'.
Re: Perl: how to insert words at the end of each line and open the file?
by toolic (Bishop) on Oct 31, 2013 at 17:53 UTC
    This gives you the output you want:
    use warnings; use strict; while (my $line = <DATA>) { if ($line =~ /(\S+)/) { my $libn = "$1.lib"; print "$libn\n"; } } __DATA__ b05_ln_rmmm_0.85v_0.00c_core /p/ldb/ln/b05_ln_rmmm_0.85v_0.00c_core. +ldb:b05_ln_rmmm_0.85v_0.00c_core.ldb b05_nn__rmmm_g9v_098c_core /p/ldb/nn/b05_nn__rmmm_g9v_098c_core.ldb: +b05_nn__rmmm_g9v_098c_core b05_un_rsss_0.66v_0.44c_core /p/ldb/un/b05_un_rsss_0.66v_0.44c_core. +ldb:b05_un_rsss_0.66v_0.44c_core

      Thank you for guidance. I keep the library names in an array and try to open each of them to do further editing. It seems I cannot open the file directly using file handle. I will be very much appreciate if you shed light on this.Thanks.

      while ($line = <CF>) { @arr2 = @empty_arr; if ($line =~ /(\S+)/) { $libn = "$1.lib"; #print "$libn\n"; push (@arr2,$libn); #print "@arr2\n"; } } foreach $libn (@arr2) { open (VI,"$libn") || die "ERROR: can't read $libn\n"; #print"$libn\n"; if ($line =~ m/(Car code:)((.*))/) { $a = $line; .... .. }
Re: Perl: how to insert words at the end of each line and open the file?
by Laurent_R (Abbot) on Oct 31, 2013 at 18:20 UTC

    In this part of your code:

    while (@arr2 = <CF>){ foreach $line (@arr2) {

    you are doing two nested loops, whereas you really need only one. And the first loop is actually faulty. You either want to do this:

    my @arr2 = <CF>; foreach $line (@arr2) { # ...

    or that:

    while (my $line = <CF>) { # ...

    The second solution is simpler, slightly faster and, in my view, cleaner.

      Yes, thank you for your guidance. =)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1060610]
Approved by davido
Front-paged by Laurent_R
[erix]: job hunting is an annoying situation to be in. Of course, once settled somewhere you're going to want out again...
[erix]: but that doesn't make the searching less anoying
[Tanktalus]: I've been looking for over 12 months :(
[erix]: that's bad... I can only wish you good luck

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-05-23 19:09 GMT
Find Nodes?
    Voting Booth?