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

Re: reparse tree in textfile

by repellent (Priest)
on Feb 20, 2010 at 05:11 UTC ( #824317=note: print w/replies, xml ) Need Help??


in reply to reparse tree in textfile

Here's another way, which follows the same technique presented by ikegami in the previous post:
my @path; while (<DATA>) { chomp(); /^((?:\| )*[|`]-- )(.+)/ or next; $#path = length($1)/2 - 3; # truncate path based on tree level push(@path, $2); print $1, "./", join("/" => @path), "\n"; }

Replies are listed 'Best First'.
Re^2: reparse tree in textfile
by ikegami (Patriarch) on Feb 20, 2010 at 06:29 UTC
    Why set the length of the array only to change it immediately? I also hate the needless use of global vars.
    my @path = '.'; while (<DATA>) { chomp; my ($prefix, $node) = /^((?:\| )*[|`]-- )(.+)/ or next; $#path = length($prefix)/2 - 1; $path[-1] = $node; print $prefix, join("/" => @path), "\n"; }
    Alternative, we could use splice.
    my @path = '.'; while (<DATA>) { chomp; my ($prefix, $node) = /^((?:\| )*[|`]-- )(.+)/ or next; splice @path, length($prefix)/2-1, 0+@path, $node; print $prefix, join("/" => @path), "\n"; }

    It avoids the addition of magic to @path, and it combines two operations. The latter usually makes something more readable, but it's unfortunately not the case here.

    Speaking of readability, that regex is rather hard to read. I had something like that initially, but I switched to split because it was much more straightforward.

    my @path = '.'; while (<DATA>) { chomp; my ($prefix, $node) = split(/(?<=-- )/, $_, 2) or next; $#path = length($prefix)/2 - 1; $path[-1] = $node; print $prefix, join("/" => @path), "\n"; }
        Why set the length of the array only to change it immediately?

      It was one way to do it. The goal was to keep @path with only "pure" node information, and not have redundancies like ".", "/", and previous nodes repeated as the indexes go higher.

        I also hate the needless use of global vars.

      Me too. But a data structure outside the loop was needed to oversee things.

        It was one way to do it. The goal was to keep @path with only "pure" node information

        That's unrelated. I was referring to $#a=...; push @a,...;. The first statement sets the size of the array. Then next changes it. Why not just set it to the right size right from the start.

        Me too. But a data structure outside the loop was needed to oversee things.

        eh? What global data structure? I was referring $1 and $2. The only other global is DATA.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://824317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2023-09-30 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?