Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Construct a hash from a log file. 2

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
A sample log file somewhat look like this:
Code:
x11:x12:x13:...:x1m:value1
x21:x22:x23:...:x2m:...:x2n:value2
x31:x32:x33:...:x3i:value3
x41:x42:x43:...:x4i:...:x4j:value4

Note that --
1) In each row, the elements are delimited by ':';
2) The number of elements in each row may very, i.e. m, n, i, & j are integers and m != n != i != j.

How to construct a multi-dimension hash whose keys are the elements in rows denoted as xij and values are the last element in each row.

For instance, if a simplified log file was like:
Code:
x11:x12:x13:value1
x21:x22:x23:x24:value2

then the hash would be like:
Code:
my %cache => (
    'x11'=>{
        'x12'=>{
            'x13'=>value1,
        },
    },
    'x21'=>{
        'x22'=>{
            'x23'=>{
                'x24'=>value2,
                },
            },
        },
    },
);

I guess the implementation should use recursive call. But I don't know how to implement it.

Thanks for your kind help.
 
Hi

No need for recursion, a simple loop is enough :
Perl:
[b]use[/b] Data[teal]::[/teal]Dumper[teal];[/teal]

[b]my[/b] [navy]%cache[/navy][teal] = ();[/teal]

[b]while[/b] [teal]([/teal][b]chomp[/b][teal]([/teal][b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<DATA>[/i][/green][teal]))[/teal] [teal]{[/teal]
  [b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b] [green][i]/:/[/i][/green][teal],[/teal] [navy]$line[/navy][teal];[/teal]

  [b]my[/b] [navy]$parent[/navy] [teal]=[/teal] [teal]\[/teal][navy]%cache[/navy][teal];[/teal]
  [b]for[/b] [teal]([/teal][b]my[/b] [navy]$i[/navy] [teal]=[/teal] [purple]0[/purple][teal],[/teal] [navy]$l[/navy] [teal]=[/teal] scalar [navy]@field[/navy][teal];[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$l[/navy] [teal]-[/teal] [purple]2[/purple][teal];[/teal] [navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
    [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal] [teal]=[/teal] [teal]{}[/teal] [b]unless[/b] [b]defined[/b] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
    [navy]$parent[/navy] [teal]=[/teal] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
  [teal]}[/teal]
  [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][-[/teal][purple]2[/purple][teal]][/teal][teal]}[/teal] [teal]=[/teal] [navy]$field[/navy][teal][-[/teal][purple]1[/purple][teal]];[/teal]
[teal]}[/teal]

[b]print[/b] Dumper [teal]\[/teal][navy]%cache[/navy][teal];[/teal]

__DATA__
x11:x12:x13:value1
x21:x22:x23:x24:value2
Code:
$VAR1 = {
          'x11' => {
                     'x12' => {
                                'x13' => 'value1'
                              }
                   },
          'x21' => {
                     'x22' => {
                                'x23' => {
                                           'x24' => 'value2'
                                         }
                              }
                   }
        };

Feherke.
[link feherke.github.com/][/url]
 
Hi Feherke,

Thank you so much for your help!!

But I have some follow-up questions. At first, please take a look at my modified code and input data.
Perl:
#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

my %cache = ();

while (chomp(my $line = <DATA>)) { [b] # This is line 9[/b]
  if($line !~ /^#/) {
    my @field = split /:/, $line;

    my $parent = \%cache;
    for (my $i = 0, my $l = scalar @field; $i < $l - 2; $i++) {
      $parent->{$field[$i]} = {} unless defined $parent->{$field[$i]};
      $parent = $parent->{$field[$i]};
    }
    if($field[-2]) { [b]# To filter NULL strings
      # But would it also filter out 0 - zero's? [/b]
      $parent->{$field[-2]} = $field[-1];
    }
  }
}

print Dumper \%cache;

__DATA__
# the 2nd element is NULL
x11::x13:value1
# the 2nd element is zero
x21:0:x23:x24:value2
x31:x32:value3

The actual output:

Code:
% ./readCache.pl
[COLOR=red]Use of uninitialized value $line in chomp at ./readCache.pl line 9, <DATA> line 5.[/color]
$VAR1 = {
          'x11' => {
                     '' => {
                             'x13' => 'value1'
                           }
                   },
          'x31' => {
                     'x32' => 'value3 '
                   },
          'x21' => {
                     '0' => {
                              'x23' => {
                                         'x24' => 'value2 '
                                       }
                            }
                   }
        };

The output I want:
Code:
$VAR1 = {
          'x11' => {
                     'x13' => 'value1'
                   },
          'x31' => {
                     'x32' => 'value3 '
                   },
          'x21' => {
                     '0' => {
                              'x23' => {
                                         'x24' => 'value2 '
                                       }
                            }
                   }
        };

The output I expected from my modified codes, but not I wanted:
Code:
$VAR1 = {
          'x11' => {
                     'x13' => 'value1'
                   },
          'x31' => {
                     'x32' => 'value3 '
                   },
          'x21' => {
                     'x23' => {
                                'x24' => 'value2 '
                              }
                   }
        };

My questions:
1) Where does that warning come from? How to get rid of it?
2) Why didn't my implementation get rid of --
i) the NULL string, which is what I expected and wanted;
ii) the '0', which is what I expected, but not what I wanted
 
Hi

whn said:
1) Where does that warning come from? How to get rid of it?
That is because :
[ul]
[li]You turned on warnings. ( By the way, unless you want to turn warnings on/off for certain blocks, the [tt]-w[/tt] switch and [tt]use warnings[/tt] pragma are equivalent. )[/li]
[li]Personally I prefer to enjoy Perl's flexibility and ignore warnings. Despite that I try to not force the edges too much when giving advices, there are catchy situations I miss.[/li]
[/ul]

In other words, at the end of input, the last value returned by the [tt]<>[/tt] operation is [tt]undef[/tt]. Using [tt]chomp()[/tt] on a variable with [tt]undef[/tt] value, gives that warning :
Code:
[blue]master #[/blue] perl -e 'chomp $x'

[blue]master #[/blue] perl -we 'chomp $x'
Name "main::x" used only once: possible typo at -e line 1.
Use of uninitialized value $x in scalar chomp at -e line 1.

[blue]master #[/blue] perl -e '$x=undef; chomp $x'

[blue]master #[/blue] perl -we '$x=undef; chomp $x'
Use of uninitialized value $x in scalar chomp at -e line 1.

To get rid of that warning just move the [tt]chomp()[/tt] call from the condition inside the loop :
Code:
[b]while[/b] [teal]([/teal][b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<DATA>[/i][/green][teal])[/teal] [teal]{[/teal]  [gray]# This is line 9[/gray]
  [b]chomp[/b] [navy]$line[/navy][teal];[/teal]

whn said:
2) Why didn't my implementation get rid of --

i) the NULL string, which is what I expected and wanted;
In that DATA line the field indexes are
[pre]
[gray][highlight #fcc]0 [/highlight] [highlight]1 [/highlight] [highlight #cfc]2 [/highlight] [highlight #ccf]3 [/highlight] # from left[/gray]
[highlight #fcc]x11[/highlight] : : [highlight #cfc]x13[/highlight] : [highlight #ccf]value1[/highlight]
[gray][highlight #fcc]-4 [/highlight] [highlight]-3[/highlight] [highlight #cfc]-2 [/highlight] [highlight #ccf]-1 [/highlight] # from right[/gray]
[/pre]

Even more, you tested after the loop, in that moment the key with the empty string was already created.

Better handle it on [tt]split()[/tt] by splitting not on each colon ( : ), but on one or more consecutive colons :
Code:
  [b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b] [green][i]/:[highlight]+[/highlight]/[/i][/green][teal],[/teal] [navy]$line[/navy][teal];[/teal]

whn said:
ii) the '0', which is what I expected, but not what I wanted
Similarly, in that DATA line '0' was at index -4, not the tested one.

One minor thing. I find that instructions which condition the execution of the loop body are better to do [tt]next[/tt] ( or [tt]continue[/tt] in other languages ) then to control a huge block enclosing the entire loop block.

So finally I this is what I have now :
Perl:
[gray]#!/usr/bin/perl -w[/gray]

[b]use[/b] strict[teal];[/teal]
[b]use[/b] warnings[teal];[/teal]
[b]use[/b] Data[teal]::[/teal]Dumper[teal];[/teal]

[b]my[/b] [navy]%cache[/navy] [teal]=[/teal] [teal]();[/teal]

[b]while[/b] [teal]([/teal][b]my[/b] [navy]$line[/navy] [teal]=[/teal] [green][i]<DATA>[/i][/green][teal])[/teal] [teal]{[/teal]  [gray]# This is line 9[/gray]
  [b]chomp[/b] [navy]$line[/navy][teal];[/teal]
  [highlight #ffc][b]next[/b] [b]if[/b] [navy]$line[/navy] [teal]=~[/teal] [green][i]/^#/[/i][/green][teal];[/teal][/highlight]

  [b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b] [green][i]/:+/[/i][/green][teal],[/teal] [navy]$line[/navy][teal];[/teal]

  [b]my[/b] [navy]$parent[/navy] [teal]=[/teal] [teal]\[/teal][navy]%cache[/navy][teal];[/teal]
  [b]for[/b] [teal]([/teal][b]my[/b] [navy]$i[/navy] [teal]=[/teal] [purple]0[/purple][teal],[/teal] [b]my[/b] [navy]$l[/navy] [teal]=[/teal] scalar [navy]@field[/navy][teal];[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$l[/navy] [teal]-[/teal] [purple]2[/purple][teal];[/teal] [navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
    [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal] [teal]=[/teal] [teal]{}[/teal] [b]unless[/b] [b]defined[/b] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
    [navy]$parent[/navy] [teal]=[/teal] [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
  [teal]}[/teal]
  [navy]$parent[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][-[/teal][purple]2[/purple][teal]][/teal][teal]}[/teal] [teal]=[/teal] [navy]$field[/navy][teal][-[/teal][purple]1[/purple][teal]];[/teal]
[teal]}[/teal]

[b]print[/b] Dumper [teal]\[/teal][navy]%cache[/navy][teal];[/teal]

__DATA__
# the 2nd element is NULL
x11::x13:value1
# the 2nd element is zero
x21:0:x23:x24:value2
x31:x32:value3

Feherke.
[link feherke.github.com/][/url]
 
You're the man, Feherke. Many thanks!!
 
At first, my sincere thanks go to Feherke! With his help I have used the implementation suggested by Feherke a few times at work. They are all working very well. But it encountered a problem lately that I cannot understand it at all.

Hence I constructed an example to show the problem that I am having now. Below is my sample code (a bit long, 63 lines):

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $log = '/tmp/log';
my $dlmt = '_';
my $tail = 't';
my @ver = (3, 3.22, 5.15, 5.16, '5.2.33', 5);
[b]@ver = (3.22, 3.3, '4.4.4.4', '3.5.21', 6, 4); [COLOR=#CC0000]# this is line 11[/color].
# comment out this line in 2nd run[/b]
print Dumper(@ver);
&cacheIt(\@ver);
my $info = &readCache();
print Dumper($info);
exit;

sub readCache {
  my %cache = ();
  open(RH, "$log");
  my $lineCnt = 1;
  my $key;
  while(my $line = <RH>) {
    chomp($line);
    if($line =~ /$dlmt/) {
      my @field = split(/$dlmt/, $line);
      my $parent = \%cache;
      my $l = scalar @field;
      for (my $i = 0; $i < $l - 2; $i++) {
        $parent->{$field[$i]} = {} unless exists $parent->{$field[$i]};
        $parent = $parent->{$field[$i]};
      }
      if($#field > 1) {
        $parent->{$field[-2]} = $field[-1]; [b][COLOR=#EF2929]# This is line 35[/color][/b]
      }
      else {
        if(!exists($parent->{$field[-2]})) {
          $parent->{$field[-2]} = $field[-1];
        }
      }
    } # end of if($line =~ /$dlmt/)
    else {
      $key = $line;
    }
    $lineCnt++;
  } # end of while(my $line = <RH>)
  return \%cache;
}

sub cacheIt {
  my $a = $_[0];
  my %h;
  my $hRef = \%h;
  open(WH, ">$log");
  for(my $i = 0; $i <= $#$a; $i++) {
    my @tmp = split(/\./, $a->[$i]);
    my $str = join $dlmt, @tmp;
    $str .= $dlmt.$tail;
    print WH $str."\n";
  }
  close(WH);
}

The output from first run. Line 11 is taking effect.
Everything is expected.
Code:
./test.pl
$VAR1 = '3.22';
$VAR2 = '3.3';
$VAR3 = '4.4.4.4';
$VAR4 = '3.5.21';
$VAR5 = 6;
$VAR6 = 4;
$VAR1 = {
          '6' => 't',
          '4' => {
                   '4' => {
                            '4' => {
                                     '4' => 't'
                                   }
                          }
                 },
          '3' => {
                   '22' => 't',
                   '3' => 't',
                   '5' => {
                            '21' => 't'
                          }
                 }
        };

Below is the output from 2nd run. Line 11 is commented out.
Code:
./test.pl
$VAR1 = 3;
$VAR2 = '3.22';
$VAR3 = '5.15';
$VAR4 = '5.16';
$VAR5 = '5.2.33';
$VAR6 = 5;
[b]Can't use string ("t") as a HASH ref while "strict refs" in use [COLOR=#EF2929]at ./test.pl line 35[/color], <RH> line 2.[/b]

I may have more questions to follow. But let me stop here for now.
Thank you so much for your time and help.
 
Hi prex,

Thanks for your post. After having read your post, I know what's wrong. But I don't know how to fix it, yet.

Below is my updated codes. The code in blue are new. Please also note that the line numbers changed too.

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $log = '/tmp/log';
my $dlmt = '_';
my $tail = 't';
[b][COLOR=#3465A4]my @ver = (3, 3.22, '5.2.33', 5, 6); # The 1st element '3' breaks the code
#@ver = (3.22, '5.2.33', 5, 6); # this is line 11.
# comment out line 11 in 2nd run[/color][/b]
print Dumper(@ver);
&cacheIt(\@ver); 
[COLOR=#3465A4][b]my $logFileContents = `cat $log`;
print "Log File Contents:\n$logFileContents\n\n";[/b][/color]
my $info = &readCache();
print Dumper($info);
exit;

sub readCache {
  my %cache = ();
  open(RH, "$log");
  my $lineCnt = 1;
  my $key;
  while(my $line = <RH>) {
    chomp($line);
    if($line =~ /$dlmt/) {
      my @field = split(/$dlmt/, $line);
      my $parent = \%cache;
      my $l = scalar @field;
      for (my $i = 0; $i < $l - 2; $i++) {
        $parent->{$field[$i]} = {} unless exists $parent->{$field[$i]};
        $parent = $parent->{$field[$i]};
      }
      if($#field > 1) {
        $parent->{$field[-2]} = $field[-1]; [COLOR=#CC0000][b]# This is line 37[/b][/color]
      }
      else {
        if(!exists($parent->{$field[-2]})) {
          $parent->{$field[-2]} = $field[-1];
        }
      }
    } # end of if($line =~ /$dlmt/)
    else {
      $key = $line;
    }
    $lineCnt++;
  } # end of while(my $line = <RH>)
  return \%cache;
}

sub cacheIt {
  my $a = $_[0];
  my %h;
  my $hRef = \%h;
  open(WH, ">$log");
  for(my $i = 0; $i <= $#$a; $i++) {
    my @tmp = split(/\./, $a->[$i]);
    my $str = join $dlmt, @tmp;
    $str .= $dlmt.$tail;
    print WH $str."\n";
  }
  close(WH);
}

First run. Line 11 takes effect:
Code:
% ./test.pl
$VAR1 = '3.22';
$VAR2 = '5.2.33';
$VAR3 = 5;
$VAR4 = 6;
Log File Contents:
3_22_t
5_2_33_t
5_t
6_t


$VAR1 = {
          '6' => 't',
          '3' => {
                   '22' => 't'
                 },
          '5' => {
                   '2' => {
                            '33' => 't'
                          }
                 }
        };

Second run. Line 11 is commented out:
Code:
./test.pl
$VAR1 = 3;
$VAR2 = '3.22';
$VAR3 = '5.2.33';
$VAR4 = 5;
$VAR5 = 6;
Log File Contents:
3_t
3_22_t
5_2_33_t
5_t
6_t


[b][COLOR=#EF2929]Can't use string ("t") as a HASH ref while "strict refs" in use at ./test.pl line 37, <RH> line 2.[/color][/b]
 
Ok, I fixed the problem - it's somewhat like a hack.

The code is simplified and the blue part is new and looks like a hack to me. I'd appreciate that if someone could polish it.

Thanks!

Code:
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $tail = 't';
[COLOR=#CC0000]my @ver = (3, 3.22, '5.2.33', 5, 6); # this is line 8.
#@ver = (3.22, '5.2.33', 5, 6); # this is line 9.
# Either line 8 or line 9 would work!![/color]
my $info = &a2h(\@ver);
print Dumper($info);
exit;

sub a2h {
  my $a = $_[0];
  my %h = ();
  my @buf;
  foreach my $x (@{$a}) {
    my @field = split(/\./, $x);
    my $last = $#field + 1;
    $field[$last] = $tail;
    my $hRef = \%h;
    my $l = scalar @field;
    for (my $i = 0; $i < $l - 2; $i++) {
      $hRef->{$field[$i]} = {} unless exists $hRef->{$field[$i]};
      $hRef = $hRef->{$field[$i]};
    }
    if($#field > 1) {
      $hRef->{$field[-2]} = $field[-1];
    }
    else {
      push @buf, $x;
    }
  } # end of while(my $line = <RH>)
  [COLOR=#3465A4]if(@buf) {
    foreach my $x (@buf) {
      my @field = split(/\./, $x);
      my $last = $#field + 1;
      $field[$last] = $tail;
      my $hRef = \%h;
      if(!exists($hRef->{$field[0]})) {
        $hRef->{$field[0]} = $field[1];
      }
    } # end of foreach my $x (@buf)
  } # end of if(@buf)[/color]
  return \%h;
}

And the output:
Code:
% ./test.pl
$VAR1 = {
          '6' => 't',
          '3' => {
                   '22' => 't'
                 },
          '5' => {
                   '2' => {
                            '33' => 't'
                          }
                 }
        };
 
In your last but one post, with line 10 in effect, in line 1 of log file you create [tt]$cache{3}='t'[/tt], then in line 2, with
[tt]$parent = $parent->{$field[$i]}[/tt]
where [tt]$i=0[/tt] and [tt]$field[$i]=3[/tt],
you set [tt]$parent='t'[/tt] and this is what perl doesn't like, when [tt]$parent[/tt] is then used as a reference.
Having not read the whole thread, it is unclear to me what is the logic of what you are trying to do; also your code is difficult to read with how you use references. You should restart from the scratch with a more readable code.

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

Personally I followed the beginning of this thread, but now I am abit lost. So I just rewrote this dumbly. As your crashes on [tt][navy]@ver[/navy] [teal]=[/teal] [teal]([/teal][green]'1'[/green][teal],[/teal] [green]'1.2'[/green][teal],[/teal] [green]'1.2.3'[/green][teal]);[/teal][/tt] I decided to not reproduce it exactly.
Perl:
[gray]#!/usr/bin/perl[/gray]

[b]use[/b] strict[teal];[/teal]
[b]use[/b] warnings[teal];[/teal]
[b]use[/b] Data[teal]::[/teal]Dumper[teal];[/teal]

[b]my[/b] [navy]$tail[/navy] [teal]=[/teal] [green][i]'t'[/i][/green][teal];[/teal]
[b]my[/b] [navy]@ver[/navy] [teal]=[/teal] [teal]([/teal][purple]3[/purple][teal],[/teal] [purple]3.22[/purple][teal],[/teal] [green][i]'5.2.33'[/i][/green][teal],[/teal] [purple]5[/purple][teal],[/teal] [purple]6[/purple][teal]);[/teal]
[b]my[/b] [navy]$info[/navy] [teal]=[/teal] [teal]&[/teal][COLOR=darkgoldenrod]a2h[/color][teal](\[/teal][navy]@ver[/navy][teal]);[/teal]
[b]print[/b] [COLOR=darkgoldenrod]Dumper[/color][teal]([/teal][navy]$info[/navy][teal]);[/teal]

[b]sub[/b] a2h [teal]{[/teal]
  [b]my[/b] [navy]$a[/navy] [teal]=[/teal] [navy]$_[/navy][teal][[/teal][purple]0[/purple][teal]];[/teal]
  [b]my[/b] [navy]%h[/navy] [teal]=[/teal] [teal]();[/teal]
  [b]my[/b] [navy]@buf[/navy][teal];[/teal]
  [b]foreach[/b] [b]my[/b] [navy]$x[/navy] [teal]([/teal]@[teal]{[/teal][navy]$a[/navy][teal]}[/teal][teal])[/teal] [teal]{[/teal]
    [b]my[/b] [navy]@field[/navy] [teal]=[/teal] [b]split[/b][teal]([/teal][green][i]/\./[/i][/green][teal],[/teal] [navy]$x[/navy][teal]);[/teal]
    [b]my[/b] [navy]$hRef[/navy] [teal]=[/teal] [teal]\[/teal][navy]%h[/navy][teal];[/teal]
    [b]my[/b] [navy]$hPrev[/navy][teal];[/teal]
    [b]my[/b] [navy]$l[/navy] [teal]=[/teal] scalar [navy]@field[/navy][teal];[/teal]
    [b]for[/b] [teal]([/teal][b]my[/b] [navy]$i[/navy] [teal]=[/teal] [purple]0[/purple][teal];[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$l[/navy][teal];[/teal] [navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
      [navy]$hPrev[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy] [teal]-[/teal] [purple]1[/purple][teal]][/teal][teal]}[/teal] [teal]=[/teal] [navy]$hRef[/navy] [teal]=[/teal] [teal]{}[/teal] [b]unless[/b] [b]ref[/b] [navy]$hRef[/navy] [b]eq[/b] [green][i]'HASH'[/i][/green][teal];[/teal]
      [navy]$hRef[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal] [teal]=[/teal] [navy]$i[/navy] [teal]<[/teal] [navy]$l[/navy] [teal]-[/teal] [purple]1[/purple] [teal]?[/teal] [teal]{}[/teal] [teal]:[/teal] [navy]$tail[/navy] [b]unless[/b] [b]exists[/b] [navy]$hRef[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
      [navy]$hPrev[/navy] [teal]=[/teal] [navy]$hRef[/navy][teal];[/teal]
      [navy]$hRef[/navy] [teal]=[/teal] [navy]$hRef[/navy][teal]->[/teal][teal]{[/teal][navy]$field[/navy][teal][[/teal][navy]$i[/navy][teal]][/teal][teal]}[/teal][teal];[/teal]
    [teal]}[/teal]
  [teal]}[/teal]
  [b]return[/b] [teal]\[/teal][navy]%h[/navy][teal];[/teal]
[teal]}[/teal]
I know, not much readable. So no hard feelings if you prefer to not use it.

Feherke.
feherke.github.io
 
To Feherke,

Actually, I like it a lot and have learned a lot from your posts, including this one!

Thank you very much. Best regards to you.

To prex,

I have already realized what was wrong after I read your first response. Thank you.

>it is unclear to me what is the logic of what you are trying to do
I know and I am sorry. In my first post in this thread, I said what I was trying to accomplish. With Feherke's help, I have achieved my goal.

Then I started to work on a new problem and I thought I cold leverage what I learned from Feherke's post in this thread.

Then I got this error: Can't use string ("t") as a HASH ref while "strict refs" in use at ./test.pl line 37, <RH> line 2.

So I decided to ask for help again in this thread w/o clearly explaining what my goal is. Sorry about this.

With Feherke's help, I have made a little progress. But I have further questions which I'll ask for your help in a new thread today or tomorrow (Time Zone: US EDT).

Sorry for the confusion and thank you so much for your time and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top