Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

Coding:

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

Comment on Perl: how to insert words at the end of each line and open the file?
Select or Download Code
Re: Perl: how to insert words at the end of each line and open the file?
by toolic (Chancellor) 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 (Parson) 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?
Username:
Password:

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
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-09-23 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (219 votes), past polls