CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
moving to a certain line in perl

 



regex2012
User

Sep 29, 2017, 8:24 AM

Post #1 of 5 (1535 views)
moving to a certain line in perl Can't Post

I have part of a script that has these lines


Code
open my $fh6, '>>', "/tmp/$strHostname/completed.txt" or die "unable to open file 'file' for writing: $!"; 
my @fields = split ' ', $cmd, $/;
if ($strHostname = "powerbox") {
go to the line that has s/\/$/\/,/g for @fields;
} else {
go to the line that has s/\/$/,/g for @fields;
}

The "go to the line" part in the script is not actually part of the script. I am trying to show that I need to go to one line under one condition, and one line under another condition.
What makes this hard is that if the script goes to the next line, it has to go through the entire body of the script until the end.
After the regex s/\/$/\/,/g for @fields; or s/\/$/,/g for @fields the script has 100 lines til the end. How do I tell it to use the regex under one condition and then run through the rest of the script's lines and to use the other regex under the other condition and then run through the rest of the lines?

Could next be used for this? I'm not very savvy in this area can someone help?


ogit2
Novice

Sep 29, 2017, 9:14 AM

Post #2 of 5 (1533 views)
Re: [regex2012] moving to a certain line in perl [In reply to] Can't Post

Hi

I've made quite a few assumptions as your code snippet has a few errors and is difficult to understand. My code is not the most elegant (I am also a beginner) but hopefully easy to follow.

I am make an assumption that $cmd is a line in your script which is a command followed by parameters.

Hope this helps.


Code
 
#START : Read in script file to memory, ok if small files
open (gdata, "script.txt");
my $data = join ('',<gdata>);
close (gdata);
#END : Read in script file to memory, ok if small files

#START: Initialise variables - some people don't like this
my $track = 0;
my $strHostname = "";
#END : Initialise variables - some people don't like this

#START: Split data into separate lines and store
my @lines=split(/\n/,$data);
#END : Split data into separate lines and store

#START: Go through each line of script
foreach my $line(@lines)
{
#START: Get hostname or command?
$strHostname = $line;
$strHostname =~s/ .*//g;
#END : Get hostname or command?

#START: Process lines of script depending on status
if ($track == 0)
{
$track = ProcessLine($strHostname);
}
elseif ($track == 1)
{
if ( $strHostname =~ m/ \/$/ ) # match space then forward slash then end of line
{
$track = 0;
#$track = ProcessLine($strHostname); # remove hash at start if you want to process the current line which has been matched in the same way
}
}
elseif ($track == 2)
{
if ( $strHostname =~ m/\/$/ ) # match forward slash then end of line
{
$track = 0
#$track = ProcessLine($strHostname); # remove hash at start if you want to process the current line which has been matched in the same way
}
}
#END : Process lines of script depending on status
}
#END : Go through each line of script

# START: Procedure to simply toggle status
sub ProcessLine
{
my ($sHostName) = @_;
my $trck = 0;
if ( index($sHostName,"powerbox") > -1 )
{
$trck = 1;
}
else
{
$trck = 2;
}
return $trck;
}
# END : Procedure to simply toggle status



BillKSmith
Veteran

Sep 29, 2017, 1:42 PM

Post #3 of 5 (1527 views)
Re: [regex2012] moving to a certain line in perl [In reply to] Can't Post

You have not provided enough information for us to understand your problem. User ogit2 has provided a solution based on several assumptions about what you mean. The pseudo-code below represents my best guess. It is written in valid perl (The ... represent unimplemented perl code. It compiles without error, but dies if you try to execute it.).

Code
use strict; 
use warnings;
my $strHostname;
my $cmd;
...; # Whatever goes before posted snipit.
open my $fh6, '>>', "/tmp/$strHostname/completed.txt"
or die "unable to open file 'file' for writing: $!";
my @fields = split /\s/, $cmd, $/; # Note: First Argument of split is regex
if ($strHostname eq "powerbox") { # Note: string compare (eq)
s/\/$/\/,/ for @fields; # Note: no /g
}
else {
s/\/$/,/ for @fields; # Note: not /g
...; # code unique to 'next' case
}
my @temp = @fields;
...; # Code common to both (final 100 lines???)
# I assume that this section uses @fields
# (with the corrected endings).

exit(0);


I have made several small, but important, changes to your code that have nothing to do with your original problem:
  • The operator in your if statement must be a string compare, not an assignment.

  • The first argument to split should be a regex, not a string

  • The statements you describe as regex are far more than than. They are substitution statements which edit the final character of every field. Your /g does not do anything because there can only be one final character in a field.

  • I added the variable @temp merely to demonstrate that @fields is in scope.

  • Good Luck,
    Bill


    regex2012
    User

    Oct 10, 2017, 12:27 PM

    Post #4 of 5 (1443 views)
    Re: [regex2012] moving to a certain line in perl [In reply to] Can't Post

    All, it worked great!


    Laurent_R
    Veteran / Moderator

    Oct 10, 2017, 11:16 PM

    Post #5 of 5 (1438 views)
    Re: [ogit2] moving to a certain line in perl [In reply to] Can't Post

    Hi ogit2,

    Congratulations for willing to help others even though, as you say yourself, you're a beginner. IMHO, this is a very good mind set and I am sure that with that spirit you'll become quickly a seasoned Perl programmer.

    Just one quick note on your code:

    In Reply To

    Quote
    my $data = join ('',<gdata>);
    close (gdata);
    #END : Read in script file to memory, ok if small files

    # ...

    #START: Split data into separate lines and store
    my @lines=split(/\n/,$data);



    In this code, you first store the content of the file into a scalar variable and then split this content into an array. You're making two copies of the content in memory, which is not very efficient (both from the standpoint of memory usage and time spent), especially if the file content is large. You can store the content directly into an array:


    Quote
    my @lines = <gdata>;


    HTH.


    (This post was edited by Laurent_R on Oct 10, 2017, 11:18 PM)

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives